Repository navigation
Make bb-app verify its server child's identity on /health - #2162
Merged
Merged
Conversation
The launcher decided its server child was up as soon as GET /health on the configured port returned 200. It never checked who answered. When another bb server already owned the port (a second bb-app with a different data dir and default ports), that server answered the first poll while the launcher's own child was still booting, so the launcher printed "Server listening", minted an enroll key from the foreign server, and its daemon persisted the resulting host key into the wrong data dir. The child then died with EADDRINUSE. Once the foreign server was gone, the poisoned data dir failed every start with 401 INVALID_API_KEY until auth.json was deleted (#1558). The launcher now hands each server spawn a fresh BB_SERVER_LAUNCH_ID, the server echoes it on /health, and the readiness probe accepts only a response carrying that id, mirroring the hostId/serverUrl check the daemon probe already does. A foreign server never satisfies the probe, the child's exit surfaces as a startup failure naming the port collision, and enrollment is never reached. Co-Authored-By: Claude <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
The bb-app launcher decided its server child was up as soon as
GET /healthon the configured port returned 200 (waitForHealthinpackages/bb-app/src/launcher.ts). It never checked who answered./healthis a constant{ ok: true }, and a bb server takes seconds to boot, so when another bb server already owned the port (a secondnpx bb-appwith a differentBB_DATA_DIRand default ports), the foreign server satisfied the first 100 ms poll every time. The launcher printedServer listening, calledmaybeAddAutoJoinEnv, minted an enroll key from the foreign server, and its daemon wrote the resulting host key into the second data dir before its own server child died withEADDRINUSE. Once the foreign server was gone, that data dir failed every start with401/INVALID_API_KEYuntilauth.jsonwas deleted. The daemon probe (waitForHostDaemonStatus) already checkshostId/serverUrl; the server probe had no identity check at all.Issue: #1558. Report: https://get-bb.github.io/reports/issues/1558.html
What changed
packages/bb-app/src/launcher.ts:startFullStackServerProcessgenerates a freshBB_SERVER_LAUNCH_ID(randomUUID()) per spawn and passes it in the server child's env.waitForHealthbecamewaitForServerHealth: it parses the/healthbody and accepts a 200 only when it echoes that launch id. A foreign 200 keeps the poll going, so the child'sEADDRINUSEexit surfaces asProcess exited before becoming healthy: another server is already answering at <url>. The probe now also uses the same per-request timeout as the daemon probe. The startup failure line prints the real reason instead of the fixed(health check timed out).packages/config/src/env-vars.ts,packages/config/src/server.ts: new optionalBB_SERVER_LAUNCH_IDread at the server's env boundary. It is launcher-owned and overwritten per spawn, so it is not a user knob and is not added to the startup-only env key list or docs (same treatment asBB_APP_VERSION).apps/server/src/types.ts,apps/server/src/start-server.ts,apps/server/src/server.ts:ServerRuntimeConfig.launchId(absent when the server was not spawned by the launcher);/healthreturns{ ok: true, launchId }when set and{ ok: true }otherwise. Additive only; the desktop and mobile probes parse/healthwith non-strict schemas.HOST_DAEMON_PROTOCOL_VERSIONbump. Launcher and server ship in the same bb-app package.This follows the report's proposed fix. The report's optional follow-ups (give the supervision restart loop an exit, add a recovery hint to the daemon's 401 message) are not in this PR; with the identity probe the launcher never reaches enrollment or supervision when the port is taken.
How you verified
New tests:
packages/bb-app/test/server-health-identity.test.tswaitForServerHealthrejects a foreign/healthwhile the child is booting and exits (the report's repro). Onorigin/main(withwaitForHealthexported) it fails withAssertionError: expected 'healthy' not to be 'healthy'.startFullStackServerProcessagainst a fake server entry: hands the child a launch id and accepts its/healthonce it listens; with another listener on the port it rejects withServer failed to become healthy: Process exited before becoming healthy: another server is already answering at ...and leavesprocesses.serverRunnull.apps/server/test/app/skeleton.test.ts:/healthechoeslaunchIdonly when configured.packages/config/test/config.test.ts:BB_SERVER_LAUNCH_IDis carried only when set.Commands (from the committed tree,
git status --porcelainempty):pnpm exec turbo run typecheck—Tasks: 72 successful, 72 totalpnpm exec turbo run test --filter=@bb/config --filter=bb-app --filter=@bb/server— config 108/108, bb-app 70/70, server 1823/1824. The one server failure isinternal-skill-trees.test.tsexpecting file mode 0644 and getting 0664; it is the known local umask 0002 artifact, unrelated to this change, and passes here underumask 022.Manual repro (report steps, built
packages/bb-app/dist/bb-app.jsfrom this branch, ports 47886/47887, data dirs under~/.bb-dev/scratch/1558/):/healthreturns{"ok":true,"launchId":"ff964df3-..."}.✗ Server failed to start/Server failed to become healthy: Process exited before becoming healthy: another server is already answering at http://127.0.0.1:47886/health. Its data dir has noauth.jsonand nohost-id;select id from hostson instance one's db lists only its own host. Before the fix the report shows✓ Server listening, anauth.jsonin the second data dir, and the host row in the first db.bb is ready, its host is enrolled in its own db.Fixes #1558
Independent verification
Verified on a fresh worktree at
1353c6477(branch contains currentorigin/main;git merge-base --is-ancestor origin/main HEADtrue;mergeable: MERGEABLE).Root cause check: the report and fix agree with my own reading of
origin/main—waitForHealthreturned on any 200 from/health(a constant{ok:true}), so a foreign bb server on the port satisfied the probe before the child reachedlisten(), andmaybeAddAutoJoinEnvthen enrolled the daemon there. The fix is launcher-to-server only (BB_SERVER_LAUNCH_IDenv ->/healthecho -> probe match); nothing on the server/host-daemon wire changes, so noHOST_DAEMON_PROTOCOL_VERSIONbump is needed. Every other in-repo/healthconsumer (desktopserver-probe.ts, mobileprofiles/probe.ts,packages/scripts/wait-for-server-health.ts) uses a tolerant schema, so the additivelaunchIdfield is safe. Thebb-app server(server-only) path has no health probe and is untouched.Fail-before / pass-after (swapped the six non-test source files to
origin/mainwithgit checkout origin/main -- ..., ran the PR's tests, then restored withgit checkout HEAD -- ...):packages/configconfig.test.ts -t "launch id": beforeAssertionError: expected undefined to be 'launch-123'; after 1 passed.apps/serverskeleton.test.ts -t "launch id": beforeAssertionError: expected { ok: true } to deeply equal { ok: true, launchId: 'launch-123' }; after 1 passed.packages/bb-appserver-health-identity.test.ts: before, as-is, 5/5 fail withTypeError: waitForServerHealth is not a function. To prove the failures are behavioral and not just missing exports, I also exported main'swaitForHealth/startFullStackServerProcessand pointed a copy of the test at them: 5/5 still fail, withexpected 'healthy' to be 'Process exited before becoming health…',expected 1 to be 3,promise resolved "undefined" instead of rejecting,expected { ok: true } to deeply equal { ok: true, …(1) }, andpromise resolved "{ …(3) }" instead of rejecting. After restoring the PR sources: 5/5 pass.Turbo (from the clean committed tree):
pnpm exec turbo run typecheck --filter=@bb/config --filter=bb-app --filter=@bb/server --filter=@bb/host-daemon --filter=@bb/desktop->Tasks: 8 successful, 8 totalpnpm exec turbo run test --filter=@bb/config --filter=bb-app --filter=@bb/server --force-> config 108/108, bb-app 70/70, server 1823/1824; the single failure isinternal-skill-trees.test.ts(mode 420 vs 436), the known local umask 0002 artifact, unrelated and green in CI.Repro on the fixed branch (built
packages/bb-app/dist/bb-app.js, ports 47890/47891, data dirs under~/.bb-dev/scratch/1558-verify/):bb is ready;/health->{"ok":true,"launchId":"46f5a30b-..."}.✗ Server failed to start/Server failed to become healthy: Process exited before becoming healthy: another server is already answering at http://127.0.0.1:47890/health. Its data dir contains onlyauth-secret bb.db bb.db-shm bb.db-wal launcher.log logs(noauth.json, nohost-id); instance one'shoststable holds only its ownhost_w5qw5tqrvh; instance two'shoststable is empty.bb is ready, enrolled in its own db (host_9c6qb6u6qj). No 401 / INVALID_API_KEY.bb is ready, no restart loop, no enrollment.CI: all checks pass (Checks, Package Smoke x2, Tests app-1/2/3, integration, packages, server; iOS and Node compat matrix skipped as usual).
Residual risks (not blocking): the server still logs
[server] Server listeningbefore the socket is bound (report's adjacent defect (a)), so an EADDRINUSE child prints a misleading INFO line; the supervision loop still retries a server child that cannot bind after a mid-run crash; the twostartFullStackServerProcesstests reserve a free port and bind it ~300 ms later, a small theoretical race under heavy parallel load.