fix: share one SQLite connection across KV stores on same path (Bug #5)#53
Open
fix: share one SQLite connection across KV stores on same path (Bug #5)#53
Conversation
Why: QA bug-hunter flagged that app.js calls initKVStore twice against
the same dedup.db file — once for webhook idempotency, once for AI
rate limits. WAL tolerates multi-connection reads but two writers can
SQLITE_BUSY each other. When markProcessed and recordReview race, one
throws → the handler crashes mid-flight → markProcessed is skipped →
the same delivery is reprocessed and the AI review runs twice.
What:
- Add a module-level Map cache in persistent-kv.js keyed by absolute
dbPath. The second initKVStore call against the same file returns
a store that wraps the same Database instance.
- Ref-counted close(): the Database is only really closed when ALL
stores against that path have called close(). Tests that rely on
closing one store while another is still active now work
correctly.
- `:memory:` stays per-call. Each `:memory:` open is its own DB
by SQLite design; caching the literal string would produce
cross-store contamination in tests that use it.
Test plan:
- 4 new tests in __tests__/unit/persistent-kv.test.js:
- identity check that two stores share one Database
- mid-life close of one store leaves the other usable
- last close actually tears down the connection
- clean reopen-after-full-close round-trip
- npm test → 838 pass (was 834)
- npm run lint → clean
Risk: low — narrow, internal change. The previous test ('persists
across separate stores on the same shared db (in-memory check)') still
passes because :memory: paths bypass the cache.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Why
Two writer connections against the same SQLite file can SQLITE_BUSY each other under WAL. When that happens mid-handler, `markProcessed` gets skipped → the delivery is reprocessed → the AI review runs twice.
Behaviour
Test plan
Risk
Low. Module-level cache scoped to this file. Public API unchanged.
🤖 Generated with Claude Code