Skip to content

fix(sqlitex): take the write lock at BEGIN so a second process waits - #664

Merged
ion-alpha-dev merged 1 commit into
mainfrom
fix/sqlite-immediate-write-tx
Nov 3, 2026
Merged

ion-alpha-dev merged 1 commit into
mainfrom
fix/sqlite-immediate-write-tx

Conversation

@ion-alpha-dev

Copy link
Copy Markdown
Collaborator

What

The SQLite write handle and the warm store now open with _txlock=immediate, so every transaction they begin takes the write lock at BEGIN. The read pool is unchanged.

Why

Two flynn processes starting on one data directory could fail with bundled: seed interface-depth: sqlite: store payload blob: database is locked (5) (SQLITE_BUSY). The live e2e kill-and-steer scenario hit this on Windows: the second process exited before its run started. The bundled skill seed's transaction reads before it writes. A deferred transaction holds only a read snapshot until its first write, and when another process has committed since that snapshot, SQLite refuses the upgrade with SQLITE_BUSY at once, without waiting out busy_timeout. With the lock taken at BEGIN, the second writer waits for it like any other lock. #652 fixed the same read-to-write upgrade for migrations; this change covers every other write transaction.

How to verify

go test -run TestTxThatReadsBeforeItWritesWaitsForAnotherWriter ./internal/sqlitex/. The test reads in one handle, lets a second handle write, then writes in the first. Without the change it fails with SQLITE_BUSY_SNAPSHOT (517); with it, both transactions commit.

Notes for reviewers

Writers were already serialised by SQLite, so taking the lock earlier only changes when a contending writer waits: at BEGIN instead of at its first write. The read pool stays deferred because it is query_only. state/statetest TestSuiteFailsOnBrokenProvider failed once in four local runs on Windows. That package does not use SQLite; it is a known intermittent failure, tracked separately.

Two flynn processes starting on one data directory could fail with
"database is locked (5)" while seeding the bundled skills. The seed's
transaction reads before it writes. A deferred transaction holds only
a read snapshot until its first write, and when another process has
committed since that snapshot SQLite refuses the upgrade with
SQLITE_BUSY at once, without waiting out busy_timeout.

The write handle and the warm store now open with _txlock=immediate,
so every transaction they begin takes the write lock at BEGIN and a
contending writer waits for it like any other lock. The read pool is
unchanged: its transactions never write.

The new test reads in one handle, lets a second handle write, then
writes in the first. It fails with SQLITE_BUSY_SNAPSHOT (517) without
the change.

Signed-off-by: Ion Alpha <[email protected]>
@codecov

codecov Bot commented Nov 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ion-alpha-dev
ion-alpha-dev merged commit f57036a into main Nov 3, 2026
34 checks passed
@ion-alpha-dev
ion-alpha-dev deleted the fix/sqlite-immediate-write-tx branch November 3, 2026 18:59
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 3, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant