Skip to content

Make bb-app verify its server child's identity on /health - #2162

Merged
SawyerHood merged 1 commit into
mainfrom
bb/fix-1558-enroll-after-eaddrinuse
Aug 21, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
bb/fix-1558-enroll-after-eaddrinuse

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

The bb-app launcher decided its server child was up as soon as GET /health on the configured port returned 200 (waitForHealth in packages/bb-app/src/launcher.ts). It never checked who answered. /health is a constant { ok: true }, and a bb server takes seconds to boot, so when another bb server already owned the port (a second npx bb-app with a different BB_DATA_DIR and default ports), the foreign server satisfied the first 100 ms poll every time. The launcher printed Server listening, called maybeAddAutoJoinEnv, 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 with EADDRINUSE. Once the foreign server was gone, that data dir failed every start with 401 / INVALID_API_KEY until auth.json was deleted. The daemon probe (waitForHostDaemonStatus) already checks hostId/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: startFullStackServerProcess generates a fresh BB_SERVER_LAUNCH_ID (randomUUID()) per spawn and passes it in the server child's env. waitForHealth became waitForServerHealth: it parses the /health body and accepts a 200 only when it echoes that launch id. A foreign 200 keeps the poll going, so the child's EADDRINUSE exit surfaces as Process 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 optional BB_SERVER_LAUNCH_ID read 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 as BB_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); /health returns { ok: true, launchId } when set and { ok: true } otherwise. Additive only; the desktop and mobile probes parse /health with non-strict schemas.
  • No wire change between server and host daemon, so no HOST_DAEMON_PROTOCOL_VERSION bump. 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.ts
    • waitForServerHealth rejects a foreign /health while the child is booting and exits (the report's repro). On origin/main (with waitForHealth exported) it fails with AssertionError: expected 'healthy' not to be 'healthy'.
    • keeps polling past foreign answers until the launch id matches; times out with the "another server is already answering" reason when it never does.
    • startFullStackServerProcess against a fake server entry: hands the child a launch id and accepts its /health once it listens; with another listener on the port it rejects with Server failed to become healthy: Process exited before becoming healthy: another server is already answering at ... and leaves processes.serverRun null.
  • apps/server/test/app/skeleton.test.ts: /health echoes launchId only when configured.
  • packages/config/test/config.test.ts: BB_SERVER_LAUNCH_ID is carried only when set.

Commands (from the committed tree, git status --porcelain empty):

  • pnpm exec turbo run typecheck — Tasks: 72 successful, 72 total
  • pnpm 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 is internal-skill-trees.test.ts expecting file mode 0644 and getting 0664; it is the known local umask 0002 artifact, unrelated to this change, and passes here under umask 022.

Manual repro (report steps, built packages/bb-app/dist/bb-app.js from this branch, ports 47886/47887, data dirs under ~/.bb-dev/scratch/1558/):

  1. Instance one starts; /health returns {"ok":true,"launchId":"ff964df3-..."}.
  2. Instance two (different data dir, same ports) now exits 1 with ✗ 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 no auth.json and no host-id; select id from hosts on instance one's db lists only its own host. Before the fix the report shows ✓ Server listening, an auth.json in the second data dir, and the host row in the first db.
  3. Stop instance one, start instance two alone: bb is ready, its host is enrolled in its own db.

Fixes #1558

AGENT GENERATED: by Claude Opus 5

Independent verification

Verified on a fresh worktree at 1353c6477 (branch contains current origin/main; git merge-base --is-ancestor origin/main HEAD true; mergeable: MERGEABLE).

Root cause check: the report and fix agree with my own reading of origin/main — waitForHealth returned on any 200 from /health (a constant {ok:true}), so a foreign bb server on the port satisfied the probe before the child reached listen(), and maybeAddAutoJoinEnv then enrolled the daemon there. The fix is launcher-to-server only (BB_SERVER_LAUNCH_ID env -> /health echo -> probe match); nothing on the server/host-daemon wire changes, so no HOST_DAEMON_PROTOCOL_VERSION bump is needed. Every other in-repo /health consumer (desktop server-probe.ts, mobile profiles/probe.ts, packages/scripts/wait-for-server-health.ts) uses a tolerant schema, so the additive launchId field is safe. The bb-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/main with git checkout origin/main -- ..., ran the PR's tests, then restored with git checkout HEAD -- ...):

  • packages/config config.test.ts -t "launch id": before AssertionError: expected undefined to be 'launch-123'; after 1 passed.
  • apps/server skeleton.test.ts -t "launch id": before AssertionError: expected { ok: true } to deeply equal { ok: true, launchId: 'launch-123' }; after 1 passed.
  • packages/bb-app server-health-identity.test.ts: before, as-is, 5/5 fail with TypeError: waitForServerHealth is not a function. To prove the failures are behavioral and not just missing exports, I also exported main's waitForHealth/startFullStackServerProcess and pointed a copy of the test at them: 5/5 still fail, with expected '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) }, and promise 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 total
  • pnpm 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 is internal-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/):

  1. Instance one: bb is ready; /health -> {"ok":true,"launchId":"46f5a30b-..."}.
  2. Instance two (different data dir, same ports): exits with ✗ 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 only auth-secret bb.db bb.db-shm bb.db-wal launcher.log logs (no auth.json, no host-id); instance one's hosts table holds only its own host_w5qw5tqrvh; instance two's hosts table is empty.
  3. Stop one, restart two on the freed ports: bb is ready, enrolled in its own db (host_9c6qb6u6qj). No 401 / INVALID_API_KEY.
  4. Report variant (only the server port collides, daemon port 47893): also fails fast at the server step with the same message; no 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 listening before 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 two startFullStackServerProcess tests reserve a free port and bind it ~300 ms later, a small theoretical race under heavy parallel load.

AGENT GENERATED: by Claude Opus 5

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]>
@SawyerHood
SawyerHood marked this pull request as ready for review August 21, 2026 06:39
@SawyerHood
SawyerHood merged commit 2845b3c into main Aug 21, 2026
13 checks passed
@SawyerHood
SawyerHood deleted the bb/fix-1558-enroll-after-eaddrinuse branch August 21, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bb-app can enroll a host against the wrong server after its server child fails with EADDRINUSE

1 participant