Repository navigation
fix(sqlitex): take the write lock at BEGIN so a second process waits - #664
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 outbusy_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/statetestTestSuiteFailsOnBrokenProviderfailed once in four local runs on Windows. That package does not use SQLite; it is a known intermittent failure, tracked separately.