[GH-ISSUE #5631] Transaction rollback can silently revert a plain write that raced the transaction's opening gate #3117

Closed
opened 2026-10-21 05:59:45 +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/5631

Originally assigned to: @kolacheee on GitHub.

What happens

The file store's transaction rollback can silently revert a concurrent non-transactional write that slipped through a check-then-act window. Found during adversarial review of the #5599/#5600 fixes; it predates them (the store has worked this way since transactions landed), but message edits now run in a transaction, so the window has more traffic near it.

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

  • A plain write gates on await this.waitForWritableTurn() and applies its mutation in a following microtask.
  • transaction() increments activeTransactionCount only after await previousTransaction and await this.activeFlush.
  • A writer that passes the gate while the count is still 0 can therefore apply after the transaction takes its first-mutation table snapshot (ctx.snapshots.set(tableName, currentRows.slice())).
  • If that transaction then throws, the catch path restores the whole table array — and the interleaved write vanishes from memory with no error surfaced to its caller, which already saw its insert/update succeed.

When it matters

Rollbacks are rare (a mid-transaction throw), and the interleaving window is a microtask — so this is a low-probability silent-revert, not a live data-loss report. But the failure is invisible when it does happen: e.g. a createMessage from an in-flight generation lands in the window while a message edit's transaction throws (StorageWriterLeaseError during shutdown is the realistic thrower), and the new message disappears from memory.

Suggested direction

Close the gate atomically: reserve the transaction slot (increment activeTransactionCount, or set a "transaction pending" flag that waitForWritableTurn honors) before the awaits in transaction(), so no plain write can pass the gate between slot acquisition and the snapshot. Alternatively, snapshot-on-first-mutation could verify no table generation bump occurred since the transaction began and fall back to a conservative abort.

Found by the #5599/#5600 verification pass; the reviewers verified the message-edit transaction itself is otherwise sound (rollback restores all three tables and re-pairs load-heal marks per #5606).

Originally created by @kolacheee on GitHub (Aug 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5631 Originally assigned to: @kolacheee on GitHub. **What happens** The file store's transaction rollback can silently revert a concurrent non-transactional write that slipped through a check-then-act window. Found during adversarial review of the #5599/#5600 fixes; it predates them (the store has worked this way since transactions landed), but message edits now run in a transaction, so the window has more traffic near it. **The mechanism** (all in `packages/server/src/db/file-backed-store.ts`) - A plain write gates on `await this.waitForWritableTurn()` and applies its mutation in a following microtask. - `transaction()` increments `activeTransactionCount` only *after* `await previousTransaction` and `await this.activeFlush`. - A writer that passes the gate while the count is still 0 can therefore apply *after* the transaction takes its first-mutation table snapshot (`ctx.snapshots.set(tableName, currentRows.slice())`). - If that transaction then **throws**, the catch path restores the whole table array — and the interleaved write vanishes from memory with no error surfaced to its caller, which already saw its insert/update succeed. **When it matters** Rollbacks are rare (a mid-transaction throw), and the interleaving window is a microtask — so this is a low-probability silent-revert, not a live data-loss report. But the failure is invisible when it does happen: e.g. a `createMessage` from an in-flight generation lands in the window while a message edit's transaction throws (`StorageWriterLeaseError` during shutdown is the realistic thrower), and the new message disappears from memory. **Suggested direction** Close the gate atomically: reserve the transaction slot (increment `activeTransactionCount`, or set a "transaction pending" flag that `waitForWritableTurn` honors) *before* the awaits in `transaction()`, so no plain write can pass the gate between slot acquisition and the snapshot. Alternatively, snapshot-on-first-mutation could verify no table generation bump occurred since the transaction began and fall back to a conservative abort. Found by the #5599/#5600 verification pass; the reviewers verified the message-edit transaction itself is otherwise sound (rollback restores all three tables and re-pairs load-heal marks per #5606).
bsakaguchi 2026-10-21 05:59:45 +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#3117
No description provided.