[GH-ISSUE #5652] Check-once activeFlush await lets a second flush run concurrently with a transaction and persist uncommitted rows #3130

Closed
opened 2026-10-21 05:59:51 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kolacheee on GitHub (Aug 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5652

Originally assigned to: @kolacheee on GitHub.

What happens

A narrow pre-existing window lets a flush run concurrently with a transaction callback and persist uncommitted rows to disk with no dirty mark left behind. Verified byte-identical before and after the #5631 gate fix — the fix neither widened nor narrowed it; filing so it isn't lost.

The mechanism (packages/server/src/db/file-backed-store.ts)

transaction()'s opening if (this.activeFlush) await this.activeFlush; is check-once, not a loop. The crack needs a double-flush ordering:

  1. A transaction parks on await this.activeFlush (flush 1) while a second flush() call is also parked on the same promise, subscribed first.
  2. Flush 1 resolves → flush 2's recursion re-enters first, sees no active transaction (the parked transaction hasn't resumed yet), captures the dirty set, and sets a NEW activeFlush.
  3. The transaction's continuation resumes past its already-consumed check without re-checking and runs its callback concurrently with flush 2's I/O.
  4. saveFileSnapshots reads this.tables live during writes, so a table in flush 2's captured dirty set that the transaction mutates early can be persisted with uncommitted rows. A rollback then restores memory and the PRE-transaction dirty flags, leaving the uncommitted rows on disk with no dirty mark until some later write happens to dirty that shard again.

When it matters

Requires two overlapping flushes plus a transaction landing between them plus a rollback — rare, but the failure is persisted uncommitted data, the worst kind to leave unfixed silently.

Suggested fix

One line: make the opening check a loop — while (this.activeFlush) await this.activeFlush; — so the transaction re-checks after every flush completion and can never activate concurrently with a fresh flush's capture. (Worth a small staged regression via the existing beforeTableWrite test hook, which can hold flush 2 mid-write.)

Found by the #5631 verification pass while auditing the gate fix's interaction with flush parking.

Originally created by @kolacheee on GitHub (Aug 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5652 Originally assigned to: @kolacheee on GitHub. **What happens** A narrow pre-existing window lets a flush run concurrently with a transaction callback and persist uncommitted rows to disk with no dirty mark left behind. Verified byte-identical before and after the #5631 gate fix — the fix neither widened nor narrowed it; filing so it isn't lost. **The mechanism** (`packages/server/src/db/file-backed-store.ts`) `transaction()`'s opening `if (this.activeFlush) await this.activeFlush;` is check-once, not a loop. The crack needs a double-flush ordering: 1. A transaction parks on `await this.activeFlush` (flush 1) while a second `flush()` call is also parked on the same promise, subscribed first. 2. Flush 1 resolves → flush 2's recursion re-enters first, sees no active transaction (the parked transaction hasn't resumed yet), captures the dirty set, and sets a NEW `activeFlush`. 3. The transaction's continuation resumes past its already-consumed check without re-checking and runs its callback concurrently with flush 2's I/O. 4. `saveFileSnapshots` reads `this.tables` live during writes, so a table in flush 2's captured dirty set that the transaction mutates early can be persisted with uncommitted rows. A rollback then restores memory and the PRE-transaction dirty flags, leaving the uncommitted rows on disk with no dirty mark until some later write happens to dirty that shard again. **When it matters** Requires two overlapping flushes plus a transaction landing between them plus a rollback — rare, but the failure is persisted uncommitted data, the worst kind to leave unfixed silently. **Suggested fix** One line: make the opening check a loop — `while (this.activeFlush) await this.activeFlush;` — so the transaction re-checks after every flush completion and can never activate concurrently with a fresh flush's capture. (Worth a small staged regression via the existing `beforeTableWrite` test hook, which can hold flush 2 mid-write.) Found by the #5631 verification pass while auditing the gate fix's interaction with flush parking.
bsakaguchi 2026-10-21 05:59:51 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#3130
No description provided.