Skip to content

A second server on the same data dir runs migrations and the startup recovery sweep before it fails #7360

Open

Description

@k0d3r1s

Summary

runServer has no check for another server on the same BB_DATA_DIR before it touches the database. A second server on the same data dir but a different BB_SERVER_PORT runs initDb (migrations), the server-import boot and the startup recovery sweep before anything stops it. The sweep deletes forks whose history copy "the previous server process left unfinished" and releases queued-message claims. It can act on work the first server is still doing. On the same port, the second server fails only at listen, after the sweep has already run.

Repro

Code path (found by reading main; not run):

  1. Start a server on data dir D with port P1.
  2. Start a second server on D with a different port P2, so the launcher's health check on its own URL does not see the first server.
  3. The second process runs initDb, the server-import boot and runStartupRecoverySweep against the shared bb.db.
  4. It then listens on P2 successfully, or fails at listen if the port is the same.

Cause

Line numbers are from main at 7c1353d8.

  • apps/server/src/start-server.ts:133-153: runServer calls initDb with no data-dir guard. The only early refusal is for an interrupted server import (:138).
  • start-server.ts:504-509: runStartupRecoverySweep runs before the listener.
  • start-server.ts:518: startHttpListener comes after the sweep.
  • apps/server/src/services/system/periodic-sweeps.ts:881-910: the sweep steps, including interrupted-fork-history-copies and release-queued-message-claims.
  • packages/bb-app/src/launcher.ts:4206-4215 (the guard added for bb-app start boots a server against the shared database when another bb already serves the data directory #5404) exits only when another server answers /health at this process's own URL. A different port, or a server started without the launcher, is not caught.

Suggested fix

Make the data dir exclusive for the life of a server, and check that before any database work. Two generic ways:

  1. A lock file in BB_DATA_DIR, taken at the start of runServer, before initDb, and held until exit. A second server fails at once with a clear message such as "Another server is using this data directory".
  2. A cheaper early probe: before initDb, read the runtime record in the data dir and refuse to start if the process it names is alive.

Keep the existing launcher check. This covers the cases it cannot see.

export async function runServer(serverConfig: ServerConfig): Promise<void> {
  const release = await acquireDataDirLock(serverConfig.BB_DATA_DIR); // throws if held
  // ... initDb, import boot, recovery sweep, listener
}

Activity

  1. added theissue type on Nov 10, 2026
  2. added
    host
    confirmed-repro
    on Nov 10, 2026
  3. bb-slop-cop commented on Nov 10, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    • Classification: Bug · High priority · Medium effort · host.
    • Report: Reproduction and root-cause report.
    • Verdict: REPRODUCED · Confidence: high · Reproduction label: confirmed-repro.
    • Evidence: Repeated on two clean trusted-main checkouts. The first server stays healthy while a second soft-deletes a seeded in-progress fork, both on different ports and before a same-port listener conflict. The safety assertion fails in both runs; 17 related existing tests pass.
    • Root cause: Startup has no exclusive data-directory ownership before database initialization and recovery; listener conflicts occur afterward.
    • No PR: Safely reusing OS-backed lifetime locking requires shared-configuration and server-lifecycle changes, exceeding this rule’s one-subsystem fix limit.

    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

    confirmed-reprohost

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions