Skip to content

test(conformance): retry a launch only when a stranger owns its port - #247

Merged
Antruly merged 1 commit into
mainfrom
fix/246-launch-port-takeover
Oct 18, 2026
Merged

Antruly merged 1 commit into
mainfrom
fix/246-launch-port-takeover

Conversation

@Antruly

@Antruly Antruly commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

🤖 Antruly-dev-1 · AI Engineer 1 · agent:dev-ai-1

What this changes

The launch conformance cases reserve a port with freePort(), which holds it only for the instant between the check and the fixture's own bind. A worker elsewhere can take the number in that window; the fixture then prints Error: listen EADDRINUSE … and exits, while the readiness probe is answered by the stranger — every conformance fixture serves /healthz. The case observes ready: true with still_running: false and fails, even though uvp_run behaved correctly (#246, observed on #245's attempt 1: 141 ms, no diff in the launching code).

The change follows the issue's second option — a bounded, evidence-gated retry — with the gate made testable:

  • launchWasOvertaken(run, portStillAnswers) is the decision, as a pure predicate: the ambiguous pair alone (ready: true, still_running: false) is not evidence. It also requires either the child's own EADDRINUSE in stderr_tail, or a probe showing a listener answering on the port right now. A service that answered and then died leaves the same pair, and that is a real defect — it must fail, not be retried.
  • launchOnReservedPort(launch) re-reserves the port and retries at most 3 times, only when the gate says yes. When all attempts are overtaken it throws with a message saying the case never launched its own listener, rather than asserting a handler defect.
  • The three real launch cases ("launches an absolute binary where it is", "launches a real process, waits for a real probe, then stops it", "keeps a run it did not detach in this session's registry") go through the helper. expectConforms("uvp_run", run) stays at each call site so a retried run is still checked against the contract.

Nothing in src/ changes; the handler is not altered. This does not establish that the takeover was the cause of any other failure: it makes that collision distinguishable from a defect and stops the collision from reddening an unrelated PR.

Addresses #246.

Tests

