fix(server): wait for concurrent SQLite writers instead of failing with SQLITE_BUSY - #5134
fix(server): wait for concurrent SQLite writers instead of failing with SQLITE_BUSY#5134ostapondo wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughSQLite initialization now sets ChangesSQLite busy timeout
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/server/src/persistence/Layers/Sqlite.test.ts`:
- Around line 15-24: Update the lock-holder setup around lockHolderSource so it
does not require node:sqlite when tests run under Bun. Use a Bun-compatible
bun:sqlite helper for Bun, or conditionally run the existing
process.execPath/node:sqlite lock simulation only on Node while preserving the
lock behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: dcdb76e1-b225-477a-bbcf-3c56dda9454b
📒 Files selected for processing (2)
apps/server/src/persistence/Layers/Sqlite.test.tsapps/server/src/persistence/Layers/Sqlite.ts
ApprovabilityVerdict: Approved 3cc9673 This is a straightforward bug fix adding a standard SQLite You can customize Macroscope's approvability policy. Learn more. |
…th SQLITE_BUSY The CLI and server open the same state database from separate processes, but no busy_timeout was ever set, so a write that hit another process's write lock failed immediately with "database is locked" instead of waiting for the lock to clear.
e74059a to
3cc9673
Compare
Dismissing prior approval to re-evaluate 3cc9673
Fixes #5099
Concurrent writes to the state database from separate processes — e.g.
t3 auth session issuewhile a server is running, or a few CLI invocations in parallel — intermittently fail withdatabase is locked. The persistence layer never setsbusy_timeout, so SQLite gives up on a contended write lock immediately instead of waiting for it to clear.The shared
setuplayer now appliesPRAGMA busy_timeout = 5000(the valuescripts/t3-sqlite-state.tsalready uses) ahead of the WAL/foreign-keys pragmas and migrations, so it covers the server, the CLI, and both the node and bun clients through the one place every writable connection goes through.Verified:
BEGIN IMMEDIATEon the same file while the layer writes. It fails withdatabase is lockedon main and passes with the fix (a second test pins the pragma value). Stable across repeated runs.t3 auth session issueruns against one state dir failed intermittently with the exact stack from the issue (AuthSessionRepository.create→database is locked); with the fix, 24/24 runs succeeded.Scope: this only makes writers wait out short-lived lock contention; it does not change WAL or durability behavior.
Written by Claude (Fable 5) in Claude Code, directed and reviewed by @ostapondo.
Note
Low Risk
Single pragma in existing setup path; no WAL or durability changes—only retry behavior on lock contention.
Overview
Fixes intermittent
database is lockedwhen the CLI and server (or parallel CLI runs) write the same state DB: the shared SQLitesetuplayer now runsPRAGMA busy_timeout = 5000before foreign keys, WAL, and migrations, matchingscripts/t3-sqlite-state.tsso every writable connection waits briefly instead of failing immediately.New tests spawn a child that holds
BEGIN IMMEDIATEwhile the layer inserts, and assertbusy_timeoutis 5000 on the in-memory persistence layer.Reviewed by Cursor Bugbot for commit 3cc9673. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Fix SQLite busy errors by setting
PRAGMA busy_timeoutto 5000msSets
PRAGMA busy_timeout = 5000in the SQLite connection setup so that statements wait up to 5 seconds for a write lock instead of immediately failing withSQLITE_BUSY. The pragma is applied in Sqlite.ts before foreign key enforcement and WAL/migration setup. Tests in Sqlite.test.ts verify both the wait behavior with a concurrent writer and that the timeout value is correctly applied.Macroscope summarized 3cc9673.
Summary by CodeRabbit
Bug Fixes
Tests