Skip to content

fix(ci): contract cells and the mutants job fail intermittently with exit 4 (the server under test never comes up) #93

Closed

Description

@aRustyDev

Context

scripts/contract.sh exits 4 when the server under test never comes up: "target did not become healthy" or "target process exited early" (contract.sh:221 and :226). Contract cells and the mutants job then fail intermittently. Because gate requires every job, one of these turns the required check red, on main and on pull requests, for a reason that says nothing about the code.

scripts/mutants.sh runs each mutant as contract.sh ... > "$OUT/$name.out" 2>&1 (:53), so the server log that contract.sh prints on exit 4 goes into a file the job log does not show. Its summary then labels any non-kill SURVIVED or did not run (exit=..) (:33), so a server that never started reads like a surviving mutant, which is the finding the job exists to make.

Evidence

Where Job What was seen
push to main, b1c9d1f (run 40258128717) mutants native-memory engine + case-sensitive-search SURVIVED or did not run (exit=4, failed tests=)
#86, first run (42158481836) mutants native-memory main + ignore-revoked ... (exit=4, failed tests=)
#86, first run contract (worker-cf, bindings) Address already in use (127.0.0.1:34322), then target process exited early, exit 4
#88, a throwaway pull request (42164639829) mutants ignore-revoked again, exit=4; the other ten mutants were killed
#88 contract (worker-cf, main) Address already in use (127.0.0.1:43896), exit 4

Of the nine pushes to main from ad07a79 to a206319, four failed (ae7dfa6, c0736a0, 7e4e858, b1c9d1f). For c0736a0, 7e4e858 and b1c9d1f the failing job was mutants ("Every mutant must be KILLED"); I did not inspect ae7dfa6. The last three pushes passed.

It is not a real surviving mutant: rerunning the failed jobs of #86 passed, #87's own run was green, and running the ignore-revoked mutant locally on the same tree gave a healthy server and a kill (49 passed, 5 failed, exit 101).

What is not known

  • Why mutants fails: its log does not show the server's. The worker-cf cells are Address already in use.
  • Two hypotheses, neither tested. (1) pick_port (contract.sh:72-85) picks a random port in 20000-49999 and checks only that nothing is listening on it, at the start of the script. The server binds long afterwards, after a build, so there is a window. (2) That range overlaps Linux's default ephemeral range (32768-60999), where a port can be held as the source port of an outgoing connection, which lsof -sTCP:LISTEN does not see but a bind refuses. macOS's ephemeral range starts at 49152, which would explain why it is never seen locally.
  • ignore-revoked (the fourth mutant to start) has been the failing one on both pull-request runs where mutants failed. That may be chance.

Proposal

  1. Make it diagnosable first: when a mutant is not killed, print the tail of $OUT/$name.out (the server log) into the job log, and upload target/mutants on failure.
  2. Fix the port choice: take it from below the ephemeral range, or bind port 0 and read the port back, or retry the server start once on address already in use with a fresh port. That is a retry of the start, not of a test; AGENTS.md rule 2 forbids retrying tests, and this must not weaken it.
  3. Separate "did not run" from "survived" in the mutants summary. An exit-4 mutant is not evidence that an assertion is vacuous, and a real survivor must stay unmistakable.

