Skip to content

Conversation

@nmoATusercube
Copy link

ReconfigureTenant removes the tenant scope before adding the reconfigured one. If another thread calls GetTenantScope between the remove and the add, an empty scope is kept in the _tenantLifetimeScopes dictionary.

…configureTenant to set the new tenant scope instance.
@codecov
Copy link

codecov bot commented Jan 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (1e4c063) 90.62% compared to head (d902b77) 93.57%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop      #38      +/-   ##
===========================================
+ Coverage    90.62%   93.57%   +2.95%     
===========================================
  Files            5        5              
  Lines           96      109      +13     
  Branches        27       28       +1     
===========================================
+ Hits            87      102      +15     
+ Misses           4        3       -1     
+ Partials         5        4       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tillig
Copy link
Member

tillig commented Jan 24, 2024

While I see what this is trying to do, part of the challenge here is that there's an assumption that configuring Autofac is thread-safe. Component registration and container building have never been thread-safe.

There's going to be the same race condition if someone is calling DisposeAsync on one thread while ReconfigureTenant is called on another.

We used to have reader/writer locking (#8) but that got removed because it didn't work with DisposeAsync (#35) (as an example of why this may not be as easy as all that).

Anyway, that's what I'm thinking about.

I wonder if this would be a good use of ConcurrentDictionary.AddOrUpdate or maybe ConcurrentDictionary.TryUpdate - ways to use the locking inside ConcurrentDictionary so we don't have to kind of hack around it.

…un in parallel if needed (in the previous solution, calls to GetTenantScope could return an empty scope until the call the ReconfigureTenant is completed)
@nmoATusercube
Copy link
Author

I completely agree. The first solution aimed at minimize the amount of changes while making ReconfigureTenant deterministic.
The new commit implements your suggestion of AddOrUpdate to atomically change the tenants dictionary while respecting the return value behavior.

Copy link
Member

@tillig tillig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking nice, I like the updated use of the ConcurrentDictionary bits. I have a couple of inline comments, but minor stuff. Thanks!

@nmoATusercube
Copy link
Author

I have also added a test for the case where we go through the Add path in the AddOrUpdate.

Copy link
Member

@tillig tillig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome. Let me get this merged in and I'll make a 0.0.1 release with it.

@tillig tillig merged commit 6c6a8d2 into autofac:develop Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants