Skip to content

fix: set PRAGMA busy_timeout=5000 to handle concurrent writers#5

Merged
dzhng merged 1 commit intomainfrom
fix/schema-migrations-and-busy-retry
May 2, 2026
Merged

fix: set PRAGMA busy_timeout=5000 to handle concurrent writers#5
dzhng merged 1 commit intomainfrom
fix/schema-migrations-and-busy-retry

Conversation

@dzhng
Copy link
Copy Markdown
Owner

@dzhng dzhng commented May 2, 2026

Repro

SQLite is single-writer. Without busy_timeout, parallel write calls under load surface SQLITE_BUSY directly:

DrizzleQueryError: Failed query: insert into "contacts" ...
cause: SqliteError: database is locked

Easy to reproduce — 30 parallel crm contact add via xargs -P10 produces ~2/30 failures consistently. Same pattern hits an agent that batches writes or has a daemon + CLI running concurrently against the same DB.

Fix

Set PRAGMA busy_timeout=5000 in openDB. SQLite handles retry/wait internally; concurrent writers block on the lock for up to 5s instead of erroring immediately. 5s is enough to ride out any realistic burst.

Test

Added test/db-busy-timeout.test.ts: spawns 40 parallel crm contact add processes and asserts all 40 succeed with the contact list ending up at length 40.

  • Without the PRAGMA: 4–6 failures in the run, test fails.
  • With the PRAGMA: 0 failures, test passes.

Verified by toggling the PRAGMA off and rerunning. bun run check-types clean. bun run build clean.

🤖 Generated with Claude Code

SQLite is single-writer. Without busy_timeout, parallel `crm contact
add` calls under load (e.g. xargs -P or several agent CLIs writing at
once) immediately surface SQLITE_BUSY:

  DrizzleQueryError: Failed query: insert into "contacts" ...
  cause: SqliteError: database is locked

5s is enough to ride out any realistic burst. Verified: 40 parallel
adds drop from ~4 failures to 0.
@dzhng dzhng merged commit cd1025c into main May 2, 2026
1 check failed
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