You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[OpenAPI] Fix shutdown hanging during a definition update - #10792
Stopping and disposing a host that uses the OpenAPI adapter could hang forever when a definition update was being applied at the same time. Applying an update changes the adapter's endpoint data source, and ASP.NET Core's routing caches deadlock when an EndpointDataSource changes on another thread while they are being disposed. This is the hang that timed out the HotChocolate.Adapters.OpenApi.Tests job in CI.
The adapter now stops applying definition updates when the host stops. It unsubscribes from the definition storage and waits for an update already in progress, so no endpoint change overlaps disposal. Neither an executor rebuilt after the host stopped nor an OpenAPI registration created after it loads definitions.
The endpoint data source raises its change notification outside its lock, so a consumer can read Endpoints from another thread while it handles the change.
Test plan
New tests, run for both Hot Chocolate and Fusion: stopping the host waits for a definition update in progress and unsubscribes from the definition storage. A Hot Chocolate test checks that an executor rebuilt after the host stopped doesn't load definitions.
New OpenApiManagerTests: a registration created after the manager stopped doesn't subscribe to the definition storage.
New DynamicEndpointDataSourceTests: another thread can read the endpoints while a change callback runs.
HotChocolate.Adapters.OpenApi.Tests passes on net11.0, net10.0, and net9.0.
Ran the project with --coverage, as CI does, 127 times without a hang. Without the fix, 4 of 130 runs hung.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
A concurrency gap in OpenApiManager.StopAsync allows late registrations to remain subscribed and update endpoints after shutdown begins, leaving the endpoint/disposal race unresolved.
Moderate, 4 votes:StopAsync can miss registrations added concurrently by Get, allowing late registries to remain subscribed and update endpoints after shutdown begins. Synchronize or reject late registration creation, or immediately stop registrations created after shutdown starts.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Two moderate shutdown-race issues remain: registration can subscribe after shutdown begins, and serial stopping can leave later registries subscribed when cancellation occurs.
No open issues 🎉
2 previously missed issues
In code that hasn't changed since last review
Serial registry shutdown can leave later registries running
This waits for registries serially. With multiple schema registrations, simultaneous updates make shutdown take the sum of their durations; if the host's bounded shutdown token is canceled while the first registry is waiting, the loop exits before later registries are stopped, leaving them subscribed and able to call SetEndpoints during disposal. Stop/unsubscribe all registries before awaiting them, or await all StopAsync calls concurrently so one slow/canceled registry cannot prevent the others from being stopped.
Registry construction can subscribe after manager shutdown
OpenApiDefinitionRegistry subscribes to the storage in its constructor, so this line still calls storage.Subscribe even when CreateRegistration is racing with a manager that has already stopped. The registration is only stopped after that subscription is created; a storage can therefore start resources or throw from Subscribe after host shutdown, and the new test only checks the final count so it does not catch the transient subscription. Guard construction/subscription under the stop state, or make the registry subscribe lazily after it has been admitted to _runningRegistries.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Two critical shutdown races remain: disposal can publish updates after the endpoint data source is disposed, and cancellation can release the shutdown barrier before an in-flight update finishes.
No open issues 🎉
2 previously missed issues
In code that hasn't changed since last review
Cancelled shutdown barrier can allow endpoint updates during disposal
Passing the host cancellation token into WaitForUpdateAsync lets StopAsync finish with cancellation while an update is still inside GetDefinitionsAsync. The update paths do not re-check _stopped/_disposed after that await, so a shutdown timeout can still reach UpdateAllDefinitions and SetEndpoints while ASP.NET Core is disposing routing state, which reintroduces the race this barrier is intended to prevent. Keep the shutdown barrier alive independently of the host cancellation, or add a post-read stopped/disposed guard to both update paths before publishing endpoints.
Manager disposal does not drain in-flight storage updates
OpenApiShutdownService.StopAsync is not invoked when an IHost is disposed without an explicit StopAsync (for example, await using an already-started host). In that path the service provider disposes DynamicEndpointDataSource before this manager's Dispose can dispose its registries, but Dispose does not wait for an in-flight HandleStorageChangedAsync; that update can still call SetEndpoints after the data source has been disposed. Please make the manager's disposal path participate in the same async drain, or otherwise guarantee the hosted-service stop runs before provider disposal.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Concurrency and lifecycle coordination around stopping and queued schema updates presents a significant correctness risk that requires human assessment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EndpointDataSourcechanges on another thread while they are being disposed. This is the hang that timed out theHotChocolate.Adapters.OpenApi.Testsjob in CI.Endpointsfrom another thread while it handles the change.Test plan
OpenApiManagerTests: a registration created after the manager stopped doesn't subscribe to the definition storage.DynamicEndpointDataSourceTests: another thread can read the endpoints while a change callback runs.HotChocolate.Adapters.OpenApi.Testspasses on net11.0, net10.0, and net9.0.--coverage, as CI does, 127 times without a hang. Without the fix, 4 of 130 runs hung.