Repository navigation
Conversation
Apply the requested port before starting the legacy web host so captured startup service errors cannot bypass the override. Retain the startup filter for generic and minimal hosts, reusing the address configuration helper. Add a real-server regression that keeps the application port occupied and checks the captured startup error is served on a dynamic loopback port. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the legacy-host port-override change.
No open issues 🎉
What changed in this PR
Preserves WebApplicationFactory.UseKestrel(port) overrides when a legacy host captures a startup-services exception, without reintroducing the minimal-hosting startup race addressed by earlier fixes.
Changes:
- Applies the requested address after building the legacy host and before starting it.
- Reuses address-configuration logic while retaining the startup filter for generic and minimal hosts.
- Adds a regression test that occupies the application’s configured port and verifies the original startup error is served over HTTP on a different dynamic loopback port.
| File | Description |
|---|---|
src/Mvc/test/Mvc.FunctionalTests/RealServerPortOverrideTests.cs |
Adds real-server coverage for port overrides during captured startup-services failures. |
src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs |
Applies legacy-host port overrides before startup and extracts shared address configuration. |
Review effort: Balanced
kubaflo
left a comment
There was a problem hiding this comment.
Review summary
Reviewed 72042c1fe03750c74dae826180100626471aab69, target main, frozen base tip 6f80be5513b2f1d785d51752f9cd065c56af4c6f, verified merge base 90d90ff3574fb9b0b59838e9cebca908e6af93c5. Read both changed files in full and traced legacy WebHost initialization, startup-error recovery, address selection, factory client-address extraction, and the unchanged generic/minimal-host path. Reconciled CCR agreement 5947628803; no existing inline defect finding was present.
No actionable defect established. The source supports the reported mechanism: WebHost.Initialize retains the startup-services exception when capture is enabled; BuildApplication rethrows it before composing startup filters, then constructs the error-page application in its catch. Thus the port startup filter does not run on this path although Kestrel still starts.
The new legacy-only pre-start call gets IWebHost.ServerFeatures (which ensures the server), replaces the advertised addresses with the requested loopback port, and sets PreferHostingUrls. AddressBinder.CreateStrategy gives those nonempty preferred addresses precedence over configured endpoints. The error-page path subsequently reuses that server; it does not restore the application's URL. The generic/minimal host still uses the startup filter at the existing pre-bind seam, so the former minimal-host race fix is not replaced with a later post-start mutation. The helper extraction preserves the existing filter's behavior.
Regression-test assessment: the new test holds a real TCP listener open on the application's configured port, throws through IStartup.ConfigureServices on BasicWebSite's actual legacy builder, and requires a different bound loopback endpoint plus the original startup exception served as HTTP 500. That exercises the missing-filter producer and consumer-visible error page rather than directly testing the helper. Existing callback/URL/explicit-endpoint/minimal-host/short-circuit tests remain unchanged. The test verifies both port selection and preservation of the startup error rather than accepting a successful bind alone.
Validation limits: git diff --check 90d90ff3574fb9b0b59838e9cebca908e6af93c5 72042c1fe03750c74dae826180100626471aab69 passed. The checkout requires SDK 12.0.100-alpha.1.26557.102; local dotnet --list-sdks reports only 11.0.100. I did not substitute that SDK, restore a toolchain, or run the functional suite. The reported 16 passing tests and historical-source red/green runs remain author-reported, not independently reproduced here. No claim that sockets or HTTP behavior were empirically validated by this review.
The title/description match the implementation and distinguish managed HTTP coverage from excluded IIS/browser/full-suite work. Required Build Analysis is pending; license/cla passes. Verdict: NEEDS_DISCUSSION; safety confidence: low, for shared startup infrastructure, pending CI, and unavailable exact-SDK execution rather than a newly established defect.
GPT-6.1 Sol source/text/CLI review; no relevant supplied visual media or UI change, browser/device or model campaign, product edits, approval, request-changes event, merge, or thread resolution.
|
/review |
|
✅ No documentation PR was created. Confidence that documentation is needed: 10% No docs needed: bug fix restoring expected port-override behavior; confident, not an insufficient-evidence case. Documentation surfaces considered
|
Preserve WebApplicationFactory ports for captured startup errors
Honor Kestrel port overrides when legacy startup services fail
Description
With a legacy
IWebHostBuilder,CaptureStartupErrors(true), and an exception inStartup.ConfigureServices, the host skips startup filters but still starts Kestrel to serve the startup error page. Since the factory's port override is applied by a startup filter,factory.UseKestrel(0)is ignored on this path. Kestrel instead attempts to bind the application's configured URL, which can fail if that port is already in use.Apply the requested port after building the legacy web host and before starting it. Keep the existing startup filter for generic and minimal hosts, reusing the address configuration logic without restoring the minimal-hosting startup race.
Add
RealServerPortOverrideTests.PortOverrideIsAppliedWhenStartupServicesThrow. The test keeps the application's configured port occupied, throws from the real startup services path, and verifies that the factory serves the original startup error over HTTP on a different dynamic loopback port.Validation
Passed: 16, failed: 0, skipped: 0.
Before the fix, the new regression fails with the expected occupied-port bind error; the six existing port-override tests pass. The same regression passes using the exact factory source from before #67714 and from after #67714 / before #70520, compiled against the remaining current source graph.
Validation covers real Kestrel sockets, legacy startup-error handling, generic/minimal hosting, and existing port-precedence scenarios. JavaScript/static assets and IIS native assets are disabled for this focused managed HTTP run; browser/IIS behavior and the full MVC suite were not validated.
Follow-up to #70520; historical context: #67714.