[GH-ISSUE #5651] Transaction rollback erases rows lazily loaded by a concurrent read, leaving the unit marked loaded #3131

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/5651

Originally assigned to: @kolacheee on GitHub.

What happens

A transaction rollback can erase rows that a concurrent read lazily loaded into memory mid-transaction — and the unit stays marked loaded, so those persisted rows are invisible for the rest of the session. Disk is intact; a restart recovers them.

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

  • #5631 closed the write gate atomically, but reads never take the gate: matchingRows and select queries call ensureQueryScopeLoaded → ensureUnitsLoaded directly from the reader's own context.
  • The load-heal mirror (mergeLoadedRows) mirrors loaded rows into the transaction snapshot only when the loader itself runs inside the transaction's ALS context. A concurrent HTTP request's read has no such context.
  • So: a transaction mutates table T (first-mutation snapshot taken) → a concurrent request reads a non-resident unit of T between the transaction callback's awaits → the loaded rows land in the live table post-snapshot, un-mirrored → the transaction throws → rollback restores the snapshot, erasing the loaded rows — while loadedUnits and the read-once shard-encoding set still say the unit is loaded, so nothing ever reloads it.

This is the exact #5631 lazy-unit symptom via ungated reads instead of writes. It predates #5631's fix (the #5606 mirror only ever covered in-context loads).

When it matters

Needs a rollback (rare: a mid-transaction throw, realistically StorageWriterLeaseError during shutdown) racing a first-touch read of a cold chat — e.g. a user opening an old chat while a message edit's transaction fails. Low probability, invisible when it happens (the chat just looks empty until restart).

Suggested direction

The queue guarantees at most one active transaction, so the store can keep a reference to the active transaction context and have mergeLoadedRows mirror into it whenever activeTransactionCount > 0, regardless of the loader's own ALS context. (Alternative: gate out-of-context lazy loads on the same two counters the write gate now uses, at a read-latency cost.)

Found by the #5631 verification pass (adversarial review of PR TBD), which confirmed the write-side fix does not cover this path.

Originally created by @kolacheee on GitHub (Aug 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5651 Originally assigned to: @kolacheee on GitHub. **What happens** A transaction rollback can erase rows that a *concurrent read* lazily loaded into memory mid-transaction — and the unit stays marked loaded, so those persisted rows are invisible for the rest of the session. Disk is intact; a restart recovers them. **The mechanism** (all in `packages/server/src/db/file-backed-store.ts`) - #5631 closed the write gate atomically, but **reads never take the gate**: `matchingRows` and select queries call `ensureQueryScopeLoaded` → `ensureUnitsLoaded` directly from the reader's own context. - The load-heal mirror (`mergeLoadedRows`) mirrors loaded rows into the transaction snapshot only when the *loader itself* runs inside the transaction's ALS context. A concurrent HTTP request's read has no such context. - So: a transaction mutates table T (first-mutation snapshot taken) → a concurrent request reads a non-resident unit of T between the transaction callback's awaits → the loaded rows land in the live table post-snapshot, un-mirrored → the transaction throws → rollback restores the snapshot, erasing the loaded rows — while `loadedUnits` and the read-once shard-encoding set still say the unit is loaded, so nothing ever reloads it. This is the exact #5631 lazy-unit symptom via ungated reads instead of writes. It predates #5631's fix (the #5606 mirror only ever covered in-context loads). **When it matters** Needs a rollback (rare: a mid-transaction throw, realistically `StorageWriterLeaseError` during shutdown) racing a first-touch read of a cold chat — e.g. a user opening an old chat while a message edit's transaction fails. Low probability, invisible when it happens (the chat just looks empty until restart). **Suggested direction** The queue guarantees at most one active transaction, so the store can keep a reference to the active transaction context and have `mergeLoadedRows` mirror into it whenever `activeTransactionCount > 0`, regardless of the loader's own ALS context. (Alternative: gate out-of-context lazy loads on the same two counters the write gate now uses, at a read-latency cost.) Found by the #5631 verification pass (adversarial review of PR TBD), which confirmed the write-side fix does not cover this path.
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#3131
No description provided.