Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): wait for concurrent SQLite writers instead of failing with SQLITE_BUSY #5134

Mergedostapondo(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/5134/headmerged by juliusmarminge View on GitHub

Fixes #5099

Concurrent writes to the state database from separate processes — e.g. t3 auth session issue while a server is running, or a few CLI invocations in parallel — intermittently fail with database is locked. The persistence layer never sets busy_timeout, so SQLite gives up on a contended write lock immediately instead of waiting for it to clear.

The shared setup layer now applies PRAGMA busy_timeout = 5000 (the value scripts/t3-sqlite-state.ts already 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:

  • New test reproduces the failure cross-process: a child process holds BEGIN IMMEDIATE on the same file while the layer writes. It fails with database is locked on main and passes with the fix (a second test pins the pragma value). Stable across repeated runs.
  • The repro from the issue: on main, parallel t3 auth session issue runs 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.


Open in Devin Review

Note

Low Risk Localized persistence tuning with no auth or schema changes; writers may block briefly under contention but behavior otherwise matches prior WAL/durability setup.

Overview Fixes intermittent database is locked / SQLITE_BUSY when the server and CLI (or parallel CLI runs) write the same state DB by having every writable connection wait up to 5 seconds for a lock instead of failing immediately.

The shared SQLite setup layer in Sqlite.ts now runs PRAGMA busy_timeout = 5000 before foreign keys, WAL, and migrations, so server, CLI, and in-memory test layers all pick it up from one place.

Sqlite.test.ts adds a cross-process repro (child holds BEGIN IMMEDIATE while the layer inserts) and asserts the pragma is 5000 via SqlitePersistenceMemory.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit dd2121d618791ade409313345c0da48308fefcbe. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Fix SQLite busy_timeout to wait up to 5s instead of failing with SQLITE_BUSY

Sets PRAGMA busy_timeout = 5000 in the SQLite layer setup in Sqlite.ts, applied before enabling foreign keys and WAL mode. Adds tests in Sqlite.test.ts that verify the pragma value and confirm concurrent write locks are waited out rather than causing immediate failures.

  • Behavioral Change: SQLite operations that previously failed immediately with SQLITE_BUSY under write contention now block for up to 5 seconds before failing.
Macroscope summarized dd2121d.

Summary by CodeRabbit

  • Bug Fixes

    • Improved SQLite handling for concurrent writes.
    • Write operations now wait up to five seconds when the database is temporarily locked, reducing immediate failures during simultaneous CLI and server activity.
  • Tests

    • Added coverage for concurrent write handling and the shared database lock timeout configuration.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      📝 Walkthrough

      Walkthrough

      SQLite initialization now sets PRAGMA busy_timeout = 5000 before other pragmas. Tests cover concurrent writer handling and verify the timeout value for shared in-memory persistence.

      Changes

      SQLite busy timeout

      Layer / File(s)Summary
      Configure SQLite busy timeout
      apps/server/src/persistence/Layers/Sqlite.ts
      SQLite setup applies a five-second busy timeout before enabling WAL and foreign keys.
      Validate concurrent writer behavior
      apps/server/src/persistence/Layers/Sqlite.test.ts
      Tests hold a write lock in a child process, verify that another insert waits successfully, clean up the temporary database, and assert a 5000 millisecond timeout for shared in-memory persistence.

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Suggested reviewers: juliusmarminge

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Linked Issues check✅ PassedThe PR adds the requested SQLite busy timeout and tests the concurrent writer behavior described in issue #5099.
      Out of Scope Changes check✅ PassedThe changes are limited to the shared SQLite setup and focused persistence tests related to issue #5099.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Title check✅ PassedThe title clearly and concisely identifies the SQLite concurrency fix and its primary user-visible behavior.
      Description check✅ PassedThe description explains the change, motivation, testing, scope, and UI applicability, although it does not reproduce the template headings or checklist.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      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.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      ✅ Action performed

      Review finished.

      Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

    4. 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

      📥 Commits

      Reviewing files that changed from the base of the PR and between a041981276b4c789fed8132e3b8a320749bf25e8 and 0b0f6d33b56b1a722ba80539f416ec00d64c4ec2.

      📒 Files selected for processing (2)
      • apps/server/src/persistence/Layers/Sqlite.test.ts
      • apps/server/src/persistence/Layers/Sqlite.ts
    5. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Approved dd2121d

      This PR adds a single SQLite PRAGMA setting to configure busy_timeout, preventing SQLITE_BUSY errors when CLI and server write concurrently. The change is minimal, follows existing patterns, and includes thorough tests.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    6. juliusmarminge merged thisc0f9d91

    7. juliusmarminge closed this

Sign in to join this conversation.