Repository navigation
fix: serialize SQLite asset writes without multiplying busy waits - #362
Merged
Merged
Conversation
Deep mode fires many create_asset calls at once. UpsertAssets used a DEFERRED transaction (SELECT then INSERT) on a 25-connection pool, so writers hit SQLITE_BUSY immediately and returned "database is locked" in a few milliseconds instead of waiting. Open SQLite with _txlock=immediate and a 30s busy timeout, shrink the pool, and retry locked upserts. Add a 32-way concurrent upsert test.
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Parallel
create_assetcalls can fail immediately withdatabase is lockedbecause deferred read-modify-write transactions cannot upgrade their read snapshots to a writer. Start SQLite transactions with_txlock=immediateso competing writers wait before taking a snapshot, using the shared DSN for both application and knowledge databases.This builds on @OrdinarySF’s #329 and preserves the original authored commit. It retains the proposed 8/4 connection pool and 30-second busy timeout, but removes the outer eight-attempt retry: a persistent lock could otherwise cause roughly four minutes of waiting. SQLite handles the lock wait directly.
Regression coverage now starts 32 workers together and verifies distinct inserts, same-target deduplication and returned IDs, mixed creates/updates, and persisted counts. Both database constructors are also checked for writer reservation at Begin, concurrent WAL reads, and recovery after a competing writer releases its lock.
Validation:
go test -race ./internal/database -count=1go test ./internal/database -run 'TestUpsertAssetsConcurrent|TestAssetConcurrentCreateAndUpdate|TestSQLiteBeginReservesWriter' -count=20go test ./internal/app ./internal/handler -count=1git diff --checkFixes #328. Alternative to #329 with the retry latency tightened and broader regression coverage.