Repository navigation
fix(store): operator commands poll for the write lock beside a busy server (#1072) - #1073
Merged
Merged
Conversation
…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
enabled auto-merge (squash)
September 30, 2026 17:42
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 lockholdms per commit (synchronous=FULL,Atomics.waitinside the transaction), then idlesgapms 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 addwas run repeatedly against the database beside it:reassign, 20 ms hold, 1 ms gapOn main about one
members addin five failed (57 of 70 succeeded).membersreports the failure asThe store could not save this change(the 503 wrapping of SQLITE_BUSY).reassignreportsdatabase is locked.Fix
openStoreDatabase(path, { operator: true })is the operator connection. Its statements wait up toOPERATOR_LOCK_WAIT_MS(10 s). ItsBEGIN IMMEDIATE, for both write transactions and schema-upgrade steps, is retried withbusy_timeout=0every 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,listMembersandreassignOwneropen 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.busy_timeoutand503 storage_unavailable. The one-server lock and the operator fence are untouched.audit,backupandrestoreonly read, so WAL readers never wait on the write lock and they are unchanged.Regression test
packages/store/test/operator-lock.test.tsruns a writer with a 25 ms hold and a 1 ms gap. Beside it, it runs 15 rounds ofaddMember,reassignOwnerandremoveMemberin-process, then oneurlcode-store members addthrough the CLI. It asserts that the writer committed throughout.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
maxRetrieson the removal.Docs
docs/STORE.mdcovers 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.Shared helper opportunity
#1071 (auth) was still open when this was written, so the store implements the helper independently (
beginImmediateinpackages/store/src/database.ts). Both packages now carry the same poll loop andisBusycheck. Once both land, a smallbeginImmediatePolling(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 verifyis green locally on macOS, workspaces included.🤖 Generated with Claude Code