[GH-ISSUE #5606] Transaction rollback strands a stale-file mark without its paired dirty keys — the next flush can erase a misfiled row's only copy #3107

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

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

Originally assigned to: @kolacheee on GitHub.

What happens

A transaction rollback can erase a misfiled row's only on-disk copy. If a chat's shard file contains a row that belongs to a different chat (a stray — the state the store's self-healing exists to fix), and that chat's unit loads inside a transaction that later rolls back, the next flush rewrites the host file canonically (without the stray) while never writing the stray's own shard. The row then exists only in memory; a restart loses it.

Verified on current staging (d660fffbf) with the lazy store as merged in #5597 — this is not an eviction (#5603) regression; it reproduces identically with eviction disabled.

Why

Loading a shard that holds foreign rows creates two paired marks in the same synchronous block (loadShardFileSync in packages/server/src/db/file-backed-store.ts): a stale-file mark for the host file (so the flush rewrites it canonically) and dirty keys for every row's real shard (so the strays get written to their own files first). The flush's write-then-unlink ordering is only safe because those two always travel together.

Transaction rollback breaks the pairing: the rollback path restores dirtyShards from the pre-transaction snapshot (erasing the load's dirty keys) but does not touch staleShardFiles — the stale mark survives alone. On the next flush, the host file is rewritten canonically without the stray, and the stray's own shard is skipped by the clean-shard fast path (!effectiveDirty.has(key) && known.has(encoded)) because its key is no longer dirty and its file already exists on disk.

Try it yourself (repro shape; a regression-style script against createFileNativeDB shows it directly)

  1. Seed tables/messages/chat-a.json with one chat-a row plus a row whose chatId is chat-b; seed tables/messages/chat-b.json with a different chat-b row (the file must already exist — if it doesn't, the flush's recreate-if-missing branch happens to rescue the row).
  2. Open a db.transaction, run any query that touches chat-a (the unit loads mid-transaction, healing marks are created), then throw to force a rollback.
  3. flush, restart, and list chat-b's messages: the misfiled row is gone from disk everywhere.

Suggested direction

Load-created healing marks are not transaction mutations and should survive a rollback the same way the loaded rows themselves do (the row arrays are deliberately mirrored into the transaction snapshot for exactly this reason). Either re-create the load's dirty keys on rollback (track them per-transaction alongside the snapshot), or make the rollback restore staleShardFiles and dirtyShards as a consistent pair — the invariant to preserve is simply "a stale mark never reaches a flush without the dirty keys it was created with."

How found

Adversarial verification of #5603 (durability lens, probe on both HEAD and the parent commit). Related machinery context in #5592.

Originally created by @kolacheee on GitHub (Aug 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5606 Originally assigned to: @kolacheee on GitHub. **What happens** A transaction rollback can erase a misfiled row's only on-disk copy. If a chat's shard file contains a row that belongs to a *different* chat (a stray — the state the store's self-healing exists to fix), and that chat's unit loads **inside a transaction that later rolls back**, the next flush rewrites the host file canonically (without the stray) while never writing the stray's own shard. The row then exists only in memory; a restart loses it. Verified on current `staging` (`d660fffbf`) with the lazy store as merged in #5597 — this is **not** an eviction (#5603) regression; it reproduces identically with eviction disabled. **Why** Loading a shard that holds foreign rows creates two paired marks in the same synchronous block (`loadShardFileSync` in `packages/server/src/db/file-backed-store.ts`): a **stale-file mark** for the host file (so the flush rewrites it canonically) and **dirty keys** for every row's real shard (so the strays get written to their own files first). The flush's write-then-unlink ordering is only safe because those two always travel together. Transaction rollback breaks the pairing: the rollback path restores `dirtyShards` from the pre-transaction snapshot (erasing the load's dirty keys) but does **not** touch `staleShardFiles` — the stale mark survives alone. On the next flush, the host file is rewritten canonically without the stray, and the stray's own shard is skipped by the clean-shard fast path (`!effectiveDirty.has(key) && known.has(encoded)`) because its key is no longer dirty and its file already exists on disk. **Try it yourself** (repro shape; a regression-style script against `createFileNativeDB` shows it directly) 1. Seed `tables/messages/chat-a.json` with one chat-a row **plus** a row whose `chatId` is `chat-b`; seed `tables/messages/chat-b.json` with a different chat-b row (the file must already exist — if it doesn't, the flush's recreate-if-missing branch happens to rescue the row). 2. Open a `db.transaction`, run any query that touches chat-a (the unit loads mid-transaction, healing marks are created), then throw to force a rollback. 3. `flush`, restart, and list chat-b's messages: the misfiled row is gone from disk everywhere. **Suggested direction** Load-created healing marks are not transaction mutations and should survive a rollback the same way the loaded rows themselves do (the row arrays are deliberately mirrored into the transaction snapshot for exactly this reason). Either re-create the load's dirty keys on rollback (track them per-transaction alongside the snapshot), or make the rollback restore `staleShardFiles` and `dirtyShards` as a consistent pair — the invariant to preserve is simply "a stale mark never reaches a flush without the dirty keys it was created with." **How found** Adversarial verification of #5603 (durability lens, probe on both HEAD and the parent commit). Related machinery context in #5592.
bsakaguchi 2026-10-21 05:59:39 +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#3107
No description provided.