Repository navigation
fix(auth): operator commands poll for the write lock beside a busy server - #1071
Merged
Merged
Conversation
…rver urlcode-auth create-user failed with "database is locked" on Windows CI beside a serving process that commits continuously. SQLite's busy handler sleeps up to 100 ms between attempts and is not a queue: when each server commit holds the write lock for most of its time (FlushFileBuffers under synchronous=FULL) and only a request's round trip separates commits, its ~30 attempts over the 2-second busy timeout can all find the lock held. The operator connection (betterAuthOptions(..., operator) -> openAuthDatabase) now takes the write lock at Better Auth's `begin` by trying BEGIN IMMEDIATE every millisecond for up to 10 seconds, and waits 10 seconds for other statements. Serving keeps its 2-second busy timeout and 503 auth_unavailable; the one-server lock is unchanged. A new test reproduces the starvation on every platform with a writer that holds the lock 25 ms per commit: it fails on main and passes with the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
jimhoyd
enabled auto-merge (squash)
September 30, 2026 17:24
…mits
On Windows the fixture's writer, which released and re-took the write lock in
one event-loop turn, won nearly every 1 ms retry and starved create-user for
the whole 10 s. A real server answers the request between two commits, so the
writer now frees the lock for a 2 ms timer after each 100 ms commit. On macOS
the test fails 7 of 7 against main's 2 s busy handler ("database is locked")
and passes 8 of 8 with the fix. The README states the no-gap limit.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Sep 30, 2026
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.
What failed
urlcode-auth create-user succeeds while a serving process commits continuouslyfailed on both attempts ofworkspace-verify (windows-latest, 24, auth)in run 36747953034 (PR #1069). The CLI exited 1 withdatabase is locked. TheERROR [Better Auth]: INTERNAL_SERVER_ERROR Error: database is lockedstack in that log comes from the earlier 503 test, where the lock is held on purpose. It is not this failure.Not a regression
PR CI doesn't run on main, so I checked the Windows auth job on every PR run since #1050. It passed every time, including #1065, #1066, #1057 and #1059, and the #1068 docs PR at 16:54 on the same base. The test took between 4.3 and 6.4 s on Windows. #1069 only changes a lock-refusal message. This is a latent race that a slow runner exposed. No merge caused it.
Root cause
The CLI already opens the database through
openAuthDatabase, with WAL, a 2 s busy timeout andBEGIN IMMEDIATE, so the pragmas were not the problem. The only write lockcreate-usertakes is theBEGIN IMMEDIATEof Better Auth's sign-up transaction. SQLite's busy handler waits on that lock by sleeping 1, 2, 5 … 100 ms between attempts, about 30 attempts in 2 s, and it is not a queue. The test's server commits a rate-limit row on every request, with only an HTTP round trip between commits. Undersynchronous=FULLon Windows, each commit holds the write lock throughFlushFileBuffers, which is slow on runner disks. If the lock is held for most of each commit, all ~30 attempts can find it held. The CLI starves and fails at 2 s. On macOS and Linux the flush is fast, so the lock is free most of the time.This affects real operators:
create-usercan fail against a saturated live site on a slow disk. The test is not unrealistic.Fix
betterAuthOptions(..., bootstrap, operator = true), which callsopenAuthDatabase(path, { operator: true }). On that connection, Better Auth'sbeginbecomesBEGIN IMMEDIATE, tried withbusy_timeout=0every millisecond for up toOPERATOR_LOCK_WAIT_MS(10 s). The busy timeout is restored afterwards. Other statements on that connection wait up to 10 s. A lock that is really held, such as a stuck process, still fails once the 10 s are up.503 auth_unavailable(not 401) and the one-server lock all behave as before.Evidence (macOS, local)
create-usercalls succeeded; 4 failed at about 2.4 s withdatabase is locked.database is lockedafter 10.3 s.urlcode-auth create-user takes the write lock between the slow commits of a saturated serverusestest/slow-commit-writer.tswith 25 ms holds. It fails on main's source (database is locked) and passed 5 of 5 runs with the fix. Unlike the Auth SQLite: WAL, busy timeout, BEGIN IMMEDIATE, database rate limit; 503 not false 401 (#927) #934 test, it reproduces on every platform.npm run verifyandnpm run verify -w packages/auth(31/31) are green.Limitations
urlcode-storeoperator commands use the same 2 s busy handler and could starve the same way. That is not verified or changed here.🤖 Generated with Claude Code