Repository navigation
fix(contract): draw test ports from below the kernel's outgoing-connection range - #224
Merged
Merged
Conversation
…ction range The `mutants` job, and now and then a contract cell, failed with exit 4: the target's own bind was refused with AddrInUse (os error 98) on the port pick_port had chosen. pick_port drew from 20000-49999 and only asked lsof whether something LISTENED there. 57% of that range is inside Linux's outgoing-connection range (32768-60999), where a closed client connection keeps its port in TIME_WAIT. lsof -sTCP:LISTEN does not see that socket, and since the client set no SO_REUSEADDR, the server's bind fails although nothing listens on the port. Shown in ubuntu:24.04 after 3000 client-closed connections: - 200 sampled TIME_WAIT ports: lsof called all 200 free, and a SO_REUSEADDR bind (what tokio does) refused all 200. - 200 ports below the range: 0 refused. - Under the same churn, the old picker's 1000 draws were refused 187 times and the new one's 0 times. scripts/lib/ports.sh now draws from 20000 to just below the kernel's range, read from procfs (with awk: dash's `read` gets one byte from procfs) or from sysctl on macOS. It refuses when it cannot read the range or when fewer than 1000 ports are left. contract.sh uses it for every port and stops with exit 3 when a draw fails; before this, an empty port went on to start a server. It also prints the range at the start of each run, and on a refused bind it prints `ss` for the port. When the Postgres or Mailpit container cannot start, it now prints Docker's own error. test-ports.sh runs in ci-scripts. 13 hand mutants of the library and 3 of contract.sh's wiring all fail it. Refs: #93 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 16, 2026
Merged
aRustyDev
added a commit
that referenced
this pull request
Nov 17, 2026
…ind says what the run holds (#268) `pick_port` asked only whether something listened on a port at the moment it drew. `contract.sh` draws the server's port, the mock identity provider's, Postgres's and Mailpit's before any of them listens, so the check could not see the first when it drew the second, and the picker could hand out the same port twice. The service that listens first then holds the port the other is refused (`bind: AddrInUse`, exit 4). Shown on the real library over its narrowest range, 400 draws with nothing bound in between: 324 different ports, 66 handed out twice. - `scripts/lib/ports.sh`: `pick_port` records every draw in `$PORTS_DRAWN_FILE` and never returns a recorded port; it refuses when there is no file, or one it cannot write, because a picker that forgets what it drew is the defect. `ports_remember` records the ports the environment gave. - `scripts/contract.sh`: the run's file is `$LOG.ports` (kept with the run's logs), the five `THRUST_*_PORT` it reads are remembered, and a refused bind prints the ports of the run in order and by name, and the ports the containers publish, beside the `ss` of the port, so that a refusal that is not a draw twice names its holder. - `.github/scripts/tests/test-ports.sh`: 600 draws over the narrowest room are 600 different ports; the file holds the draws in order; remembered ports are never drawn and all of them is a refusal; no file and an unwritable file are refusals; `contract.sh` remembers every `THRUST_*_PORT` it reads. The knobs are set in subshells, because a prefix assignment before a function persists in bash's POSIX mode and not in dash. - `docs/src/testing/status.md` D-20 says the second way and what is not established. Not established: that this was the cause of the one refused bind after #224 (run 44004714491, `native-pg main`, a `LISTEN` socket on the port with the Postgres container up). The run did not record its ports. At one draw in 12768 per pair it is a rare cause; the next recurrence prints what the run held. Controls, each a broken copy of the library that the test must fail: no memory check (3 cases fail), no append (3), no refusal without a file (1), an append whose failure is ignored (1), a `ports_remember` that records nothing (1); and `contract.sh` with one `THRUST_*_PORT` left out of the `ports_remember` call (1, which names the variable). A real `native-memory main` cell is 71 of 71 with two different ports in its file, and a bind refused on purpose (a listener on `THRUST_PORT`) ends with exit 4 and the diagnostic. Refs: #93 Co-authored-by: Claude Sonnet 5.5 <[email protected]>
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.
Why
mutantsfailed on 3 of the last 4 fullciruns onmain:It also failed on pull requests, for example #216's run 43814088649. Every one of those failures has the same cause. A row
reports
SURVIVED or did not run (exit=4), and its output shows the target's own bind failing:pick_portdrew from 20000-49999 and checked onlylsof -sTCP:LISTEN. 57% of that range is inside Linux'soutgoing-connection range (32768-60999). A closed client connection keeps its port there in TIME_WAIT,
lsofdoes notsee it, and since the client set no SO_REUSEADDR, the server's bind is refused although nothing listens on the port.
Shown, not assumed (
ubuntu:24.04, 3000 client-closed connections)lsof -sTCP:LISTENsays freepick_port, 1000 draws under the same churn, refusedpick_port, 1000 draws interleaved with those, refusedWhat changes
scripts/lib/ports.shdraws from 20000 up to just below the kernel's range: 20000-32767 on Linux and 20000-49151on macOS.
readgets one byte from procfs, which I measured.On macOS it reads
sysctl.scripts/contract.shuses the library for every port: the target, the mock IdP, Postgres, Mailpit and the delayproxy. These are the same collisions fix(ci): contract cells and the mutants job fail intermittently with exit 4 (the server under test never comes up) #93 lists for
wrangler devandmock_idp.rs.ports drawn from …at the start of each run, so the runner's real range shows in every log.ssfor the port..github/scripts/tests/test-ports.shruns inci-scripts. It covers:lsof;contract.sh's wiring.lsof, a hard-codedstart, the procfs
read, the old range, and others. So do 3 mutants of the wiring.plan-ci.pynamesscripts/lib/ports.shin the harness rule, so it runs everything. The fallback for an unknownscripts/file already did that, so this rule only names the file.cicd.md.Verification
ci-scriptsanddepscommands pass under dash.test_plan_ci.py: 5014 checks pass.scripts/contract.sh native-memory mainpasses, and theignore-revokedmutant is killed with exit 101.Refs #93. This PR does not close it: #93 asks for a run count that the owner picks. Its other item, a
mutantssummarythat tells "did not run" apart from "survived", is not in this PR.
🤖 Generated with Claude Code