Skip to content

test(web): bind the auto-open server to :0 instead of probing a free port - #2669

Merged
cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2602-auto-open-port-race
Oct 12, 2026
Merged

cliffhall merged 1 commit into
v2/mainfrom
v2/fix/2602-auto-open-port-race

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2602

What changed

server-auto-open.test.ts used to call findFreePort(): it bound :0, read the port, closed the probe socket, and passed the number to startHonoServer, which bound it later. On a loaded machine another process could take the port in between, and server.ts's EADDRINUSE handler then called process.exit(1), failing local:gate.

The test now passes port: 0 to startHonoServer, so the server owns its port from the moment the kernel picks it. It reads the real port back from the URL handed to the (mocked) opener. That URL is the banner URL, which printServerBanner builds from the bound port (info.port). findFreePort and its node:net import are removed.

  • No change to server.ts. As the issue asks, this is a fix to the test, not to the server's handling of a port that is really taken (Race condition: 'PORT IS IN USE' error caused by Inspector's own process #1090). startHonoServer already accepted 0 and already reported the bound port through the banner, so no new API was needed.
  • allowedOrigins is now [], because the origin isn't known until after the bind. The test only fetches /, and the allow-list guards only /api/*.
  • The assertions are slightly stronger: the opener is called exactly once, with a 127.0.0.1 URL on a nonzero port, and that URL's origin serves / with 200.

Verification

  • server-auto-open.test.ts: 4/4 passing.
  • npm run local:gate: green through local:validate, local:dco, verify:skills:cli, coverage, verify:build-gate, verify:bundle-externals and smoke. smoke:web:firefox fails to launch Firefox on the maintainer's macOS 27 machine (smoke:web:firefox cannot launch Playwright's Firefox on macOS 27 #2625), which stops the chain. So that pass ran in the Linux Playwright container (mcr.microsoft.com/playwright:v1.62.1-noble, clean git archive export) and passed. local:storybook was run on its own and passed (123 files, 529 tests).
  • The first gate attempt failed on an unrelated flake in auth/node/storage.test.ts, which clears the real ~/.mcp-inspector OAuth store that all worktrees share. That's filed as Flaky: OAuth storage test clears the real ~/.mcp-inspector store, racing other worktrees #2668 and kept out of this PR.

🤖 Generated with Claude Code

…#2602)

findFreePort() closed its probe socket before startHonoServer bound the
port, so under load another process could take it and the server's
EADDRINUSE handler process.exit(1)'d the run. Bind :0 and read the real
port back from the URL handed to the opener, which the banner builds
from the bound port.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall cliffhall added the v2 Issues and PRs for v2 label Oct 12, 2026
@cliffhall
cliffhall requested a balanced review from Copilot October 12, 2026 04:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused test-only change correctly resolves #2602 without altering production behavior.

0 open findings

What changed in this PR

Eliminates a flaky port-selection race in the web server auto-open integration test.

Changes:

  • Binds the test server directly to ephemeral port 0.
  • Derives and validates the assigned port from the mocked browser URL.
  • Removes the obsolete free-port probe.
File Description
clients/​web/​src/​test/​integration/​server/​server-auto-open.test.ts Uses race-free ephemeral binding and strengthens startup assertions.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed after round 1, which was clean: 0 inline comments, "0 open findings" in the headline, and no suppressed comments. Per the pr-flow exit rule, no confirming round was requested.

@cliffhall
cliffhall merged commit ed03ae9 into v2/main Oct 12, 2026
5 checks passed
@cliffhall
cliffhall deleted the v2/fix/2602-auto-open-port-race branch October 12, 2026 04:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky: server-auto-open.test.ts races for its port and process.exit(1)s under load

2 participants