[GH-ISSUE #5600] Message edit writes the messages row and its swipe mirror non-atomically — a badly timed crash leaves pre-edit text in exports and branches #3102

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

Originally assigned to: @kolacheee on GitHub.

What happens

Editing a message writes the new text twice — once to the messages row and once to the active message_swipes row (the mirror that makes edits survive swipe switching). These two writes are not atomic: in updateMessageContent (packages/server/src/services/storage/chats.storage.ts:1761-1810) the messages-row update (:1782) and the swipe-row update (:1805) are separated by several awaited calls (memory-chunk invalidation, a re-read, a swipe list read), and nothing wraps the sequence in db.transaction.

If the server dies in that window — or a debounced flush happens to run between the two writes and the process dies before the next flush — the edit persists on the messages row while the active swipe row still holds the pre-edit text.

What a user actually sees

The chat itself looks fine after restart (the UI renders the messages row, and switching swipes away copies the current message content back onto the outgoing swipe, which heals the pair). The stale text survives in the paths that read swipe rows directly:

  • Chat export: the exported swipe carries the pre-edit text while the message carries the edited text.
  • Branching a chat: the branch copies the swipe rows, so the stale text rides along into the new chat.

So the symptom is "my export/branch has an old version of a message I know I edited" — confusing precisely because the live chat shows the edit.

There's also a transient, milliseconds-scale version with no crash needed: a concurrent read between the two writes sees the message row and its active swipe disagree. Mostly invisible today since the UI renders message content, but any consumer comparing the two can observe it.

Suggested direction

Wrap the messages-row update and the swipe-mirror update in one db.transaction (the store already supports it, defers flushes while a transaction is active, and rolls both back together on failure). That closes the crash/flush-timing tear; the transient read window shrinks to nothing user-visible.

How verified

Code reading on current staging (line numbers above) as part of the #5592 Phase 2 concurrency review: no db.transaction in updateMessageContent, awaited gaps between :1782 and :1805, and setActiveSwipe's copy-back at :2116-2122 is what hides the tear in the live UI. Not a #5592 regression; the two-write mirror predates the sharded store.

Originally created by @kolacheee on GitHub (Aug 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5600 Originally assigned to: @kolacheee on GitHub. **What happens** Editing a message writes the new text twice — once to the `messages` row and once to the active `message_swipes` row (the mirror that makes edits survive swipe switching). These two writes are not atomic: in `updateMessageContent` (`packages/server/src/services/storage/chats.storage.ts:1761-1810`) the messages-row update (`:1782`) and the swipe-row update (`:1805`) are separated by several awaited calls (memory-chunk invalidation, a re-read, a swipe list read), and nothing wraps the sequence in `db.transaction`. If the server dies in that window — or a debounced flush happens to run between the two writes and the process dies before the next flush — the edit persists on the `messages` row while the active swipe row still holds the pre-edit text. **What a user actually sees** The chat itself looks fine after restart (the UI renders the messages row, and switching swipes away copies the current message content back onto the outgoing swipe, which heals the pair). The stale text survives in the paths that read swipe rows directly: - **Chat export**: the exported swipe carries the pre-edit text while the message carries the edited text. - **Branching a chat**: the branch copies the swipe rows, so the stale text rides along into the new chat. So the symptom is "my export/branch has an old version of a message I know I edited" — confusing precisely because the live chat shows the edit. There's also a transient, milliseconds-scale version with no crash needed: a concurrent read between the two writes sees the message row and its active swipe disagree. Mostly invisible today since the UI renders message content, but any consumer comparing the two can observe it. **Suggested direction** Wrap the messages-row update and the swipe-mirror update in one `db.transaction` (the store already supports it, defers flushes while a transaction is active, and rolls both back together on failure). That closes the crash/flush-timing tear; the transient read window shrinks to nothing user-visible. **How verified** Code reading on current `staging` (line numbers above) as part of the #5592 Phase 2 concurrency review: no `db.transaction` in `updateMessageContent`, awaited gaps between `:1782` and `:1805`, and `setActiveSwipe`'s copy-back at `:2116-2122` is what hides the tear in the live UI. Not a #5592 regression; the two-write mirror predates the sharded store.
bsakaguchi 2026-10-21 05:59:38 +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#3102
No description provided.