Skip to content

bb-app start boots a server against the shared database when another bb already serves the data directory #5404

Closed

Description

@duizabojul

Summary

Suppose another bb process already holds bb-app-runtime.json and answers /health on the configured URL. bb-app start then prints "Another bb already runs on this data directory", but it still spawns a server child. That child opens the shared bb.db and runs the startup recovery sweep. It then logs Server listening, dies with EADDRINUSE, and bb-app exits 1. A service manager that restarts on failure repeats this every ~11 s for as long as the other bb runs.

Expected: bb-app start already knows another bb serves this data directory at this URL, so it should not spawn a server child.

Versions and environment

Steps to reproduce

d=$(mktemp -d)
export BB_DATA_DIR=$d BB_SERVER_PORT=49386 BB_HOST_DAEMON_PORT=49387 BB_TELEMETRY=false
bb-app start > /tmp/a.out 2>&1 &                        # A
until curl -sf http://127.0.0.1:49386/health; do sleep 1; done
/usr/bin/time -l bb-app start --no-in-app-updates; echo "exit=$?"   # B
ls "$d/logs" | grep -c uncaughtException
bb-app stop

Expected vs actual

Actual output of B (exit 1):

  !  Another bb already runs on this data directory
     record: .../bb-app-runtime.json
     Run `bb-app stop` to stop it.

  ○  Starting server
  ✗  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:49386/health

B's child writes "msg":"Server listening" to server.N.log even though it never binds. About 43 ms later it writes process-server-uncaughtException-*.json with listen EADDRINUSE: address already in use 127.0.0.1:49386.

Each version ran 3 times with the same flags (node dist/bb-app.js start --no-in-app-updates), measured with /usr/bin/time -l:

B wall user+sys CPU max RSS new diagnostic files
0.46.0 0.98–1.00 s 0.90–0.91 s 237–239 MB 1 per run
0.46.0 + proposed fix 0.11–0.14 s 0.12–0.14 s 75 MB 0

Against a copy of a real 413 MB bb.db, the unpatched cost is the same (0.99–1.08 s wall).

Evidence from a real install

  • ~/Library/Logs/bb-server.log (launchd stdout) contains "Another bb already runs on this data directory" 6,836 times. It contains "Restarting server" (the in-process supervisor) only 2 times, and both restarts succeeded. Every cycle is therefore a fresh bb-app start from launchd.
  • ~/.bb/logs/ holds 6,888 process-server-uncaughtException-*.json files, 6,846 of them EADDRINUSE. They take 27 MB and none were pruned.
    • Daily counts: 895 on 2026-09-29, 3,681 on 09-30, and 2,265 on 10-12 (00:54:30Z → 07:46:30Z).
    • The median gap is 11.0 s: launchd's 10 s throttle plus ~1 s of doomed boot.
  • During the 10-12 window, the doomed children log no plugin loads, only Server listening.
  • Code at d011a88:

Risk (code reading only, not reproduced)

runStartupRecoverySweep re-drives creating/provisioning environments and starting threads. For threads it skips only those where hasLiveThreadStartInFlight is true. That guard is an in-memory, process-local lock, so a doomed second server cannot see starts in flight in the live one. I have not observed any resulting damage.

Proposed fix

Commit: duizabojul@457cb56 (+10 lines in packages/bb-app/src/launcher.ts, on top of d011a88; branch duizabojul:fix/bb-app-start-skip-duplicate-server).

If the runtime record is held by another live process and isHealthyServerAnswering(<serverUrl>/health) returns true, the launcher logs ✗ bb already serves <url> from this data directory and exits 1 before spawning anything. I kept exit code 1 so that a restart-on-failure service still takes over once the other bb quits. Each retry then costs ~0.12 s of CPU and writes no file.

Verification:

  • The table above for the duplicate case.
  • A lone patched start with no other bb still reaches bb is ready and exits 0 on bb-app stop.
  • vitest in packages/bb-app: 199 pass. 4 fail with or without the patch, at d011a88 too: the config restart-notice tests in index.test.ts.

I can open the PR once I'm approved as a contributor.

What I ruled out

Suggested priority

Low to medium. It only affects setups that run bb-app start under a restart-on-failure service next to another bb on the same data directory. While the loop lasts it costs about 9% of one core (0.96–0.99 s of CPU every 11 s through the npm shim, same /usr/bin/time -l method) plus one diagnostic file every 11 s. Workaround: run only one of the two. No data loss observed.

AGENT GENERATED

Activity

  1. added theissue type on Oct 12, 2026
  2. added
    host
    cli
    confirmed-repro
    on Oct 12, 2026
  3. bb-slop-cop commented on Oct 12, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · High priority · Low effort; areas host, cli.
    • Reproduction report: REPRODUCED, high root-cause confidence. Applied confirmed-repro.
    • Root cause: A rejected runtime-record claim only warns; startup still spawns a server that initializes the shared store before failing on the occupied port.
    • Verification: Reproduced in two clean trusted origin/main checkouts. startup-runtime-record.test.ts fails before the fix and passes after; all 206 package tests and typecheck pass. Patched real-process startup creates no duplicate child or diagnostic and leaves the incumbent healthy.
    • Fix: Ready PR #5406, 112 changed text lines across three files. Not merged.

    AGENT GENERATED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliconfirmed-reprohost

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions