Skip to content

test(website): reserve the dev-SSR port and hold it on both loopbacks - #1647

Merged
trueadm merged 2 commits into
mainfrom
test/dev-ssr-port-reservation
Oct 3, 2026
Merged

trueadm merged 2 commits into
mainfrom
test/dev-ssr-port-reservation

Conversation

@trueadm

@trueadm trueadm commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Test-harness only. This fixes the dev-SSR server's port race in website/tests/ssr-hydration.e2e.test.ts. The website integration suite failed on #1625 (run 37106507164, job 111156042312) with server for http://localhost:41193/ exited with code 1 before listening / Error: Port 41193 is already in use. The other 67 website tests passed.

  • The dev server reserves its port. It previously called getFreePort(), which closed its probe socket before the .vite cache removal and the vite spawn. The cache is now removed first. The port is then reserved and released immediately before spawnServer.
  • reserveFreePort() holds the port on both 127.0.0.1 and ::1. Vite (8.1.5, --strictPort) binds localhost, and Node resolves that to a single address, which can be ::1. A hold on 127.0.0.1 alone said nothing about [::1]:port. Locally localhost resolves to ::1 first, and with only the IPv4 hold a second ::1 listener bound the same port. Holding both makes that bind fail with EADDRINUSE. A host without IPv6 loopback keeps the IPv4-only hold. This also covers the production preview reservation, which uses the same helper.
  • The window between release and bind gets a bounded retry. release() has to come before the child binds, and pnpm exec vite then loads its config and every plugin before it calls listen(). That window is much longer than the milliseconds between release() and the spawn, and no reservation can cover it. startServerOnFreePort() retries on a fresh reservation, at most three times. It retries only when the child exited before listening and vite reported that exact port as in use. Any other startup failure is rethrown unchanged.
  • getFreePort() is removed. It had no other caller.

The hardened properties from the earlier port-squatter fix are unchanged:

  • an ephemeral port per run;
  • detached: true with a process-group kill in stopServer();
  • waitForServer rejecting when the child exits.

The spawn callback assigns server as it spawns, so afterAll still stops the child if the hook times out mid-start.

Validation

  • pnpm format:check, not run repo-wide. pnpm format:files:check passes on the four touched and related files.
  • pnpm typecheck, not run repo-wide. pnpm typecheck:files website/tests/ssr-hydration.e2e.test.ts website/tests/support/server-process.ts website/tests/setup/production-server.ts website/tests/server-process.test.ts passes.
  • pnpm test, not run locally because the machine is shared with parallel sessions. CI covers it.
  • targeted: a standalone node check on macOS. With only 127.0.0.1 held, ::1 binds the same port (the gap). With both held, it gets EADDRINUSE. localhost resolves to ::1 here.
  • New unit cases in website/tests/server-process.test.ts (run by the website project in CI; not run locally):
    • a reservation holds the port on 127.0.0.1, and on ::1 when the host has it, until release(). The ::1 case fails against the old IPv4-only reservation.
    • a server whose first child exits with vite's lost-port message comes up on a new port. The old single-shot path rejects here.
    • the retry is bounded.
    • a non-port startup failure is not retried.
  • The website integration suite job in CI is the end-to-end check.

Risk / follow-ups

  • The production preview server keeps the same release-to-bind window. It can't use the retry, because its origin goes to the specs through project.provide before the build, so its port cannot change. Closing that would mean moving the origin into the ready-state file. That is left out of this PR.
  • The retry only fires on vite's exact Port <released port> is already in use message. A real startup failure still fails the suite on the first attempt.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Low Risk
Changes are limited to website test utilities and e2e setup; no production application or runtime behavior is modified.

Overview
Test-harness only: hardens website integration/e2e server startup so Vite dev SSR no longer flakes on Port N is already in use under --strictPort.

reserveFreePort() now holds an ephemeral port on both 127.0.0.1 and ::1 (with retry when IPv6 side is taken), because localhost may bind only on ::1 while an IPv4-only hold left the same port free there. getFreePort() is removed.

