Skip to content

Test helpers pick a loopback port, release it, and bind it later: any process in a parallel lane can take it first #1395

Closed

Description

@V3RON

14 files under src/ define a freeLoopbackPort that listens on port 0, closes the server and returns the number (e.g. src/daemon/test-relay-support.ts:30, src/agent-device/test-support.ts:139, src/daemon/input-lease.e2e.test.ts:17). The caller binds the number later. The unit lane runs files in parallel processes (vitest.config.ts), so another file's listen(0) can draw the same port in that window and the bind fails with EADDRINUSE. No failure seen yet outside the timing lane; #1394's cause is the in-process case. Found while triaging #1394.

Written by an agent.

Activity

  1. added
    bug:new
    on Nov 6, 2026
  2. self-assigned this
    on Nov 6, 2026
  3. added a commit that references this issue on Nov 6, 2026
  4. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    Reproduction

    Confirmed. Test on bug/1395-repro: src/agent-device/rig-ports.e2e.test.ts, "start their daemon on another port when another listener takes the port they picked before the daemon binds it". It covers the two shared rigs (startFleet, the agent-device setUp). The stand-in for another process takes the port the moment the rig's listener closes.

    pnpm exec vitest run --project unit src/agent-device/rig-ports.e2e.test.ts
    -   "agent-device setUp": "started",
    -   "startFleet": "started",
    +   "agent-device setUp": "EADDRINUSE",
    +   "startFleet": "EADDRINUSE",
     ❯ src/agent-device/rig-ports.e2e.test.ts:77:22
    

    Root cause

    Defect. freeLoopbackPort (src/daemon/test-relay-support.ts:30-37, and the 13 copies the issue names) closes its listener before it returns the port. The daemon binds that port only after its socket claim (src/daemon/worker-composition.ts:612, a gateway at main.ts:613). If the port is taken in that window, the start rejects with EADDRINUSE (worker-composition.ts:692, main.ts:616), and no rig retries. The e2e lane already retries this case (withHttpDaemon, e2e/helpers/http-daemon.ts:13, #560). The in-process rigs never got that retry.

    Simplest fix

    Add one src/test-support/loopback-port.ts with freeLoopbackPort and startOnFreePort(start). startOnFreePort picks a new port on EADDRINUSE, up to 5 tries. Wrap each of the 14 rigs' daemon start in it and delete the copies. Tried on the two shared rigs only:

    repro + relay-gateway, worker-capabilities, 6 agent-device edge files: 9 files, 344 passed
    relay-promises.timing.test.ts: 8/8
    

    Alternatives rejected

    • Let the daemon bind port 0 and report the port it got: http.port refuses 0, and the local profile and the advertised URLs read the configured port.
    • A fixed port range per vitest worker: two worktrees running the lane at once collide.

    Risk

    Fast track

    • Defect: yes
    • Fix tried: no — tried on 2 of the 14 rigs
    • Nothing public added or broken: yes
    • No ADR or rule changed: yes
    • One fix: yes

    Side findings

    Written by an agent.

  5. removed their assignment
    on Nov 6, 2026
  6. added
    bug:ready
    and removed
    bug:new
    on Nov 6, 2026
  7. self-assigned this
    on Nov 6, 2026
  8. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    Added scope (maintainer, 2026-11-06)

    The same released-port bug in e2e/, from #1397 (27 flows in 8 files). Triage report and repro test: #1397's comment, test e2e/picked-ports.test.ts on branch bug/1397-repro.

    • e2e/helpers/port.ts:21 freeLoopbackPort and three local reservePort copies (e2e/http-api.test.ts:15, e2e/daemon-log.test.ts:47, e2e/device-exec.test.ts:23) release the port before a daemon in another process binds it. Only withHttpDaemon (e2e/helpers/http-daemon.ts:13) retries.
    • Switch to withHttpDaemon: lease-limit-per-requester:91, usage-stats:125, physical-leasing:699, http-api (60, 216, 315, 348, 420, 469, 512), daemon-log:95, device-exec:83. Delete the reservePort copies.
    • daemon-folders and daemon-lifecycle:79 start the gateway inside the test: wrap that step in the same retry as startOnFreePort. One helper for src/ and e2e/, not two.
    • Check too: e2e/console/fixtures.ts (221, 596, 643), e2e/helpers/env.test.ts:114, e2e/update/update.e2e.test.ts:22.
    • The src/ callers that still pass a released port to startDaemon through the re-export in src/daemon/test-relay-support.ts:21 also start through startOnFreePort: src/daemon/test-main.ts (36, 185), main-stop.test.ts (95, 485, 520, 605, 607, 684, 720), main.test.ts (1445, 1470, 1652, 1680, 1764), main-update.test.ts (52, 401), main-host-load.test.ts:308, main-agent-device.test.ts:396.

    Done when (added)

    • e2e/picked-ports.test.ts passes: every flow "passed" with the port taker running.
    • The e2e lane stays green.

    Written by an agent.

  9. V3RON commented on Nov 6, 2026

    @V3RON
    ContributorAuthor

    Delivery run

    Merged: #1405 (#1395)
    Asleep: 39 min

    Written by an agent.

  10. added a commit that references this issue on Nov 10, 2026
    0ebd818
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions