Repository navigation
fix(api): begin SQLite read-write transactions with IMMEDIATE lock - #1028
Conversation
A deferred SQLite transaction that reads first and writes later cannot wait for the write lock in WAL mode: when another connection committed in between, SQLite returns SQLITE_BUSY at once and never calls the busy handler, so busy_timeout does not help. bootstrap-admin, run through kubectl exec next to a live API, hit this in e2e as 'update user: database is locked (5) (SQLITE_BUSY)' and 'migrate: seed telemetry state: insert: database is locked'. Add _txlock=immediate to the SQLite DSN unless the DSN already sets _txlock, so modernc sqlite opens read-write transactions with BEGIN IMMEDIATE and waits on busy_timeout. Read-only transactions stay deferred. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Claude <noreply@anthropic.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 19 minutes. View limit details
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. |
CI Report - 6b4dc32run 44182623181 - attempt 1 0 failed - 23 passed - 10 skipped - 33m17s elapsed Test selection: partial - per-suite table Coverageno coverage/* commit statuses found on 6b4dc32 Changed
Slowest jobs
e2e buckets run
|
withSQLiteBusyTimeout always leaves a query string, and modernc sqlite reads the first _txlock value, so appending &_txlock=immediate keeps a configured _txlock and needs no helper. Drops the untested branches the changed-lines coverage gate flagged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Claude <noreply@anthropic.com>
Summary
bootstrap-admin(run throughkubectl execnext to a live API) can fail withdatabase is locked (5) (SQLITE_BUSY)even though the SQLite DSN already setsbusy_timeout(5000). It madeTestAPI_BootstrapAdminForceEndsExistingSessions(e2e api-mods bucket) fail twice on 2026-11-17:bootstrap-admin: update user: database is locked (5) (SQLITE_BUSY)bootstrap-admin: migrate: seed telemetry state: insert: database is locked (5) (SQLITE_BUSY)Cause
Our transactions use SQLite's default
BEGIN DEFERRED. Both failing paths read first and write later (SELECT id FROM users …thenUPDATE users …, and the migration count then the telemetry seedINSERT). In WAL mode, when another connection commits between the read and the write, SQLite can't upgrade the read snapshot. It returnsSQLITE_BUSYat once and never calls the busy handler, sobusy_timeoutdoesn't help.Change
api/internal/db/db.go: afterwithSQLiteBusyTimeout(which always leaves a query string), the SQLite DSN also gets&_txlock=immediate. The driver reads the first_txlockvalue, so a_txlockalready in the configured DSN still wins. modernc sqlite v1.62.0 then opens read-write transactions withBEGIN IMMEDIATE(sqlite.go:426, tx.go:27). They take the write lock up front and wait outbusy_timeoutinstead of failing. Read-only transactions (sql.TxOptions{ReadOnly: true}) stay deferred. Within one process the pool is already capped at one connection, so nothing changes there.No test changes. The PostgreSQL path is untouched.
Verification
go build ./...andgo vet ./internal/db/pass inapi/.Generated by Claude Code