Skip to content

[release/17.0] Reject occupied loopback ports during endpoint allocation - #22037

Closed
Mitch Denny (mitchdenny) wants to merge 1 commit into
mitchdenny-backport-17-0-38-pr21852from
mitchdenny-backport-17-0-39-pr21961
Closed

Mitch Denny (mitchdenny) wants to merge 1 commit into
mitchdenny-backport-17-0-38-pr21852from
mitchdenny-backport-17-0-39-pr21961

Conversation

@mitchdenny

Copy link
Copy Markdown
Member

Description

Backport of #21961 to release/17.0 (cherry-picked from f02c7f8 with no modifications).

Layer 39 of 55 in the release/17.0 backport stack; stacked on #22036.

Backport recommendation: Yes — macOS could hand out a port already in use on loopback.

Validation: Stable patch IDs match. Builds/tests not run for this unchanged cherry-pick.

Backport of #21961

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No

<!-- issue-pr-loop -->
## Description

On macOS, `ProxylessEndpointPortAllocator.TryProbePort` reports an
occupied loopback TCP port as available. An existing listener bound to
`127.0.0.1:<port>` with `ReuseAddress` enabled does not prevent the
allocator's exclusive wildcard bind to `0.0.0.0:<port>`. A later
workload binding that exact loopback address fails with `Address already
in use`; a proxy may meanwhile reach the existing listener.

Probe both wildcard and loopback addresses in IPv4 and IPv6. Release the
wildcard sockets before probing loopback to avoid detecting the probe's
own sockets. Keep the existing best-effort error handling, allocation
range, reservation tracking and disposal behavior.

The regression holds an OS-assigned loopback socket open throughout the
assertion, with TCP/UDP and IPv4/IPv6 coverage. No fixed port, timing
margin, retry or test serialization is needed.

Validation selected before execution:
- Before the fix, `ProbeRejectsOccupiedLoopbackPorts` fails for TCP:
expected unavailable, actual available. UDP control passes.
- After the fix, `ProxylessEndpointPortAllocatorTests`: **15/15
passed**, zero skips; **2.24s wall / 0.0272 command vCPU-minutes**.
Includes production free-port allocation and dependent
reservation/exclusion tests.
- Ran the exact source-linked allocator, `PortRange`, and test class on
.NET 10.0.14 in an isolated net10.0 harness using xUnit v3 4.0.2 and
shipped public endpoint types. Only internal `DcpOptions` range
properties were shimmed. The repository-wide .NET 11 RC SDK is
unavailable locally, so the original test-project build and full AppHost
scenario remain for CI; these are not claimed validated.
- Full diff reviewed for reuse, quality, efficiency and cleanup.
Whitespace check passes. Test output swept: no unexpected exceptions or
runtime errors.

## Checklist

- Is this feature complete?
  - [x] Yes. Ready to ship.
  - [ ] No. Follow-up changes expected.
- Are you including unit tests for the changes and scenario tests if
relevant?
  - [x] Yes
  - [ ] No
- Did you add public API?
  - [ ] Yes
  - [x] No
- Does the change make any security assumptions or guarantees?
  - [ ] Yes
  - [x] No

(cherry picked from commit f02c7f8)
@github-actions

This comment has been minimized.

@mitchdenny
Mitch Denny (mitchdenny) force-pushed the mitchdenny-backport-17-0-39-pr21961 branch from a5ae1e9 to 292a98d Compare November 10, 2026 04:23
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 22037

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 22037"

@mitchdenny

Copy link
Copy Markdown
Member Author

Approved for inclusion in release/17.0. Do not merge yet.

@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

@github-actions

This comment has been minimized.

@mitchdenny
Mitch Denny (mitchdenny) force-pushed the mitchdenny-backport-17-0-39-pr21961 branch from 6122bae to a5ae1e9 Compare November 10, 2026 06:31
@mitchdenny
Mitch Denny (mitchdenny) force-pushed the mitchdenny-backport-17-0-39-pr21961 branch from a5ae1e9 to 6122bae Compare November 10, 2026 07:49
@mitchdenny
Mitch Denny (mitchdenny) force-pushed the mitchdenny-backport-17-0-39-pr21961 branch from 6122bae to a5ae1e9 Compare November 10, 2026 08:41
@github-actions

Copy link
Copy Markdown
Contributor

Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants