Skip to content

fix(api): begin SQLite read-write transactions with IMMEDIATE lock - #1028

Merged
ValgulNecron merged 3 commits into
masterfrom
fix/sqlite-immediate-txlock
Nov 17, 2026
Merged

ValgulNecron merged 3 commits into
masterfrom
fix/sqlite-immediate-txlock

Conversation

@ValgulNecron

@ValgulNecron ValgulNecron commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

bootstrap-admin (run through kubectl exec next to a live API) can fail with database is locked (5) (SQLITE_BUSY) even though the SQLite DSN already sets busy_timeout(5000). It made TestAPI_BootstrapAdminForceEndsExistingSessions (e2e api-mods bucket) fail twice on 2026-11-17:

Cause

Our transactions use SQLite's default BEGIN DEFERRED. Both failing paths read first and write later (SELECT id FROM users … then UPDATE users …, and the migration count then the telemetry seed INSERT). In WAL mode, when another connection commits between the read and the write, SQLite can't upgrade the read snapshot. It returns SQLITE_BUSY at once and never calls the busy handler, so busy_timeout doesn't help.

Change

api/internal/db/db.go: after withSQLiteBusyTimeout (which always leaves a query string), the SQLite DSN also gets &_txlock=immediate. The driver reads the first _txlock value, so a _txlock already in the configured DSN still wins. modernc sqlite v1.62.0 then opens read-write transactions with BEGIN IMMEDIATE (sqlite.go:426, tx.go:27). They take the write lock up front and wait out busy_timeout instead 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 ./... and go vet ./internal/db/ pass in api/.
  • CI on this branch is the check, including the e2e api-mods bucket.

Generated by Claude Code

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>
@ValgulNecron ValgulNecron added area: api REST/WebSocket gateway type: fix Bug fix labels Nov 17, 2026 — with Claude
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between ff076d7 and 6b4dc32.

Files selected for processing: 2

  • api/internal/db/db.go
  • changelog/unreleased/1028-sqlite-bootstrap-admin-lock.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@ValgulNecron ValgulNecron self-assigned this Nov 17, 2026
@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

CI Report - 6b4dc32

run 44182623181 - attempt 1

0 failed - 23 passed - 10 skipped - 33m17s elapsed

Test selection: partial - per-suite table

Coverage

no coverage/* commit statuses found on 6b4dc32

Changed

Area Files +/-
api/ 1 +8/-0
changelog/ 1 +6/-0

Slowest jobs

Job Duration
e2e telemetry / arm64 (kind) 24m28s
e2e telemetry / amd64 (kind) 22m44s
e2e postgres / amd64 (kind) 19m13s
e2e restricted-workloads / arm64 (kind) 14m49s
e2e restricted-workloads / amd64 (kind) 13m49s

e2e buckets run

api-agent, api-auth, api-mods, api-rbac, api-roles, backup-preparation, bot-fast, management-placement, monitoring, multicluster, network-isolation, operator, optional-components, postgres-availability, postgres-ha, postgres-pitr, ratelimit, recovery, restricted-workloads, telemetry, upgrade, wake-hostport

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>
@ValgulNecron
ValgulNecron merged commit 6909740 into master Nov 17, 2026
67 checks passed
@ValgulNecron
ValgulNecron deleted the fix/sqlite-immediate-txlock branch November 22, 2026 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: api REST/WebSocket gateway type: fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants