Repository navigation
GH-4500: only ever write ASP.NET Core's endpoint data sources from the composition thread - #4503
Merged
Merged
Conversation
…e composition thread RouteOptions.EndpointDataSources is a plain ObservableCollection, and CompositeEndpointDataSource.CreateEndpointsUnsynchronized() walks it with a bare foreach. UseRouting/UseAuthorization trigger that walk on the startup thread while the pipeline is being built, so an Add from any other thread at that moment throws "Collection was modified" on the startup thread and the host does not start at all. GH-3421 published the host's endpoints into that collection lazily, from WolverineApiDescriptionProvider.OnProvidersExecuting -- i.e. from whatever thread happened to read the ApiExplorer. A monitoring observer taking a capabilities snapshot as the runtime starts is exactly that, which made Wolverine the concurrent writer that killed the startup thread's enumeration. Publish once instead, from MapWolverineEndpoints(), on the thread composing the application -- the same thread ASP.NET Core writes that collection from. To stay complete for endpoints mapped after that call (MapControllers(), a later MapGroup()), what gets registered is a single deferred UnpublishedEndpointDataSource that answers for the route builder's data sources minus whatever UseEndpoints() has already published, so nothing is registered twice on either side of host start. Reading the ApiExplorer now mutates nothing. Also harden the reader that hit this. HttpGraphUsageSource indexed the ApiExplorer once per chain, searching every group each time -- quadratic on the several-hundred-endpoint host in the report. Index once, and give up the OpenAPI shape rather than the whole capabilities snapshot if the read throws. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VDUrBeB4tTnKj4AExCS1nj
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4500.
The bug
RouteOptions.EndpointDataSourcesis a plainObservableCollection<EndpointDataSource>, andCompositeEndpointDataSource.CreateEndpointsUnsynchronized()walks it with a bareforeach.UseRouting/UseAuthorizationtrigger that walk on the startup thread while the pipeline is being built. AnAddfrom any other thread at that moment throwsInvalidOperationException: Collection was modifiedon the startup thread — and the host does not start.GH-3421 made Wolverine publish the host's endpoints into that collection lazily, from
WolverineApiDescriptionProvider.OnProvidersExecuting— i.e. from whatever thread happened to read the ApiExplorer. A monitoring observer building a capabilities snapshot as the runtime starts is exactly that, so Wolverine became the concurrent writer that killed the startup thread's enumeration. The old code documented the hazard and reasoned past it:True of the read, but the write was ours, and the thing that died was startup.
The fix
Wolverine writes that collection from exactly one place now:
MapWolverineEndpoints(), on the thread composing the application — the same thread ASP.NET Core writes it from.Publishing the route builder's data sources eagerly at map time would have regressed GH-3421 for anything mapped after that call (
MapControllers(), a laterMapGroup()), so what gets registered is one deferredUnpublishedEndpointDataSource: a view overrouteBuilder.DataSourcesminus whateverUseEndpoints()has already published. Before start it answers for everything; after start it answers for nothing, so no endpoint is ever registered twice on either side of host start. Reading the ApiExplorer mutates nothing at all.While in here, the reader that actually hit this:
HttpGraphUsageSourcesearched every ApiExplorer group once per chain, which is quadratic on the several-hundred-endpoint host in the report. It now indexes once, and gives up the OpenAPI shape rather than the whole capabilities snapshot if the read throws.Test
api_explorer_before_host_start.reading_the_api_explorer_does_not_mutate_the_live_endpoint_collectionis deterministic rather than timing-dependent: a gate data source blocks insideEndpoints, holding the collection's enumerator open exactly the wayCreateEndpointsUnsynchronizeddoes mid-startup, while the test thread performs the ApiExplorer read. Againstmainit fails with the issue's own stack trace:The existing GH-3373 / GH-3421 tests carry the rest: descriptions complete before start, minimal API routes present on a hybrid host, no duplicate registration once the host starts, and route groups still registered exactly once.
Verification
Full
wolverine.slnxbuilt Release pinned to-f net9.0.Wolverine.Http.Testsgreen (1053 passed, 10 skipped).🤖 Generated with Claude Code
https://claude.ai/code/session_01VDUrBeB4tTnKj4AExCS1nj