Repository navigation
Make the IoC container safe for concurrent registration and resolution - #11011
Richard Park (richardpark-msft) wants to merge 1 commit into
Conversation
|
Azure Pipelines: Successfully started running 1 pipeline(s). 26 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🟡 Changes recommended
Eagerly publishing replacement bindings can cause recursive resolution, cache failed constructor results, or invoke the wrong resolver during concurrent registration.
4 open issues
What changed in this PR
Introduces copy-on-write registration in azd’s dependency-injection container so extension providers can register while other requests resolve services.
Changes:
- Publish binding snapshots atomically and serialize registration without holding locks during resolver execution.
- Add concurrency, reentrancy, and singleton-cache regression tests.
- Document the locking contract and concurrent singleton-initialization limitation.
| File | Description |
|---|---|
cli/azd/pkg/ioc/container.go |
Adds atomic binding publication, registration locking, and scoped-list snapshots. |
cli/azd/pkg/ioc/container_test.go |
Checks that cached singletons survive later registrations. |
cli/azd/pkg/ioc/container_race_test.go |
Adds concurrent registration, resolution, scope, and reentrancy tests. |
cli/azd/docs/concurrency-model.md |
Documents container synchronization and singleton limitations. |
Review effort: Max
| if err := c.RegisterSingleton(resolveFn); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Resolve after RegisterSingleton releases registerMu, since the resolver may register into this container. | ||
| instance := reflect.New(reflect.TypeOf(resolveFn).Out(0)) | ||
| return c.bindings().Resolve(instance.Interface()) |
| // from the container resolver registrations | ||
| func (c *NestedContainer) Invoke(resolver any) error { | ||
| return c.inner.Call(resolver) | ||
| return c.bindings().Call(resolver) |
| childContainer := NewNestedContainer(c) | ||
|
|
||
| for _, binding := range c.scopedBindings { | ||
| for _, binding := range c.scopedBindingsSnapshot() { |
| require.NoError(t, provisioningErr) | ||
| require.NoError(t, generationErr) |
Extensions register providers over gRPC while azd (and other extensions) resolve services from the same container. golobby's container is a plain map, so a registration could race with any resolve - and the first registration of a brand-new type races with resolves of every type. Registration now copies the bindings, binds into the copy and publishes it atomically under registerMu. Resolves read a published map that is never written again, and the lock is never held while a resolver runs, so resolvers can still register into and resolve from the same container. Concurrent first resolutions of the same singleton remain unsynchronized, which matches existing behavior.
3e18e19 to
1ddb821
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved correctness issues allow failed eager registrations to replace valid bindings and concurrent scope creation to inherit services without their dependencies.
7 open issues
Initialize replacements privately before merging bindings Later Invoke arguments miss registrations made by earlier resolvers · New Scope snapshots can omit dependencies registered with their consumers · New Capture nested-container state in one locked snapshot Refresh bindings between dependency lookups Concurrent singleton resolution races on the cache · New Verify concurrent registrations preserve both bindings
Review effort: Max
| // from the container resolver registrations | ||
| func (c *NestedContainer) Invoke(resolver any) error { | ||
| return c.inner.Call(resolver) | ||
| return c.bindings().Call(resolver) |
| childContainer := NewRegistrationsOnly(c) | ||
|
|
||
| for _, binding := range c.scopedBindings { | ||
| for _, binding := range c.scopedBindingsSnapshot() { |
| **Not covered**: two goroutines resolving the same singleton for the first time | ||
| can both construct it. Use `lazy.Lazy` when shared initialization must run once. |
Azure Dev CLI Install InstructionsInstall scriptsMacOS/Linux
bash: pwsh: WindowsPowerShell install MSI install Standalone Binary
MSI
Documentationlearn.microsoft.com documentationtitle: Azure Developer CLI reference
|



Extensions register providers over gRPC while azd (and other extensions)
resolve services from the same container. golobby's container is a plain
map, so a registration could race with any resolve - and the first
registration of a brand-new type races with resolves of every type.
Registration now copies the bindings, binds into the copy and publishes it
atomically under registerMu. Resolves read a published map that is never
written again, and the lock is never held while a resolver runs, so
resolvers can still register into and resolve from the same container.
Concurrent first resolutions of the same singleton remain unsynchronized,
which matches existing behavior.
Stack created with GitHub Stacks CLI • Give Feedback 💬