Repository navigation
fix(contract): a run never draws the same port twice, and a refused bind says what the run holds - #268
Merged
Conversation
…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]>
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.
pick_portcould 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), therc/pull request, about four hours after #224 merged). It used the new picker (ports drawn from 20000-32767) and the server's bind failed withAddrInUseon 31976. #224'sssdump shows what held it: aLISTENsocket on127.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.shdraws the server's port, the mock identity provider's, Postgres's and Mailpit's before any of them listens, solsofcannot 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 on127.0.0.1while the server is refused the same one. The next recurrence will say, because of the diagnostic below.What changed
scripts/lib/ports.sh:pick_portrecords every draw in$PORTS_DRAWN_FILEand 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_rememberrecords the ports the environment gave. The record is a file becausepick_portruns 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 fiveTHRUST_*_PORTvariables 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 thessof 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_rememberrecords and skips an empty argument; andcontract.shremembers everyTHRUST_*_PORTit 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'ssh) and not in dash, which had hidden an oldLSOF_BUSYleak 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 inubuntu:24.04(the runner's shell, with the real/procrange), also on the tree rebased onto the currentmain.scripts/contract.sh native-memory maincell: 71 of 71, with a ports file of two different ports.THRUST_PORT, a stubssbecause 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 inports.shorcontract.sh;test-ports.shhas five moreSC2016/SC2013notes of the kind it already had (single quotes around shell code handed tosh -c).depsjob (compared withci.ymlmechanically; 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:ports_rememberrecords nothingTHRUST_*_PORTleft out ofports_rememberincontract.sh${THRUST_MAIL_HTTP_PORT:-}The first control also fails the same 3 cases under dash.
Not checked
ssof a real runner was seen in44004714491, the extra lines were exercised here with a stubss, anddocker pson a runner has not been run.Refs: #93
🤖 Generated with Claude Code