Skip to content

test(router): make TestUseEphemeralPort resilient to host port races, fixes #8293 (#8638) [skip ci] - #8638

Merged
rfay merged 1 commit into
ddev:mainfrom
rfay:20260730_rfay_resilient_test_ephemeral
Jul 31, 2026
Merged

rfay merged 1 commit into
ddev:mainfrom
rfay:20260730_rfay_resilient_test_ephemeral

Conversation

@rfay

@rfay rfay commented Jul 30, 2026 •

Copy link
Copy Markdown
Member

The Issue

Seen as intermittent CI failures in multiple environments, including GitHub Actions and Buildkite, for example this run:

router_test.go:259:
    Error: Received unexpected error:
    Error response from daemon: failed to set up container networking:
    driver failed programming external connectivity on endpoint
    ddev-TestUseEphemeralPortsite2...-db: error while calling RootlessKit
    PortManager.AddPort(): listen tcp4 127.0.0.1:33336: bind: address
    already in use

There are two separate sources of flakiness here:

  1. The failure above is at app.Start(), on a host port DDEV never chose. The db service publishes {{ .DockerIP }}:$DDEV_HOST_DB_PORT:{{ .DBPort }} and host_db_port is unset in this test, so it renders as 127.0.0.1::3306 and Docker draws the host port from the kernel local range (32768-60999 on Linux). Docker does not reserve its pick, and under rootless Docker the bind happens later still in RootlessKit's port manager, so the port can be held by an unrelated socket or by a just-removed container that has not released it yet. That range overlaps DDEV's 33000-35000 ephemeral router range, which is why the port number looks like a DDEV allocation but is not one.

  2. The port assertions predicted exact port numbers as MinEphemeralPort + i*4 with a +2 margin, which hardcodes both "each project consumes exactly 4 ports" and "nothing else on the machine holds a port in 33000-35000". The original comment already called this fragile.

Loosening the assertions alone would not have fixed the linked run, and retrying alone would not fix (2) — both changes are needed, for different flakes.

How This PR Solves The Issue

For (1), startRetryingHostPortRace() retries app.Start() up to 3 times with a 2s backoff, but only for address already in use / port is already allocated. Every other error still fails immediately.

It deliberately does not call app.Stop() between attempts. Stop() calls clear(EphemeralRouterPortsAssigned), and with that map cleared AllocateAvailablePortForRouter takes its "already bound by router, reusing it" branch and can hand the second project a port already serving the first — which would break the distinctness check below in a way that looks like a product bug but is not.

For (2), the test now asserts what actually matters rather than predicting port numbers:

  • the project is pushed off the occupied configured ports (unchanged)
  • each port falls within [MinEphemeralPort, MaxEphemeralPort]
  • each port is distinct from every port already handed out, tracked in an assignedPorts map that names the claimant so a collision reports which project took the port
  • the port actually works, via the existing HTTP/HTTPS content checks, which were always the assertion carrying the real weight

Distinctness is kept rather than dropped because AllocateAvailablePortForRouter tracks EphemeralRouterPortsAssigned specifically to avoid reissuing a port, so it is intended behavior. Since Traefik routes by Host header, two projects sharing a router port would still serve correct content, so the content checks alone would not catch that regression.

No non-test code changes.

Manual Testing Instructions

DDEV_NO_INSTRUMENTATION=true go test -v -timeout 30m \
  -run 'TestUseEphemeralPort$' ./pkg/ddevapp/

To exercise the retry path, hold a port in the range Docker will pick from while the test starts a project, or run the test repeatedly on rootless Docker where the collision occurs naturally.

Automated Testing Overview

This is a test-only change to an existing test. The test passed locally (--- PASS: TestUseEphemeralPort (43.43s)) with both projects exercising the new range and distinctness assertions.

Note that the local run warned mkcert CA files are not readable from mkcert -CAROOT, so the HTTPS content assertions were likely skipped locally; CI has a working CAROOT.

make staticrequired reports 0 issues.

Release/Deployment Notes

None. Test-only, no user-visible or runtime behavior change.

🤖 Generated with Claude Code

## The Issue

No filed issue; seen as intermittent CI failures in multiple environments,
including GitHub Actions and Buildkite, for example
https://github.com/ddev/ddev/actions/runs/30582029761/job/91004397529

    router_test.go:259:
        Error: Received unexpected error:
        Error response from daemon: failed to set up container networking:
        driver failed programming external connectivity on endpoint
        ddev-TestUseEphemeralPortsite2...-db: error while calling RootlessKit
        PortManager.AddPort(): listen tcp4 127.0.0.1:33336: bind: address
        already in use

Two separate sources of flakiness:

1. The failure above is at `app.Start()`, on a host port DDEV never chose.
   The db service publishes `{{ .DockerIP }}:$DDEV_HOST_DB_PORT:{{ .DBPort }}`
   and `host_db_port` is unset in this test, so it renders as
   `127.0.0.1::3306` and Docker draws the host port from the kernel local
   range (32768-60999 on Linux). Docker does not reserve its pick, and under
   rootless Docker the bind happens later still in RootlessKit's port manager,
   so the port can be held by an unrelated socket or by a just-removed
   container that has not released it yet. That range overlaps DDEV's
   33000-35000 ephemeral router range, which is why the port number looks
   like a DDEV allocation but is not one.

2. The port assertions predicted exact port numbers as
   `MinEphemeralPort + i*4` with a `+2` margin, which hardcodes both "each
   project consumes exactly 4 ports" and "nothing else on the machine holds a
   port in 33000-35000". The original comment already called this fragile.

## How This PR Solves The Issue

For (1), `startRetryingHostPortRace()` retries `app.Start()` up to 3 times
with a 2s backoff, but only for `address already in use` / `port is already
allocated`. Every other error still fails immediately.

It deliberately does not call `app.Stop()` between attempts. `Stop()` calls
`clear(EphemeralRouterPortsAssigned)`, and with that map cleared
`AllocateAvailablePortForRouter` takes its "already bound by router, reusing
it" branch and can hand the second project a port already serving the first,
which would break the distinctness check below in a way that looks like a
product bug but is not.

For (2), the test now asserts what actually matters rather than predicting
port numbers:

- the project is pushed off the occupied configured ports (unchanged)
- each port falls within `[MinEphemeralPort, MaxEphemeralPort]`
- each port is distinct from every port already handed out, tracked in an
  `assignedPorts` map that names the claimant so a collision reports which
  project took the port
- the port actually works, via the existing HTTP/HTTPS content checks, which
  were always the assertion carrying the real weight

Distinctness is kept rather than dropped because
`AllocateAvailablePortForRouter` tracks `EphemeralRouterPortsAssigned`
specifically to avoid reissuing a port, so it is intended behavior. Since
Traefik routes by Host header, two projects sharing a router port would still
serve correct content, so the content checks alone would not catch that
regression.

No non-test code changes.

## Manual Testing Instructions

```bash
DDEV_NO_INSTRUMENTATION=true go test -v -timeout 30m \
  -run 'TestUseEphemeralPort$' ./pkg/ddevapp/
```

To exercise the retry path, hold a port in the range Docker will pick from
while the test starts a project, or run the test repeatedly on rootless
Docker where the collision occurs naturally.

## Automated Testing Overview

This is a test-only change to an existing test. The test passed locally
(`--- PASS: TestUseEphemeralPort (43.43s)`) with both projects exercising the
new range and distinctness assertions. Note that the local run warned
`mkcert CA files are not readable from mkcert -CAROOT`, so the HTTPS content
assertions were likely skipped locally; CI has a working CAROOT.

`make staticrequired` reports 0 issues.

## Release/Deployment Notes

None. Test-only, no user-visible or runtime behavior change.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown

@stasadev stasadev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

It probably fixes this issue:

Since I can't see the surrounding code in #8293, I can't tell which tests failed there, so let's have this PR close that issue.

@stasadev stasadev changed the title test(router): make TestUseEphemeralPort resilient to host port races test(router): make TestUseEphemeralPort resilient to host port races, fixes #8293 Jul 31, 2026
@rfay
rfay marked this pull request as ready for review July 31, 2026 14:30
@rfay
rfay requested a review from a team as a code owner July 31, 2026 14:30
@rfay rfay changed the title test(router): make TestUseEphemeralPort resilient to host port races, fixes #8293 test(router): make TestUseEphemeralPort resilient to host port races, fixes #8293 (#8638) [skip ci] Jul 31, 2026
@rfay
rfay merged commit e5b89b2 into ddev:main Jul 31, 2026
54 checks passed
@rfay
rfay deleted the 20260730_rfay_resilient_test_ephemeral branch July 31, 2026 14:34
rfay added a commit to weitzman/ddev that referenced this pull request Jul 31, 2026
@rfay

rfay commented Aug 1, 2026

Copy link
Copy Markdown
Member Author

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.

Add retry for "bind: address already in use" error in rootless

2 participants