Repository navigation
Fix flaky WebApplicationFactory Kestrel port race - #67714
Merged
Merged
Conversation
Configure the Kestrel endpoint via KestrelServerOptions.Listen instead of
mutating IServerAddressesFeature.Addresses after the host is built. For
minimal/WebApplication apps run via DeferredHostBuilder, the app starts
Kestrel on its own thread, which can make the addresses collection read-only
before the factory could update it, causing an intermittent
InvalidOperationException ("IServerAddressesFeature.Addresses cannot be
modified after the server has started").
Also un-quarantines
RealServerUsingMinimalBackedIntegrationTests.CanResolveServices, whose root
cause is this same race.
Co-authored-by: Copilot App <[email protected]>
This was referenced Nov 4, 2026
This was referenced Nov 5, 2026
BrennanConroy
marked this pull request as ready for review
November 10, 2026 23:46
BrennanConroy
requested review from
a team,
Youssef1313,
cincuranet,
davpetr and
rokonec
as code owners
November 10, 2026 23:46
BrennanConroy
requested review from
DeagleGross and
halter73
and
a balanced review from Copilot
November 10, 2026 23:46
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The Kestrel configuration can ignore the factory’s requested port and retain existing endpoints, breaking dynamic-port isolation and causing port collisions or startup failures.
3 open issues
What changed in this PR
This PR addresses the WebApplicationFactory startup race behind #67340 by configuring the Kestrel port before binding rather than modifying server addresses after the host is built.
Changes:
- Configures a loopback listener through Kestrel options for the requested port.
- Removes
TryConfigureServerPortand its post-build calls from both host creation paths.
| File | Description |
|---|---|
src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs |
Moves port selection into Kestrel configuration to avoid racing with server startup. |
Review effort: Balanced
| { | ||
| builder.UseKestrel(_configureKestrelOptions); | ||
| } | ||
| else if (_kestrelPort is int port) |
| // (e.g. minimal apps run via DeferredHostBuilder) bind Kestrel on their own thread | ||
| // and can make the addresses collection read-only before the factory could update it, | ||
| // causing an intermittent InvalidOperationException. | ||
| builder.UseKestrel(options => options.Listen(IPAddress.Loopback, port)); |
| // (e.g. minimal apps run via DeferredHostBuilder) bind Kestrel on their own thread | ||
| // and can make the addresses collection read-only before the factory could update it, | ||
| // causing an intermittent InvalidOperationException. | ||
| builder.UseKestrel(options => options.Listen(IPAddress.Loopback, port)); |
DeagleGross
approved these changes
Nov 11, 2026
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 11, 2026 10:23 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 11, 2026 10:23 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 11, 2026 10:24 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 11, 2026 10:26 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 11, 2026 10:27 — with
GitHub Actions
Inactive
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 11, 2026 10:27 — with
GitHub Actions
Inactive
Contributor
|
✅ No documentation PR was created. Confidence that documentation is needed: 8% No docs needed: internal bug fix for a flaky port race in WebApplicationFactory with Kestrel; no public API or documented behavior change. Documentation surfaces considered
|
DeagleGross
temporarily deployed
to
copilot-pat-pool
November 11, 2026 10:27 — with
GitHub Actions
Inactive
2 of 4 tasks
3 of 4 tasks
This branch was previously deployed
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.

Fix a flaky WebApplicationFactory Kestrel port race
Description
RealServerUsingMinimalBackedIntegrationTestsintermittently fails with:Root cause
When
WebApplicationFactoryis configured withUseKestrel(port), it calledTryConfigureServerPort, which cleared and re-added entries inIServerAddressesFeature.Addressesafterbuilder.Build(). For minimal /WebApplicationapps the app owns its own lifecycle:DeferredHostBuilder.Build()runs the entry point viaHostFactoryResolver, and the app starts Kestrel on its own thread. Kestrel'sBindAsynccallsPreventPublicMutation(), making the addresses collection read-only. That bind races with the factory'sAddresses.Clear()on the test thread; when Kestrel wins the race,Clear()throws.Fix
Instead of mutating the addresses after the host is built, configure the Kestrel endpoint directly via
KestrelServerOptions.Listen(IPAddress.Loopback, port)inConfigureBuilderToUseKestrel. This runs during the app's own startup, before binding, so there is no post-start mutation and no race. WithPreferHostingUrls == false(the default),AddressBinderusesOverrideWithEndpointsStrategy, so the configured endpoint overrides any addresses the app set viaapp.Urls, and the resolved (possibly dynamic) port is written back into the addresses feature soTryExtractHostAddressstill reads the real bound port.TryConfigureServerPortis removed.This also un-quarantines
RealServerUsingMinimalBackedIntegrationTests.CanResolveServices, which was quarantined for the same exception.Verification
Microsoft.AspNetCore.Mvc.Testingbuilds clean.Listen(Loopback, 0)endpoint overrides a non-emptyapp.Urlsand reports the real dynamically assigned port (the semantics the factory relies on). The fullMvc.FunctionalTestsproject requires the native IIS/ANCM build, so the real-server tests will be validated in CI.Fixes #67340