-
Notifications
You must be signed in to change notification settings - Fork 381
fix: prevent cross-session nested SQLite transactions during bootstrap #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jalehman
merged 4 commits into
Martian-Engineering:main
from
100yenadmin:fix/txn-race-bootstrap
Apr 5, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
28a0937
fix: add per-DB async transaction mutex to prevent cross-session nest…
d3591f3
[subagent] fix: address PR #261 review nits
e35611a
fix: widen shared SQLite transaction coordination
jalehman 7ab4fc1
fix: add release notes for sqlite transaction hotfix
jalehman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * Per-database async transaction mutex. | ||
| * | ||
| * Hotfix for https://github.com/Martian-Engineering/lossless-claw/issues/260 | ||
| * | ||
| * Problem: Multiple async operations (from different sessions) share one | ||
| * synchronous DatabaseSync handle. SQLite does not support nested transactions. | ||
| * When two async code paths both try to BEGIN while an earlier BEGIN is still | ||
| * in-flight (awaiting async work inside the transaction), the second BEGIN | ||
| * fails with "cannot start a transaction within a transaction". | ||
| * | ||
| * Solution: A per-database async mutex that serializes all explicit transaction | ||
| * entry points. Uses a WeakMap keyed on the DatabaseSync instance so each | ||
| * database gets its own queue, and databases are garbage-collected normally. | ||
| */ | ||
|
|
||
| import type { DatabaseSync } from "node:sqlite"; | ||
|
|
||
| interface MutexState { | ||
| /** Tail of the promise chain — each acquirer appends to this. */ | ||
| tail: Promise<void>; | ||
| } | ||
|
|
||
| const mutexMap = new WeakMap<DatabaseSync, MutexState>(); | ||
|
|
||
| function getOrCreateMutex(db: DatabaseSync): MutexState { | ||
| let state = mutexMap.get(db); | ||
| if (!state) { | ||
| state = { tail: Promise.resolve() }; | ||
| mutexMap.set(db, state); | ||
| } | ||
| return state; | ||
| } | ||
|
|
||
| /** | ||
| * Acquire exclusive async access to the database for a transaction. | ||
| * | ||
| * Usage: | ||
| * const release = await acquireTransactionLock(this.db); | ||
| * try { | ||
| * this.db.exec("BEGIN IMMEDIATE"); | ||
| * // ... do work ... | ||
| * this.db.exec("COMMIT"); | ||
| * } catch (err) { | ||
| * this.db.exec("ROLLBACK"); | ||
| * throw err; | ||
| * } finally { | ||
| * release(); | ||
| * } | ||
| * | ||
| * Returns a release function that MUST be called in a finally block. | ||
| */ | ||
| export function acquireTransactionLock(db: DatabaseSync): Promise<() => void> { | ||
| const mutex = getOrCreateMutex(db); | ||
|
|
||
| let releaseResolve: () => void; | ||
| const releasePromise = new Promise<void>((resolve) => { | ||
| releaseResolve = resolve; | ||
| }); | ||
|
|
||
| // Capture the current tail — we wait on it | ||
| const waitOn = mutex.tail; | ||
|
|
||
| // Advance the tail — next acquirer will wait on our release | ||
| mutex.tail = releasePromise; | ||
|
|
||
| // Wait for the previous holder to release, then return our release fn | ||
| return waitOn.then(() => releaseResolve!); | ||
|
100yenadmin marked this conversation as resolved.
Outdated
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.