Skip to content

Refuse a second server on the same data directory before database work - #7663

Closed
lanjak wants to merge 1 commit into
get-bb:mainfrom
lanjak:fix-7360-datadir-lock
Closed

lanjak wants to merge 1 commit into
get-bb:mainfrom
lanjak:fix-7360-datadir-lock

Conversation

@lanjak

@lanjak lanjak commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

runServer in apps/server/src/start-server.ts had no check for another server on the same BB_DATA_DIR before it touched the database. A second server on the same data directory (for example on another port, or started without the launcher) ran initDb (migrations), the server-import boot and runStartupRecoverySweep against the shared bb.db before anything stopped it. The sweep acts on state that it takes to be from a previous server process. The launcher guard from #5404 catches only a second server that answers /health at the same URL (#7360, triage report).

What changed

  • packages/config/src/file-lock.ts: a new acquireFileLock({ path, timeoutMs }) takes the existing native lock (fs-native-extensions) and returns a release function. withFileLock now uses it, with the same behavior.
  • apps/server/src/services/system/data-dir-lock.ts: lockServerDataDir takes <data-dir>/server.lock and keeps it until the process exits. It waits up to 15 s, so a server that the launcher restarts can wait for the previous process to exit. After that it throws ServerDataDirInUseError: "Another bb server is using the data directory ... Stop that server before starting another one on this directory."
  • start-server.ts: runServer takes the lock first, before refuseInterruptedServerImport and initDb.

This uses the native lock that the server already loads for managed-json-file, so there is no new dependency and no lockfile change. The OS releases the lock when the process exits, also after a crash or SIGKILL, so a stale lock cannot block a restart. The daemon's proper-lockfile lock was not used, because its stale-lock detection is time-based and the server can stall for longer than its 10 s window (#6835).

Behavior to note: if a second launcher starts on a data directory that is in use, its server exits with this error, and the launcher restarts it after its usual delay. Each attempt fails without touching the database.

No wire, CLI or documentation change.

How you verified

  • New apps/server/test/system/data-dir-lock.test.ts: a second lock on the same directory fails with ServerDataDirInUseError, and succeeds after the first is released.
  • The existing packages/config/test/file-lock.test.ts (lock held by a child process, released when the child dies) passes on the refactored code.
  • pnpm exec turbo run typecheck lint test --filter=@bb/server --filter=@bb/config: server 4111 tests and config 102 tests pass. oxfmt --check passes on the changed files.

Not checked: two real server processes on one data directory, and Windows (LockFileEx through fs-native-extensions).

Fixes #7360

AGENT GENERATED

runServer ran migrations, the server-import boot and the startup
recovery sweep before anything stopped a second server on the same
data directory. The server now takes an OS file lock on
<data-dir>/server.lock first, waits up to 15 s for a restarting server
to exit, and then fails with a clear message. The OS releases the lock
when the process exits, also after a crash.

Fixes get-bb#7360
alexburlis added a commit to alexburlis/omnyo that referenced this pull request Nov 26, 2026
Record the upstream PRs, revisions, contributors, and Omnyo integration
commits for merge d2a24ac in docs/upstream-credits.md.

Original-PR: get-bb/bb#7663
Original-Commit: get-bb/bb@1737a03
Original-Author: rufi
Original-PR: get-bb/bb#8055
Original-Commit: get-bb/bb@651ae20
Original-Author: Jake Lancaster
Original-PR: get-bb/bb#8075
Original-Commit: get-bb/bb@511e1ed
Original-Author: Jake Lancaster
Original-Co-Author: Michael Yong
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8096
Original-Commit: get-bb/bb@775090b
Original-Author: Michael Yong
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8145
Original-Commit: get-bb/bb@8de6711
Original-Commit: get-bb/bb@0d3db49
Original-Commit: get-bb/bb@92d3a76
Original-Commit: get-bb/bb@6705422
Original-Author: Michael Yong
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8171
Original-Commit: get-bb/bb@b56ed04
Original-Author: Jake Lancaster
Original-PR: get-bb/bb#8238
Original-Commit: get-bb/bb@2a9a142
Original-Author: SawyerHood
Original-PR: get-bb/bb#8247
Original-Commit: get-bb/bb@ba4ae22
Original-Author: Salem
Original-Co-Author: Claude Opus 5.5
Original-PR: get-bb/bb#8259
Original-Commit: get-bb/bb@96f40a9
Original-Author: SawyerHood
Original-PR: get-bb/bb#8263
Original-Commit: get-bb/bb@141e40a
Original-Author: mk
Original-PR: get-bb/bb#8266
Original-Commit: get-bb/bb@84e20e9
Original-Author: ILIA GOLUBEV
@github-actions github-actions Bot closed this Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

This is an automated message. We have closed this pull request because it has been open for more than two weeks.

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.

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

1 participant