Repository navigation
1. Each single instance is created under its own lock
Previously the generated code guarded the creation of every singleton, scoped and per-resolve instance with one lock of the composition. Two compositions could therefore wait for each other forever: one thread held the lock of the first composition and needed the second one, while another thread held the second and needed the first. In an application this depended on timing, so it was hard to reproduce and looked like a random hang. Fixed #206: deadlock when two compositions create each other's singletons on two threads.
- A singleton, scoped or per-resolve instance is now checked and created under a lock of its own, so creating it waits only for the instances it depends on. Compositions that create the singletons of each other on several threads no longer deadlock.
- Independent instances no longer wait for each other either: two threads that resolve a singleton or a scoped graph which shares nothing now run in parallel.
- A per-resolve instance that lambdas may create on several threads gets a lock of its own as well, so it is still created once.
2. Locks appear only when they are needed
- A lock of an instance is created by the first thread that needs it, through a single compare-exchange, so an instance that is never created does not allocate a lock.
- As before, the generated code uses
System.Threading.Lockon .NET 9 and later and anobjecton earlier targets. With theThreadSafehint disabled, an instance is not locked while it is created unless a factory usesctx.Lock.
3. ctx.Lock is the lock of the created instance
The meaning of ctx.Lock in a factory is now narrower and more precise, which matters for a factory that locks it:
- In a factory of a singleton, scoped or per-resolve instance it is the lock of that instance. The instance is checked once more under it, so it is still created only once even when thread safety is disabled by the
ThreadSafehint. - In a factory of another instance it is a lock created together with the instance and shared by the lambdas that the factory creates, so parallel calls of the same delegate do not mix their overrides.
- It is not a lock of the whole composition any more. The lock of the composition now guards only the accumulators, the disposables and
Dispose.
A factory that locks ctx.Lock keeps working and now takes exactly the lock of the instance it initializes, as described in Factory with thread synchronization and IsLockRequired.
4. Less locking in the generated code
- A root that is an ordinary member of the composition no longer takes the composition lock to apply overrides while it creates the value.
- Accumulators of such a root are guarded by the composition lock only when thread safety is enabled.
- Instances that belong to the composition are still added to its disposables under the composition lock, even when the instance itself was created under its own lock.
Closed issues
- #206 - deadlock when two compositions create each other's singletons on two threads; reported by EmilienDup with a minimal reproduction where two threads meet inside the constructors.
Full Changelog: 2.5.10...2.5.11