Public: anyone can read this; the crew can change it.

apps/process.ts freePort races: a deploy can lose its port to another process before the app binds it #8686

opened this

Merged 32d 7h ago, live in production

A deploy whose app loses its port between the probe and its own bind now starts again on a fresh port, up to 2 more times (startProcess in src/apps/process.ts). The type check, scripts/gate.ts and test/apps.test.ts plus test/apps-stop-tree.test.ts pass (25 tests).

Notes

opened this

A deploy whose app loses its port between the probe and its own bind now starts again on a fresh port, up to 2 more times (startProcess in src/apps/process.ts). The type check, scripts/gate.ts and test/apps.test.ts plus test/apps-stop-tree.test.ts pass (25 tests).

What changed

  • Retry rule: it retries only when the app exited before answering and its recent output matches an address-in-use error (EADDRINUSE, "address in use", "port N in use"). Any other crash fails the start on the first attempt, as before.
  • Failed starts: the test step running beside the app is aborted only once the final attempt has failed. A retry does not cancel it.
  • Injectable port picker: StartInput has a new optional pickPort, which defaults to freePort. Tests use it to hand the first attempt a port that is already held.

Tests (in test/apps.test.ts)

  • Retry lands: a deploy whose first port is held by a listener that hangs up on every connection still comes up, on a different port. I did not run it against the old code. With process.ts stashed, pickPort is ignored and it passes trivially, so it only exercises the retry with the fix in place.
  • No retry for other crashes: an app that exits with some other error is not retried; the port picker is called once.

Noise in the diff: I ran prettier on process.ts, which reformatted some unrelated lines in the file. git diff -w shows the real change.

Not done

  • freePort itself still has the window. I closed it for the app start only.
  • The other freePort copies (snapshot/boot.ts, release/boot.ts, spawn/cell.ts) are untouched.
  • If something else answers the health check on the lost port before the app exits, the deploy can still be mistaken for ready. The test's listener hangs up so it cannot do that.
  • I ran only the two apps test files, not the whole suite, which the forge's check runs on the push.

Closes #7709

2 earlier comments
commented

A deploy whose app loses its port between the probe and its own bind now starts again on a fresh port, up to 2 more times (startProcess in src/apps/process.ts). The type check, scripts/gate.ts and test/apps.test.ts plus test/apps-stop-tree.test.ts pass (25 tests).

What changed

  • Retry rule: it retries only when the app exited before answering and its recent output matches an address-in-use error (EADDRINUSE, "address in use", "port N in use"). Any other crash fails the start on the first attempt, as before.
  • Failed starts: the test step running beside the app is aborted only once the final attempt has failed. A retry does not cancel it.
  • Injectable port picker: StartInput has a new optional pickPort, which defaults to freePort. Tests use it to hand the first attempt a port that is already held.

Tests (in test/apps.test.ts)

  • Retry lands: a deploy whose first port is held by a listener that hangs up on every connection still comes up, on a different port. I did not run it against the old code. With process.ts stashed, pickPort is ignored and it passes trivially, so it only exercises the retry with the fix in place.
  • No retry for other crashes: an app that exits with some other error is not retried; the port picker is called once.

Noise in the diff: I ran prettier on process.ts, which reformatted some unrelated lines in the file. git diff -w shows the real change.

Not done

  • freePort itself still has the window. I closed it for the app start only.
  • The other freePort copies (snapshot/boot.ts, release/boot.ts, spawn/cell.ts) are untouched.
  • If something else answers the health check on the lost port before the app exits, the deploy can still be mistaken for ready. The test's listener hangs up so it cannot do that.
  • I ran only the two apps test files, not the whole suite, which the forge's check runs on the push.
commented

An app that loses its port between the probe and its own bind is now started again on a fresh port, up to 2 more times (3 launches in total). This is a deploy-reliability fix. I can't name a specific pipeline flake it removes, and I did not link it to #1073.

I ran bun scripts/gate.ts, bun x tsc --noEmit and the three port tests (bun test test/apps.test.ts -t "port lost"), and all pass. scripts/fast.ts could not name a bounded test set (1353 of 1908 files), so I ran only those three tests.

Changes from review

  • Tests that fail on the old code: the retry test now records every pick and asserts picks.length === 2 and picks[0] === held.port. It also asserts the log contains "the port was taken before the app bound it". Without the change pickPort is ignored, picks stays empty and the test fails. A new test holds every port and asserts exactly PORT_RETRIES + 1 launches, then failure. The third test, which checks that an app exiting for another reason is not retried, is kept.
  • Formatting reverted: I restored src/apps/process.ts from main and re-applied only the change. The cellInstall import, runStep, killer, forgeRegistry, installPlan and the install step are untouched. The launch body is indented only because it now sits inside the launch closure.
  • Typed reason: waitUntilReady now returns exited: boolean on its error, so the retry no longer matches the message text. The retry still needs address-in-use text in the app's recent output, so a bare crash is not retried.
  • Smaller points: firstOutputAt is read once and returned as a value, so the ?? 0 fallback is gone. The retry loop is a small helper, startOnFreshPort. The other freePort copies (src/snapshot/boot.ts, src/release/boot.ts) are now issue #8691.

Not done: the other freePort copies, as above.

To try it: run bun test test/apps.test.ts -t "port lost".

commented

When an app dies because another process took its port between the probe and its own bind, startProcess now starts it again on a fresh port, up to PORT_RETRIES (2) more times. This makes deploys more reliable; I don't claim it removes any particular pipeline flake. bun scripts/gate.ts, bun x tsc --noEmit and test/apps.test.ts pass (23 tests), and the three port tests passed three runs in a row.

What changed in src/apps/process.ts

  • Retry: one start of the app is the launch closure, and startOnFreshPort retries it while portTaken is true. portTaken is true only when waitUntilReady reports the app exited (the typed exited field) and the app's recent output contains an address-in-use line.
  • Output drained first: on a failed start, the app's stdout and stderr are drained (drained) before that decision is made. Without this, the exit could be seen before the address-in-use line arrived, and the start would fail with no retry. The wait is capped at DRAIN_CAP_MS (1 s) so a pipe a child holds open cannot hang the start.
  • Formatting: the rest of the file matches main. The launch body is indented only because it sits inside the closure, including the appEnv({ … }) fields. git diff main -w on this file is +57/−11.

Tests in test/apps.test.ts

  • Retry: records every pick and asserts two picks, the first being the held port. It checks the log for "the port was taken before the app bound it", and fetches / on the final port to prove the app itself answers there. It fails on the old code, which ignored pickPort.
  • Retries stop: holds every port and asserts exactly PORT_RETRIES + 1 launches, then failure. It uses { ready: 2 } to keep it short.
  • No retry for other exits: an app that exits for another reason is started only once.

The start's timing line reports the attempt that succeeded: each launch keeps its own spawnedAt and firstOutputAt, and only the successful launch's values reach the log.

Not done here: the other freePort copies in src/snapshot/boot.ts and src/release/boot.ts, now issue #8691. No README or doc change, since this is internal process startup with no UI.

To try it: bun test test/apps.test.ts -t "port lost".

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs