[GH-ISSUE #5599] Deleting a message is not serialized against an in-flight edit of the same message — the edit is silently dropped with a 404 #3099

Closed
opened 2026-10-21 05:59:36 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @kolacheee on GitHub.

What happens

Editing a message and deleting that same message are not serialized against each other. If a delete lands while an edit request is still in flight, the edit is silently dropped and the edit request answers 404 Message not found — even though the message clearly existed when the user pressed save.

Every other per-message mutation IS serialized: updateMessageContent, updateMessageExtra, appendMessageAttachment, addSwipe, setActiveSwipe, removeSwipe, and friends all run through the per-message patch queue (withPatchQueue(messageExtraPatchQueues, id, ...) in packages/server/src/services/storage/chats.storage.ts). The two deletion entry points are the exception:

  • removeMessage(id) — chats.storage.ts:2010
  • removeMessages(ids, chatId?) — chats.storage.ts:2020

Neither takes the queue, so they interleave freely with an in-flight edit of the same message.

The interleaving

updateMessageContent (chats.storage.ts:1761) reads the row, then awaits several times before its writes. If removeMessage runs to completion inside one of those gaps: the edit's db.update(messages) matches zero rows, its re-read returns null, the swipe-mirror sync is skipped, and the function returns null → the route (chats.routes.ts:2028-2030) responds 404.

When it matters

Any time an edit and a delete can race on the same message: two devices or tabs on the same chat, a batch cleanup (removeMessages) running while someone is editing, or an agent/automation deleting messages. The outcome is benign for data integrity (the message is gone either way, no corruption), but the edit is dropped without a meaningful signal — the user gets a 404 for a message they were just looking at.

Suggested direction

Wrap removeMessage and removeMessages bodies in the same per-message patch queue (for removeMessages, queue per id or acquire all ids' queues). That makes delete-vs-edit ordered like every other pair of message mutations: whichever wins the queue runs first, and the loser sees a consistent world.

How verified

Code reading on current staging (line numbers above) as part of the #5592 Phase 2 concurrency review — the patch-queue omission is visible directly: compare chats.storage.ts:1762 (edit takes the queue) with :2010/:2020 (deletes don't). Not a #5592 regression; this predates the sharded store.

Originally created by @kolacheee on GitHub (Aug 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5599 Originally assigned to: @kolacheee on GitHub. **What happens** Editing a message and deleting that same message are not serialized against each other. If a delete lands while an edit request is still in flight, the edit is silently dropped and the edit request answers `404 Message not found` — even though the message clearly existed when the user pressed save. Every other per-message mutation IS serialized: `updateMessageContent`, `updateMessageExtra`, `appendMessageAttachment`, `addSwipe`, `setActiveSwipe`, `removeSwipe`, and friends all run through the per-message patch queue (`withPatchQueue(messageExtraPatchQueues, id, ...)` in `packages/server/src/services/storage/chats.storage.ts`). The two deletion entry points are the exception: - `removeMessage(id)` — `chats.storage.ts:2010` - `removeMessages(ids, chatId?)` — `chats.storage.ts:2020` Neither takes the queue, so they interleave freely with an in-flight edit of the same message. **The interleaving** `updateMessageContent` (`chats.storage.ts:1761`) reads the row, then awaits several times before its writes. If `removeMessage` runs to completion inside one of those gaps: the edit's `db.update(messages)` matches zero rows, its re-read returns null, the swipe-mirror sync is skipped, and the function returns null → the route (`chats.routes.ts:2028-2030`) responds 404. **When it matters** Any time an edit and a delete can race on the same message: two devices or tabs on the same chat, a batch cleanup (`removeMessages`) running while someone is editing, or an agent/automation deleting messages. The outcome is benign for data integrity (the message is gone either way, no corruption), but the edit is dropped without a meaningful signal — the user gets a 404 for a message they were just looking at. **Suggested direction** Wrap `removeMessage` and `removeMessages` bodies in the same per-message patch queue (for `removeMessages`, queue per id or acquire all ids' queues). That makes delete-vs-edit ordered like every other pair of message mutations: whichever wins the queue runs first, and the loser sees a consistent world. **How verified** Code reading on current `staging` (line numbers above) as part of the #5592 Phase 2 concurrency review — the patch-queue omission is visible directly: compare `chats.storage.ts:1762` (edit takes the queue) with `:2010`/`:2020` (deletes don't). Not a #5592 regression; this predates the sharded store.
bsakaguchi 2026-10-21 05:59:36 +07:00
Author
Owner

@kolacheee commented on GitHub (Aug 29, 2026):

Taking this and #5600 together (kolacheee, agent-assisted) — same file, same concurrency family. PR incoming.

<!-- gh-comment-id:5462455918 --> @kolacheee commented on GitHub (Aug 29, 2026): Taking this and #5600 together (kolacheee, agent-assisted) — same file, same concurrency family. PR incoming.
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#3099
No description provided.