Skip to content

fix(contract): a run never draws the same port twice, and a refused bind says what the run holds - #268

Merged
aRustyDev merged 1 commit into
mainfrom
fix/pick-port-never-draws-twice
Nov 17, 2026
Merged

aRustyDev merged 1 commit into
mainfrom
fix/pick-port-never-draws-twice

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

pick_port could hand out the same port twice in one run, and when a bind was refused the log did not say what the run itself held. This fixes the first and adds to the second. It follows #224 and #248; #93 is closed and this PR does not reopen it.

Why

After #224 the picker draws below the kernel's outgoing-connection range, so a closed client connection cannot hold a port in TIME_WAIT. One refused bind has been seen since: run 44004714491 (contract (native-pg, main), the rc/ pull request, about four hours after #224 merged). It used the new picker (ports drawn from 20000-32767) and the server's bind failed with AddrInUse on 31976. #224's ss dump shows what held it: a LISTEN socket on 127.0.0.1:31976, not only a TIME_WAIT. So the mechanism of #224 was not the cause of that one.

The picker has a second hole. It asks whether something listens on a port at the moment it draws, and it reserves nothing. contract.sh draws the server's port, the mock identity provider's, Postgres's and Mailpit's before any of them listens, so lsof cannot see the first when the second is drawn, and the same number can come back twice. The service that listens first then holds the port the other is refused. Shown on the real library over its narrowest range (1000 ports), 400 draws with nothing bound in between: 324 different ports, 66 handed out more than once.

Not established: that this was the cause of run 44004714491. The run did not record its ports, and by the arithmetic a collision between two draws is rare (one in 12768 per pair of draws), so one sighting is a thin match. What does fit is the picture: the Postgres container publishing a port on 127.0.0.1 while the server is refused the same one. The next recurrence will say, because of the diagnostic below.

What changed

  • 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. The record is a file because pick_port runs in $( ), a subshell.
  • scripts/contract.sh: the run's file is $LOG.ports, so it is kept with the run's logs and uploaded with them. The five THRUST_*_PORT variables the script reads are remembered. When a bind is refused, the output now also has the ports of the run in order and by name (server, mock identity provider, Postgres, Mailpit, delay proxy) and the ports every container publishes, beside the ss of the port.
  • .github/scripts/tests/test-ports.sh (ci-scripts): 36 cases, 9 of them new: 600 draws over the narrowest room are 600 different ports; the file holds the draws in order; ports the run holds are never drawn, and all of them is a refusal; no file and an unwritable file are refusals; ports_remember records and skips an empty argument; and contract.sh remembers every THRUST_*_PORT it reads (a variable added later and not remembered fails the test, naming it). The knobs are set in subshells: a prefix assignment before a function call persists in bash's POSIX mode (this Mac's sh) and not in dash, which had hidden an old LSOF_BUSY leak behind the new cases.
  • docs/src/testing/status.md: D-20 says the second way, the sighting, and what is not established.

Verification

  • test-ports.sh: 36 of 36 on macOS and 36 of 36 under dash in ubuntu:24.04 (the runner's shell, with the real /proc range), also on the tree rebased onto the current main.
  • A real scripts/contract.sh native-memory main cell: 71 of 71, with a ports file of two different ports.
  • A bind refused on purpose (a listener already on THRUST_PORT, a stub ss because macOS has none): exit 4, and the output has the sockets, the ports of this run, in the order they were drawn or given: 23999 25336, them by name, and the containers' published ports; the given port is the first line of the file.
  • shellcheck: no new finding in ports.sh or contract.sh; test-ports.sh has five more SC2016 / SC2013 notes of the kind it already had (single quotes around shell code handed to sh -c).
  • All 22 steps of the deps job (compared with ci.yml mechanically; the one difference is CI's download of actionlint).

Controls (AGENTS.md rule 8)

Each is a broken copy of the library, or an edit to contract.sh, that the test must fail:

Broken Fails
the check that a port was not drawn removed 3: the 600 draws (127 handed out twice), a held port drawn, all held and a port returned
the append removed 3: the 600 draws (118 twice), the file not holding the draws, an unwritable file and a port returned
the refusal without a file removed 1: no file, and the reason missing
a failed append ignored 1: an unwritable file and a port returned
ports_remember records nothing 1
one THRUST_*_PORT left out of ports_remember in contract.sh 1, naming ${THRUST_MAIL_HTTP_PORT:-}

The first control also fails the same 3 cases under dash.

Not checked

  • Whether the diagnostic prints well on a runner: the ss of a real runner was seen in 44004714491, the extra lines were exercised here with a stub ss, and docker ps on a runner has not been run.
  • The whole CI run.

Refs: #93

🤖 Generated with Claude Code

…ind says what the run holds

`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]>
@aRustyDev
aRustyDev merged commit 6a89771 into main Nov 17, 2026
42 checks passed
@aRustyDev
aRustyDev deleted the fix/pick-port-never-draws-twice branch November 17, 2026 05:45
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