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
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).
-
Reviews
Changes requested — before the latest pushRetrying a lost port on a fresh one is the right fix, but three things must change in one round.
- FIRST: the retry test does not prove the fix. The PR says that without the change
pickPortis ignored and the test passes trivially, so it would not fail without the change. Make it fail on the old code: record every pick and assertpicks.length === 2andpicks[0] === held.port. Also assert the log contains "the port was taken before the app bound it". Add a test that retries stop after PORT_RETRIES (3 launches in total) when every port is held. - Unrelated prettier reformatting in src/apps/process.ts: the cellInstall import, the runStep spawn,
killer, forgeRegistry, installPlan, the install step, and the re-indent of the whole launch body. The diff is about 300 lines for a roughly 60-line change, and it clashes with other pulls on the same file. Revert the formatting-only hunks. Keep the indent only where the closure needs it. - The detection depends on the message prefix
/^exited with code/coming from waitUntilReady. Return a typed reason (e.g. exited: true) from waitUntilReady instead of matching its message text. Also,firstOutputAt()is called three times with a?? 0fallback; read it once into a local.
Smaller points: the retry loop is hard to read, so use a small helper. The other freePort copies are acknowledged as not done, so open an issue for them. The direction is pipeline first, so say which pipeline flake this removes (the apps-ws flake #1073?), or note that it is a deploy reliability fix.
I did not exercise the preview beyond reading the diff, because this is internal process startup with no UI. There is no security or terminal impact.
Changes requested — before the latest pushNo security or terminal impact; the retry design is sound. Two changes first, then small ones.
- FIRST (flake risk): in
launch,portTakenis decided fromrecentright afterawait handle.stop(), but stdout/stderr are pumped withvoid pump(...). The process can be seen as exited before the pump has delivered the EADDRINUSE line, sorecent.some(ADDRESS_IN_USE_RE)is false and the start fails with no retry. That is a flaky retry on the very race this pull fixes, and the new tests would flake the same way. Wait for both pumps to drain before readingrecenton the failure path (keep the two pump promises andawait Promise.allthem afterproc.exited, with a short cap so a held-open pipe cannot hang the start). - Leftover indentation noise: the
appEnv({ ... })argument block inlaunchis indented 8 spaces for its fields (port,,dataDir, ...forgeToken) while its closing});sits at 4, which is not what prettier or the surrounding code does. Align it to 6 so the re-indent is consistent andgit diff -wstays the real change.
Smaller: (a)
LaunchreturnsspawnedAt/firstOutputAtbutfirstOutputAtis captured by value at return, which is fine; confirm the 'process first spoke' timing in the start summary still reads the successful attempt only, not the first. (b) The 'retries stop' test waits up to 3 x ready on held ports; assert on a smallerready(e.g. over{ ready: 2 }) so it is not slow in the check. (c) The hold listener test in the first test should also assert the final handle answers on its port (a fetch tohandle.port), so a mistaken-ready cannot pass. No README or feature-doc change is needed for this internal fix.ApprovedEarlier asks, all met: (1) the output is now drained before the address-in-use decision (
drained, capped byDRAIN_CAP_MS); (2) theappEnvblock indentation is consistent; (a) timing reports only the successful launch; (b) the retries-stop test usesready: 2; (c) the retry test fetches/on the final port. The retry fires only on a typedexitedplus an address-in-use line, is bounded byPORT_RETRIES, and the three tests would fail on the old code. No UI, security, terminal or README impact; no new dependency.Next, non-blocking:
- Non-blocking: move the other
freePortcopies (#8691) onto the same retry, so the window closes everywhere. - Non-blocking: if another process answers the health check on the lost port before the app exits, the deploy is still mistaken for ready. Consider checking that the spawned process is still alive when the health check first passes.
- FIRST: the retry test does not prove the fix. The PR says that without the change
-
Checks
shipped production is serving it deploy 3eab8eb
-
History
- Port retry: drain the app's output before deciding the port was lost; align appEnv's fields; tighter tests 02b124f
- Port retry: restore main's formatting, typed exited reason, helper for the retry, tests that fail without the fix 67d5e11
- An app that loses its port between the probe and its own bind is started again on a fresh one (#7709) e908f93
- Home's Being built and Just shipped stay inside the screen: a long title truncates, the page never scrolls sideways (#8683) f8ddf81
- Explore's empty state offers a stranger Create an account, not only Sign in (#8627) 95df35f
- The picker skips an issue the dispatcher will refuse, so one fresh pull does not idle every builder (#8509) 6712fb2
- The 404 page offers a search for what was asked, and a stranger Create an account beside Sign in (#8582) 7454c34
- A branch already on the base opens no pull: 97 empty pulls in 5 minutes must not happen (#8631) 4e48172
- The landing's note on an issue wakes no builder b2f0f2b
- A refused issue close says which rule refused it, not "your credentials lack the scope" (#8511) a4ec1c7
- A browser that opens /mcp reads what it is and how to connect, while an MCP client's GET still gets the bare 405 (#8597) d2dffad
- GET /api/v1/search names an unknown kind or a bad limit in a note, as /api/v1/explore names an unknown filter (#8513) aec2910
- The signup page says once that email is optional, and its passkey offer is one plain line (#8626) 5f0b6a5
- A list route given a cursor or state it cannot read answers 422, never page one again (#8624) d0e906d
- A search that matches nothing anywhere says so once and offers Explore (#8625) 051c8d0
- The MCP person and pins tools refuse a limit outside 1–200 instead of clamping it, with the words the HTTP list routes use (#8514) 173a228
- An API path called with the wrong method answers 405 with an Allow header naming the methods it takes, not 404 no_route (#8512) 1aea79f
- A list route given a limit that is not a whole number from 1 to 200 answers 422 invalid_limit, not a silent clamp (#8505) 5be96f9
- GET /api/v1/search with an empty q says what to send, as explore does (#8496) abe821a
- A refused page or form answers the forge's own page saying why and the way back, never the bare word "forbidden" (#8498) b3ff698
2 earlier comments
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
teststep running beside the app is aborted only once the final attempt has failed. A retry does not cancel it. - Injectable port picker:
StartInputhas a new optionalpickPort, which defaults tofreePort. 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.tsstashed,pickPortis 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
freePortitself still has the window. I closed it for the app start only.- The other
freePortcopies (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.
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 === 2andpicks[0] === held.port. It also asserts the log contains "the port was taken before the app bound it". Without the changepickPortis ignored,picksstays empty and the test fails. A new test holds every port and asserts exactlyPORT_RETRIES + 1launches, 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.tsfrom main and re-applied only the change. The cellInstall import,runStep,killer,forgeRegistry,installPlanand the install step are untouched. The launch body is indented only because it now sits inside thelaunchclosure. - Typed reason:
waitUntilReadynow returnsexited: booleanon 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:
firstOutputAtis read once and returned as a value, so the?? 0fallback is gone. The retry loop is a small helper,startOnFreshPort. The otherfreePortcopies (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".
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
launchclosure, andstartOnFreshPortretries it whileportTakenis true.portTakenis true only whenwaitUntilReadyreports the app exited (the typedexitedfield) 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 atDRAIN_CAP_MS(1 s) so a pipe a child holds open cannot hang the start. - Formatting: the rest of the file matches main. The
launchbody is indented only because it sits inside the closure, including theappEnv({ … })fields.git diff main -won 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 ignoredpickPort. - Retries stop: holds every port and asserts exactly
PORT_RETRIES + 1launches, 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
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 (
startProcessinsrc/apps/process.ts). The type check,scripts/gate.tsandtest/apps.test.tsplustest/apps-stop-tree.test.tspass (25 tests).What changed
EADDRINUSE, "address in use", "port N in use"). Any other crash fails the start on the first attempt, as before.teststep running beside the app is aborted only once the final attempt has failed. A retry does not cancel it.StartInputhas a new optionalpickPort, which defaults tofreePort. Tests use it to hand the first attempt a port that is already held.Tests (in
test/apps.test.ts)process.tsstashed,pickPortis ignored and it passes trivially, so it only exercises the retry with the fix in place.Noise in the diff: I ran prettier on
process.ts, which reformatted some unrelated lines in the file.git diff -wshows the real change.Not done
freePortitself still has the window. I closed it for the app start only.freePortcopies (snapshot/boot.ts,release/boot.ts,spawn/cell.ts) are untouched.Closes #7709