Acceptance criteria

  • A mutant that is not killed shows its server log in the CI job log.
  • The mutants summary reports a mutant whose server never started differently from one that survived, and a planted unkillable mutant still reports SURVIVED (rule 8's control).
  • flake-hunt.yml on the affected cells (worker-cf main, worker-cf bindings), and a repeated run of mutants, show no exit-4 failures over a number of runs agreed in this issue (30, say).
  • If hypothesis 1 or 2 is confirmed or ruled out, the finding is recorded here.

References

#86 (first run), #87, #88; scripts/contract.sh, scripts/mutants.sh; .github/workflows/flake-hunt.yml, contract-cell.yml; AGENTS.md rules 2 and 8.

Activity

  1. added
    bug
    area:process
    on Nov 6, 2026
  2. aRustyDev commented on Nov 10, 2026

    @aRustyDev
    OwnerAuthor

    One more occurrence, and this one is the guard line, not a mutant row.

    #99, first run (run 42670075647, attempt 1, head 45bf3d0, 2026-11-09): mutants failed with

    guard: main pass, server off       NOT REFUSED (exit=4)
    

    All ten mutants before it were KILLED. That guard runs the main pass against a server started with the CSRF switch off, and expects contract.sh to refuse it (exit 5, the target guard); exit 4 is the server never coming up, so there was nothing to refuse and the line reads as a guard that does not work. gate was red with it. Re-running the failed jobs passed (mutants in 3m16s) on the same tree.

    Same observation as above: CI uploads no server log for mutants, so the cause of this one is unseen too. It also shows the issue's point about labels from another side: a server that never started reads as a surviving mutant in the other rows, and here as a guard that did not refuse.

  3. aRustyDev commented on Nov 16, 2026

    @aRustyDev
    OwnerAuthor

    Hypothesis 2 is confirmed, and #224 (merged as a243dcf) fixes it.

    What the failures were

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

    It also failed on pull requests, for example 43814088649 on #216. In every one of those jobs, the show_why output of the row that was not killed shows the target's own bind being refused:

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

    The scheduled run also had could not start the mail server, which fits the same cause: a docker run -p on a port taken the same way.

    The mechanism, reproduced

    pick_port drew from 20000-49999. It accepted a port when lsof -sTCP:LISTEN showed nothing on it. 57% of that range is inside Linux's outgoing-connection range, ip_local_port_range, which is 32768-60999 by default.

    A client connection that closes first keeps its local port in TIME_WAIT for 60 s. lsof -sTCP:LISTEN does not list that socket. The client did not set SO_REUSEADDR, so a later bind to that port fails with EADDRINUSE even with SO_REUSEADDR set, which is what tokio's TcpListener::bind does. Nothing is listening on the port at that point.

    I reproduced this in ubuntu:24.04 (range 32768-60999). The harness opened and closed 3000 client connections against a local listener, closing from the client side, then:

    check result
    200 sampled TIME_WAIT ports: lsof -sTCP:LISTEN calls them free 200/200
    the same 200 ports: a SO_REUSEADDR bind is refused (EADDRINUSE) 200/200
    control, 200 ports in 20000-32767: refused 0/200
    old pick_port, 1000 draws under repeated churn, then a bind 187 refused (543 draws inside the outgoing range)
    new pick_port, 1000 draws interleaved with those, then a bind 0 refused (0 inside the range)

    A mutants job starts the server about 45 times, alongside thousands of short connections from the suite, the health-check curls and cargo, so it builds up exactly this state. The build that runs between the pick and the bind widens the window. macOS starts its range at 49152, which is why local runs almost never collided.

    Hypothesis 1, the window between the pick and the bind, is part of the same story. It is only harmful because the drawn port can be held by a client socket that lsof cannot see.

    The fix (#224)

    • scripts/lib/ports.sh draws from 20000 up to just below the kernel's outgoing range: 20000-32767 on Linux and 20000-49151 on macOS.
      • It reads the range from procfs. It uses awk to do so: dash's read gets a single byte from procfs.
      • On macOS it reads the range with sysctl.
      • It refuses to draw when it cannot read the range, or when fewer than 1000 ports are left below it.
    • contract.sh uses it for every port: the target, the mock IdP, Postgres, Mailpit and the delay proxy. It also:
      • stops with exit 3 when a draw fails;
      • prints the range at the start of each run;
      • on a refused bind, prints ss for the port;
      • on a failed docker run, prints Docker's own error.
    • .github/scripts/tests/test-ports.sh runs in ci-scripts. 13 hand mutants of the library and 3 of the wiring each fail it under dash.
    • Recorded as D-20 in docs/src/testing/status.md.

    On the runner, the native-memory main cell now prints ports drawn from 20000-32767 (the kernel's outgoing range starts at 32768).

    Runs after the fix

    On #224's run 43944208486, I reran only the mutants job, 8 times in a row:

    attempt conclusion rows killed survived AddrInUse
    1 (the PR's own run) success all 0 0
    2 to 9 (reruns) success, each 149 each 0 0

    That is 9 clean jobs and about 400 server starts. At the rate before the fix, roughly 1 start in 90, a run that long with no failure would happen by chance about 1 time in 100.

    Against the acceptance criteria

    🤖 Generated with Claude Code

  4. aRustyDev commented on Nov 16, 2026

    @aRustyDev
    OwnerAuthor

    Both flake-hunt runs were clean after #224: 30 iterations each, on main at 3beb018, which contains a243dcf.

    run verdict iterations that printed ports drawn from 20000-32767 logs with AddrInUse / Address already in use tests per iteration
    worker-cf main, 43968248519 failures: 0 / 30 30/30 0 71/71 passed, every time
    worker-cf bindings, 43968253482 failures: 0 / 30 30/30 0 5/5 passed, every time

    The counts come from each run's artifact (flake-hunt-worker-cf-<pass>, all 30 iteration logs), not from the job's status alone, so every iteration reached its tests. All iterations of a hunt share one runner, which makes the hunt harder on ports than a fresh CI cell: closed connections pile up in TIME_WAIT across iterations, as they did in mutants.

    Acceptance criteria now:

    • A mutant that is not killed shows its server log in the job log (show_why; fix(contract): draw test ports from below the kernel's outgoing-connection range #224 adds ss and Docker's error).
    • The mutants summary still reports a row whose server never started as SURVIVED or did not run. This is the one item left open.
    • flake-hunt on worker-cf main and worker-cf bindings (above), and repeated mutants runs (9 clean jobs, earlier comment): no exit-4 failures.
    • Hypothesis 2 confirmed and recorded (earlier comment and D-20).

    🤖 Generated with Claude Code

  5. aRustyDev commented on Nov 17, 2026

    @aRustyDev
    OwnerAuthor

    Closing: every acceptance criterion is met. The port collisions are fixed by #224 (cause shown and recorded as D-20, then 9 clean mutants jobs and two clean 30-iteration flake hunts), and #248 (57f06d4) reports a row that did not run as DID NOT RUN, separately from a real SURVIVED (D-21).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:processbugprio:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions