Skip to content

[OpenAPI] Fix shutdown hanging during a definition update - #10792

Merged
glen-84 merged 4 commits into
mainfrom
gai/openapi-shutdown-deadlock
Nov 4, 2026
Merged

glen-84 merged 4 commits into
mainfrom
gai/openapi-shutdown-deadlock

Conversation

@glen-84

@glen-84 glen-84 commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Summary

  • 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.

Copilot AI lite review requested due to automatic review settings November 4, 2026 07:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

1 open issue
What changed in this PR

This pull request prevents OpenAPI definition updates from racing with ASP.NET Core host shutdown and endpoint disposal, avoiding shutdown hangs.

Changes:

  • Adds coordinated shutdown handling for OpenAPI registries.
  • Stops new definition updates, unsubscribes registries, and waits for active updates to finish.
  • Moves endpoint change notifications outside the endpoint-data-source lock.
  • Adds integration and concurrency tests covering shutdown and endpoint access behavior.

This pull request requires changes.

File Review notes
src/​HotChocolate/​Adapters/​test/​Adapters.OpenApi.Tests/​OpenApiTestBase.cs Adds definition-loading hooks and subscriber-count tracking for shutdown tests.
src/​HotChocolate/​Adapters/​test/​Adapters.OpenApi.Tests/​Endpoints/​HttpEndpointIntegrationTests.cs Tests executor rebuilding after shutdown without reloading definitions.
src/​HotChocolate/​Adapters/​test/​Adapters.OpenApi.Tests/​Endpoints/​HttpEndpointIntegrationTestBase.cs Tests waiting for active updates and unsubscribing during shutdown.
src/​HotChocolate/​Adapters/​test/​Adapters.OpenApi.Tests/​DynamicEndpointDataSourceTests.cs Tests concurrent endpoint reads during change callbacks.
src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.Core/​OpenApiShutdownService.cs Coordinates OpenAPI shutdown through the hosted service lifecycle.
src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.Core/​OpenApiManager.cs 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.
src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.Core/​OpenApiDefinitionRegistry.cs Tracks stopped state, unsubscribes from storage, and waits for active updates.
src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.Core/​Extensions/​InternalServiceCollectionExtensions.cs Registers the shutdown service.
src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.AspNetCore/​HotChocolate.Adapters.OpenApi.AspNetCore.csproj Grants tests access to internal ASP.NET Core adapter types.
src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.AspNetCore/​DynamicEndpointDataSource.cs Raises endpoint change notifications after releasing the synchronization lock.

Review effort: Lite

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Patch coverage

95.1% of changed lines covered (78/82)

File Covered Changed Patch %
…/src/Adapters.OpenApi.Core/OpenApiDefinitionRegistry.cs 34 38 89.5% 🟡
…/src/Adapters.OpenApi.AspNetCore/DynamicEndpointDataSource.cs 4 4 100.0% 🟢
…/Extensions/InternalServiceCollectionExtensions.cs 1 1 100.0% 🟢
…/Adapters/src/Adapters.OpenApi.Core/OpenApiManager.cs 36 36 100.0% 🟢
…/Adapters/src/Adapters.OpenApi.Core/OpenApiShutdownService.cs 3 3 100.0% 🟢
Uncovered changed lines (JSON)
{
  "sha": "b18cc87190f7b63d20a69952154e8221e5a52760",
  "files": [
    { "path": "src/HotChocolate/Adapters/src/Adapters.OpenApi.Core/OpenApiDefinitionRegistry.cs", "ranges": [[52, 53], [107, 108]] }
  ]
}

Project coverage: 59.4% (321357/540710 lines)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Medium severity Serial registry shutdown can leave later registries running

src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.Core/​OpenApiManager.cs:90

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.

Medium severity Registry construction can subscribe after manager shutdown

src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.Core/​OpenApiManager.cs:107

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.

1 resolved since last review

Review effort: Lite

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

High severity Cancelled shutdown barrier can allow endpoint updates during disposal

src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.Core/​OpenApiManager.cs:96

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.

High severity Manager disposal does not drain in-flight storage updates

src/​HotChocolate/​Adapters/​src/​Adapters.OpenApi.Core/​OpenApiManager.cs:152

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.

Review effort: Lite

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

No open issues 🎉

Review effort: Lite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants