Skip to content

fix(contract): draw test ports from below the kernel's outgoing-connection range - #224

Merged
aRustyDev merged 1 commit into
mainfrom
fix/pick-port-below-ephemeral
Nov 16, 2026
Merged

aRustyDev merged 1 commit into
mainfrom
fix/pick-port-below-ephemeral

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

Why

mutants failed on 3 of the last 4 full ci runs on main:

  • the scheduled run 43798891735;
  • the pushes for 962ecf3 (43811916263) and a7560b2 (43901279568).

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:

thread 'main' panicked at crates/server/src/main.rs:223: bind: Os { code: 98, kind: AddrInUse }
target process exited early

pick_port drew from 20000-49999 and checked only lsof -sTCP:LISTEN. 57% of that range is inside Linux's
outgoing-connection range (32768-60999). A closed client connection keeps its port there in TIME_WAIT, lsof does not
see 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)

result
200 TIME_WAIT ports: lsof -sTCP:LISTEN says free 200/200
the same 200: a SO_REUSEADDR bind (what tokio does) is refused 200/200
control: 200 ports in 20000-32767 refused 0/200
old pick_port, 1000 draws under the same churn, refused 187 (543 inside the range)
new pick_port, 1000 draws interleaved with those, refused 0 (0 inside the range)

What changes

  • scripts/lib/ports.sh draws from 20000 up to just below the kernel's range: 20000-32767 on Linux and 20000-49151
    on macOS.
    • It reads the range from procfs. It uses awk for that: under dash, read gets one byte from procfs, which I measured.
      On macOS it reads sysctl.
    • It refuses rather than guesses when the range cannot be read, or when fewer than 1000 ports are left.
  • scripts/contract.sh uses the library for every port: the target, the mock IdP, Postgres, Mailpit and the delay
    proxy. 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 dev and mock_idp.rs.
    • When a draw fails, it stops with exit 3. Before this, an empty port went on to start a server.
    • It prints ports drawn from … at the start of each run, so the runner's real range shows in every log.
    • On a refused bind, it prints ss for the port.
    • When Postgres or Mailpit cannot start, it prints Docker's own error.
  • .github/scripts/tests/test-ports.sh runs in ci-scripts. It covers:
    • a matrix of range starts with 300 draws each, which must stay in bounds and reach both the bottom and the top quarter;
    • ranges that cannot be read;
    • a stubbed lsof;
    • this machine's real range;
    • contract.sh's wiring.
    • All 13 hand mutants of the library fail it under dash: wrong bounds, no cap, no refusal, no lsof, a hard-coded
      start, the procfs read, the old range, and others. So do 3 mutants of the wiring.
  • plan-ci.py names scripts/lib/ports.sh in the harness rule, so it runs everything. The fallback for an unknown
    scripts/ file already did that, so this rule only names the file.
  • Docs: testing status D-20, and one line in the cell bullet of cicd.md.

Verification

  • All 41 ci-scripts and deps commands pass under dash.
  • test_plan_ci.py: 5014 checks pass.
  • Locally, scripts/contract.sh native-memory main passes, and the ignore-revoked mutant 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 mutants summary
that tells "did not run" apart from "survived", is not in this PR.

🤖 Generated with Claude Code

…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]>
@aRustyDev
aRustyDev merged commit a243dcf into main Nov 16, 2026
326 checks passed
@aRustyDev
aRustyDev deleted the fix/pick-port-below-ephemeral branch November 16, 2026 22:11
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]>
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.

1 participant