New tests in tests/conformance/handlers.test.ts:

  • a table over the gate's eight combinations (collision vs silent death × answers/undefined/false, plus ready: false and still_running: true);
  • a synthetic ambiguous launch that no evidence shows was overtaken, asserting the helper returns it after one attempt (the input is the gate's decision, not the handler);
  • a forced collision: a stranger listener owns the reserved port on attempt 1, the launch succeeds on attempt 2, and the run that survived is stopped.

Local gate

Run in /tmp/wt246 at 083c7bc, the tree pushed here (Linux host; LIBUVCPP_ROOT=/home/zwy/下载/libuvcpp-1.7.0-linux-x64, PKG_CONFIG_PATH=$LIBUVCPP_ROOT/lib/pkgconfig, LD_LIBRARY_PATH=$LIBUVCPP_ROOT/lib for the second run):

  • npm ci, npm run compile — exit 0
  • npm run test:local -- tests/conformance/handlers.test.ts — 94 passed
  • npx tsc --noEmit -p tsconfig.json — exit 0
  • npm run lint — exit 0
  • oxfmt --check — clean
  • npm run knip — exit 0
  • npm run check:pr — exit 0 twice on this tree:
    • without a libuvcpp environment: 13/13 tasks, 1380 passed, 66 skipped (the acceptance lanes skipped by name);
    • with LIBUVCPP_ROOT/PKG_CONFIG_PATH/LD_LIBRARY_PATH set to the 1.7.0 install: 13/13 tasks, 77 files passed / 2 skipped, 1432 passed / 14 skipped, doc facts … libuvcpp 1.7.0 … consistent, sbom:check and licence check up to date. The remaining skips name their reasons: the native probe is not built here (npm run build:native-probe against a CAPI-enabled libuvcpp — the two HTTP/3 lanes), the published-package lane is opt-in, and three tests are Windows-only.
  • mutation probes (source restored and md5-verified after each, c63fd8a2508a1e6645b0c23a9ead10ee): removing ready === true / still_running !== true / the EADDRINUSE arm / the portAnswers arm from the gate, removing the retry branch's !launchWasOvertaken return, and changing the attempt bound each turn at least one test red — all six killed.

Limits

  • The retry is a test-side accommodation for a race the test cannot close; freePort()'s reservation window is unchanged, and other suites that reserve-then-launch have the same window.
  • A collision where the stranger does not answer the probe (for example, no listener by the time the probe resolves) and our child dies without EADDRINUSE reaching stderr_tail would still fail — no evidence, no retry.
  • The forced-collision test makes the stranger answer after 1 s so the fixture's exit is observable before the probe resolves; it does not exercise a real cross-process takeover.

Never merged without the owner's explicit approval on this PR.

Assisted-by: Claude Code (Antruly-dev-1)

The launch conformance cases reserve a port with `freePort()` and launch the
fixture on it. The reservation holds only for an instant: another worker can
bind the number first, our fixture dies of `EADDRINUSE`, and the readiness
probe is answered by the stranger — so the case reads `ready: true` with
`still_running: false` and fails although `uvp_run` behaved correctly (#246).

`launchOnReservedPort` re-reserves and retries at most three times, gated on
evidence: either the child's own `EADDRINUSE` in `stderr_tail`, or a probe
showing a listener answering right now. The ambiguous pair alone is not
evidence — a service that answered and then died leaves the same pair, and
that is a defect the case must still fail on. The gate is a pure predicate
(`launchWasOvertaken`) with a table over its combinations; a forced collision
proves the retry end to end, and an exhaustive bound reports a premise that
never held rather than a handler defect.

Addresses #246.

Assisted-by: Claude Code (Antruly-dev-1)
@Antruly

Antruly commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

Review — test(conformance): retry a launch only when a stranger owns its port

Reviewed at head 083c7bc against base f11e57a (the merge of #245). No blocking findings. This addresses #246, which is exactly the failure shape I recorded on #245's first run: ready: true, still_running: false, the child gone, and a stranger answering /healthz. The diagnosis in the PR body matches what I saw in that log.

What I verified independently

  • The gate is evidence-gated, not pair-gated. launchWasOvertaken returns false for the ambiguous pair alone, and the helper returns that run after one attempt, so a genuine "answered and then died" defect still fails at the caller's assertion rather than being retried away.
  • The forced collision exercises the real path. I ran the focused file locally: 94 passed, including the forced-collision case (1184 ms). The stranger answers after 1 s so the fixture's EADDRINUSE exit is observable before the probe resolves — which is the ordering that produces the ambiguous pair.
  • Mutation probe. With the portAnswers arm forced false, the table test fails (silentDeath + a listener answering must be retried); restored the file afterwards. The author's six-kill claim is consistent with what I see.
  • The three launch cases keep expectConforms("uvp_run", run) at the call site, and the helper returns the run from the attempt that survived. The port returned to the "real process" case is the successful attempt's.
  • Gates at this head. npx tsc --noEmit clean, oxfmt --check clean, npm run lint with no findings in this file, npm run knip clean. CI: all applicable checks pass.

Non-blocking (my judgement)

  1. The ready === true requirement closes less than the Limits section says. The helper returns early when ready !== true, so a real takeover where the stranger does not answer the probe (readiness times out) is not retried — yet the child's stderr_tail in that case does contain EADDRINUSE. The Limits bullet says "no evidence, no retry", but here there is the strongest evidence there is. Either let the EADDRINUSE arm fire regardless of ready (the pair ready: false, still_running: false, EADDRINUSE is also a correct observation of a bad port), or narrow the sentence to "the stranger answering the probe". This is the residual gap left for the realistic case where the port is taken by a non-fixture listener.
  2. "A listener answers right now" cannot by itself tell a takeover from a still_running defect. If uvp_run were intermittently wrong about still_running while its own child is alive and serving, the port answers and the gate retries; the three-attempt bound means a systematic defect throws rather than passing, but an intermittent one is retried away and the failure message blames the port. The child's own EADDRINUSE is the only unambiguous discriminator; keeping the second arm (it covers a truncated stderr_tail) is reasonable, but the trade-off is worth stating where the gate is defined.
  3. Intermediate overtaken runs are not contract-checked. Only the returned run goes through expectConforms; the failed attempt's shape is never validated. Minor — the collision path runs the same handler — but "a retried run is still checked" is true only of the final one.

None of these changes the decision the helper makes for the collision it was written for, and none touches src/.

@Antruly
Antruly marked this pull request as ready for review October 18, 2026 11:05
@Antruly

Antruly commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

Owner approval. Approved at 083c7bc04f29629c32aec99a3b23b3c71c654f77.

Reviewed in this comment. No blocking findings: the retry is gated on evidence rather than on the ambiguous pair, the bound turns a systematic misobservation into a failure rather than a pass, and the forced-collision test exercises the ordering that produced #246. The three non-blocking notes recorded there (the ready short-circuit vs the Limits wording, what the "listener answers" arm can and cannot distinguish, and intermediate runs not being contract-checked) do not hold the change.

At this head: focused suite 94 passed, tsc --noEmit, oxfmt --check, lint and knip clean. All applicable checks pass. Readiness is declared (Owned by @Antruly; author readiness declared.).

Merging as a merge commit at the exact reviewed head.

Recorded by the owner's review assistant at the owner's explicit instruction.

@Antruly
Antruly merged commit 23485a2 into main Oct 18, 2026
19 of 20 checks passed
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