Repository navigation
test(router): make TestUseEphemeralPort resilient to host port races, fixes #8293 (#8638) [skip ci] - #8638
Merged
Conversation
## 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]>
|
Download the artifacts for this pull request:
See Testing a PR. |
rfay
marked this pull request as ready for review
July 31, 2026 14:30
rfay
added a commit
to weitzman/ddev
that referenced
this pull request
Jul 31, 2026
…ixes ddev#8293 (ddev#8638) [skip ci] Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Member
Author
|
I guess this didn't do the job :( |
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.
The Issue
Seen as intermittent CI failures in multiple environments, including GitHub Actions and Buildkite, for example this run:
There are two separate sources of flakiness here:
The failure above is at
app.Start(), on a host port DDEV never chose. The db service publishes{{ .DockerIP }}:$DDEV_HOST_DB_PORT:{{ .DBPort }}andhost_db_portis unset in this test, so it renders as127.0.0.1::3306and 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.The port assertions predicted exact port numbers as
MinEphemeralPort + i*4with a+2margin, 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()retriesapp.Start()up to 3 times with a 2s backoff, but only foraddress already in use/port is already allocated. Every other error still fails immediately.It deliberately does not call
app.Stop()between attempts.Stop()callsclear(EphemeralRouterPortsAssigned), and with that map clearedAllocateAvailablePortForRoutertakes 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:
[MinEphemeralPort, MaxEphemeralPort]assignedPortsmap that names the claimant so a collision reports which project took the portDistinctness is kept rather than dropped because
AllocateAvailablePortForRoutertracksEphemeralRouterPortsAssignedspecifically 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
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 staticrequiredreports 0 issues.Release/Deployment Notes
None. Test-only, no user-visible or runtime behavior change.
🤖 Generated with Claude Code