Skip to content

fix(store): operator commands poll for the write lock beside a busy server (#1072) - #1073

Merged
jimhoyd merged 1 commit into
mainfrom
claude/store-cli-busy-1072
Sep 30, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
claude/store-cli-busy-1072

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1072

Reproduced first (macOS, main)

A writer fixture (packages/store/test/slow-commit-writer.ts) commits continuously to the store database: it holds the write lock hold ms per commit (synchronous=FULL, Atomics.wait inside the transaction), then idles gap ms on a timer before the next commit. Unlike #1071's fixture, it always leaves a realistic idle gap. A writer with no gap at all starves any retry loop on some platforms, and that is documented as a limit, not a target. urlcode-store members add was run repeatedly against the database beside it:

writer main this PR
20 ms hold, 1 ms gap 24/30, then 17/20 (failures after ~2.5 s) 20/20 (~450 ms each)
20 ms hold, 2 ms gap 16/20 not run
50 ms hold, 1 ms gap not run 10/10
reassign, 20 ms hold, 1 ms gap 10/10 (one took 3.1 s) 10/10
lock held 15 s not run fails after 10.4 s, as intended

On main about one members add in five failed (57 of 70 succeeded). members reports the failure as The store could not save this change (the 503 wrapping of SQLITE_BUSY). reassign reports database is locked.

Fix

  • openStoreDatabase(path, { operator: true }) is the operator connection. Its statements wait up to OPERATOR_LOCK_WAIT_MS (10 s). Its BEGIN IMMEDIATE, for both write transactions and schema-upgrade steps, is retried with busy_timeout=0 every 1 ms until that deadline, and the busy timeout is restored afterwards. This is the same approach fix(auth): operator commands poll for the write lock beside a busy server #1071 takes for auth.
  • addMember, removeMember, listMembers and reassignOwner open that connection, so the CLI commands do too. These are the store's operator path. They block their own process for as long as they wait.
  • Serving is unchanged. It keeps the 2 s busy_timeout and 503 storage_unavailable. The one-server lock and the operator fence are untouched.
  • audit, backup and restore only read, so WAL readers never wait on the write lock and they are unchanged.

Regression test

packages/store/test/operator-lock.test.ts runs a writer with a 25 ms hold and a 1 ms gap. Beside it, it runs 15 rounds of addMember, reassignOwner and removeMember in-process, then one urlcode-store members add through the CLI. It asserts that the writer committed throughout.

  • On main's source it failed 6 of 6 runs, plus 4 of 5 runs of an earlier 8-round version.
  • With the fix it passed 6 of 6 runs, taking about 1.2 to 1.4 s.

It should be safe on Windows. A coarser timer only widens the gaps. Cleanup waits for the writer to exit before the directory is removed, with maxRetries on the removal.

Docs

  • docs/STORE.md covers the operator wait, its cause and its limit: a lock held for 10 s, or a writer with no idle gap, still fails the command with nothing written.
  • The store README summary and CHANGELOG are updated too.

Shared helper opportunity

#1071 (auth) was still open when this was written, so the store implements the helper independently (beginImmediate in packages/store/src/database.ts). Both packages now carry the same poll loop and isBusy check. Once both land, a small beginImmediatePolling(db, waitMs) in @jimhoyd/urlcode/sqlite (packages/core/src/sqlite.ts, the existing shared SQLite helper subpath) could serve both. Core would still import neither package.

Evidence and limits

  • npm run verify is green locally on macOS, workspaces included.
  • Not verified on Windows locally, so Windows CI is the first real check.
  • The fix is probabilistic in principle. Any idle gap of about a millisecond or more is seen with overwhelming probability, but a writer that commits back to back with no gap still starves the command for the whole 10 s.

🤖 Generated with Claude Code

…erver (#1072)

SQLite's busy handler sleeps up to 100 ms between attempts and is not a
queue: beside a serving process whose commits each hold the write lock for
most of their time, with only a short idle gap between them, its ~30
attempts in 2 s could all land on a held lock and `urlcode-store members`
or `reassign` failed. addMember, removeMember, listMembers and
reassignOwner now open an operator connection that waits up to 10 s and
tries BEGIN IMMEDIATE every millisecond. Serving keeps its 2 s bound and
503; the one-server lock is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd enabled auto-merge (squash) September 30, 2026 17:42
@jimhoyd
jimhoyd merged commit ac13de4 into main Sep 30, 2026
22 checks passed
@jimhoyd
jimhoyd deleted the claude/store-cli-busy-1072 branch September 30, 2026 17:49
jimhoyd added a commit that referenced this pull request Sep 30, 2026
…uth (#1074)

#1071 (auth) and #1073 (store) each carried a copy of the same operator
connection logic: retry BEGIN IMMEDIATE every millisecond with busy_timeout 0
up to a 10 s deadline, then restore the timeout, plus a SQLITE_BUSY check.
Move it into core's @jimhoyd/urlcode/sqlite subpath as
beginImmediateWithin(db, waitMs, begin) and call it from both packages. The
error surfaced is still SQLite's own, rethrown unchanged. The identical
slow-commit writer fixtures become one test/slow-commit-writer.ts.

Part of #1041

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

urlcode-store operator commands may starve on the write lock beside a busy server (unverified)

1 participant