New startServerOnFreePort() reserves, releases immediately before spawn, waits for listen, and retries up to three times only when the child exits before listening with Vite’s exact “that port already in use” message—covering the gap between release and Vite’s slow listen().

ssr-hydration.e2e.test.ts clears the .vite cache before reserving, then boots the dev server through this helper (assigning server in the spawn callback for safe teardown). server-process.test.ts adds unit coverage for dual-loopback holds, successful retry on lost port, bounded retry, and no retry on other failures.

Reviewed by Cursor Bugbot for commit 8df929d. Bugbot is set up for automated code reviews on this repo. Configure here.

The dev-SSR hydration e2e picked its port with getFreePort(), which closed
the probe socket before the .vite cache removal and the vite spawn. On PR
#1625 another process took the port in that window and the strictPort dev
server died with "Port 41193 is already in use".

- reserveFreePort() now holds the port on 127.0.0.1 and ::1. The servers
  listen on `localhost`, which Node can resolve to ::1, and a hold on
  127.0.0.1 alone does not stop a ::1 listener from taking the same port.
- The dev server clears the optimize-deps cache first, then reserves, and
  releases immediately before the spawn via startServerOnFreePort().
- startServerOnFreePort() retries, on a fresh reservation and at most three
  times, only when the child exits before listening because vite reported
  that exact port in use. The pnpm and vite startup between release and
  bind is a window no reservation can cover.
- getFreePort() is removed; it had no other caller.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Oct 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
octane-website-mcp Skipped Skipped Oct 3, 2026 8:31am UTC

Request Review

@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent test PR: tests only labels Oct 3, 2026
@trueadm
trueadm marked this pull request as ready for review October 3, 2026 08:32
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No size increases and no timing regressions outside noise.

Compares dc3e18023 with 3892bc42c on the same runner. Timing verdicts require a change beyond the combined margin of error (at least 5%) and one 0.1ms timer tick, with the fastest sample also moving beyond that margin. Workflow run

js-framework: 1k/10k row operations (ms, lower is better)

All 20 timed operations (20 within noise)
target operation base head Δ noise
octane-tsrx run 3.35 3.51 +4.8% ±9% ⚪ within noise
octane-tsrx replace 7.30 6.73 −7.8% ±22% ⚪ within noise
octane-tsrx add 3.34 3.18 −4.8% ±16% ⚪ within noise
octane-tsrx update 0.43 0.34 −20.9% ±72% ⚪ within noise
octane-tsrx select 0.16 0.12 −25.0% ±99% ⚪ within noise
octane-tsrx swap 0.32 0.31 −3.1% ±50% ⚪ within noise
octane-tsrx remove 0.38 0.32 −15.8% ±66% ⚪ within noise
octane-tsrx runlots 28.68 28.79 +0.4% ±27% ⚪ within noise
octane-tsrx select_lots 0.25 0.25 −0.0% ±52% ⚪ within noise
octane-tsrx clear 40.16 39.56 −1.5% ±8% ⚪ within noise
octane-jsx run 3.51 3.40 −3.1% ±11% ⚪ within noise
octane-jsx replace 6.77 6.45 −4.7% ±17% ⚪ within noise
octane-jsx add 3.09 3.06 −1.0% ±8% ⚪ within noise
octane-jsx update 0.56 0.51 −8.9% ±33% ⚪ within noise
octane-jsx select 0.39 0.37 −5.1% ±31% ⚪ within noise
octane-jsx swap 0.67 0.62 −7.5% ±28% ⚪ within noise
octane-jsx remove 0.58 0.56 −3.4% ±27% ⚪ within noise
octane-jsx runlots 27.44 27.83 +1.4% ±10% ⚪ within noise
octane-jsx select_lots 2.62 2.41 −8.0% ±22% ⚪ within noise
octane-jsx clear 41.94 42.44 +1.2% ±25% ⚪ within noise

Application bundle size (bytes)

No changes across 45 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

@trueadm
trueadm merged commit 6858a8b into main Oct 3, 2026
49 checks passed
@trueadm
trueadm deleted the test/dev-ssr-port-reservation branch October 3, 2026 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent test PR: tests only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant