Skip to content

fix(auth): operator commands poll for the write lock beside a busy server - #1071

Merged
jimhoyd merged 3 commits into
mainfrom
claude/auth-cli-busy-windows
Sep 30, 2026
Merged

jimhoyd merged 3 commits into
mainfrom
claude/auth-cli-busy-windows

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

What failed

urlcode-auth create-user succeeds while a serving process commits continuously failed on both attempts of workspace-verify (windows-latest, 24, auth) in run 36747953034 (PR #1069). The CLI exited 1 with database is locked. The ERROR [Better Auth]: INTERNAL_SERVER_ERROR Error: database is locked stack 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 and BEGIN IMMEDIATE, so the pragmas were not the problem. The only write lock create-user takes is the BEGIN IMMEDIATE of 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. Under synchronous=FULL on Windows, each commit holds the write lock through FlushFileBuffers, 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-user can fail against a saturated live site on a slow disk. The test is not unrealistic.

Fix

  • Operator connection only. The CLI calls betterAuthOptions(..., bootstrap, operator = true), which calls openAuthDatabase(path, { operator: true }). On that connection, Better Auth's begin becomes BEGIN IMMEDIATE, tried with busy_timeout=0 every millisecond for up to OPERATOR_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.
  • Serving is unchanged. The 2 s busy timeout, 503 auth_unavailable (not 401) and the one-server lock all behave as before.
  • Docs. The README and the auth CHANGELOG are updated.

Evidence (macOS, local)

  • Script repro. A writer holds the write lock 20 ms per commit with only an event-loop turn between commits.
    • main: 1 of 5 create-user calls succeeded; 4 failed at about 2.4 s with database is locked.
    • fix: 10 of 10 succeeded, and 5 of 5 with 50 ms holds.
    • A lock held for 60 s fails with database is locked after 10.3 s.
  • New test. urlcode-auth create-user takes the write lock between the slow commits of a saturated server uses test/slow-commit-writer.ts with 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.
  • Suites. npm run verify and npm run verify -w packages/auth (31/31) are green.

Limitations

  • This is not verified on Windows locally. Windows CI on this PR is the first real check.
  • The fix is probabilistic in principle: a writer with no gap at all still starves the CLI for 10 s. But any gap between commits of a millisecond or more is now seen with overwhelming probability.
  • The urlcode-store operator 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

…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 and others added 2 commits September 30, 2026 12:27
…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]>
@jimhoyd
jimhoyd merged commit 1cc094d into main Sep 30, 2026
18 checks passed
@jimhoyd
jimhoyd deleted the claude/auth-cli-busy-windows branch September 30, 2026 17:40
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.

1 participant