mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #5632] [MERGED] fix(storage): serialize message deletes against edits and make the edit's mirror write atomic #6856
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#6856
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5632
Author: @kolacheee
Created: 8/29/2026
Status: ✅ Merged
Merged: 8/29/2026
Merged by: @kolacheee
Base:
staging← Head:fix/5599-5600-edit-delete-races📝 Commits (2)
eaebdd6fix(storage): serialize message deletes against edits and make the edit's mirror write atomic591f661test: make the race regressions deterministic per adversarial review📊 Changes
2 files changed (+325 additions, -50 deletions)
View changed files
📝
packages/server/src/services/storage/chats.storage.ts(+110 -50)➕
scripts/regressions/message-edit-delete-races.regression.ts(+215 -0)📄 Description
Closes #5599. Closes #5600. The two concurrency findings from the #5592 Phase 2 review, fixed together — same file, same family.
#5599 — deletes now take the per-message patch queue
removeMessageandremoveMessageswere the only per-message mutations not serialized on the per-message patch queue, so a delete landing inside an in-flight edit's await gaps silently dropped the edit into a404 Message not found. Both now take the queue. The bulk path gets a newwithPatchQueuesmulti-acquire: one shared gate is installed on every affected id's queue synchronously before any await, so two concurrent multi-acquires order themselves strictly and a cycle cannot form (each 500-id chunk acquires and releases independently — cross-chunk atomicity was never promised).#5600 — the edit's two writes are now one transaction
updateMessageContentwrote the messages row and its active-swipe mirror with awaited gaps between them and no transaction, so a crash — or just the 750 ms debounce flush landing in the window — persisted the edit on the message while the swipe kept the pre-edit text. Invisible in the live chat (the UI reads the message row, and swipe switching heals the pair), but the stale text survived into exports and branched chats. The messages-row write, memory-chunk invalidation, and swipe-mirror write now run inside onedb.transaction; the store defers flushes while a transaction is active, so a mid-edit flush produces a consistent pair on disk. The store's transaction is ambient (AsyncLocalStorage), joins nested transactions, andmessages/message_swipesare not durable-on-commit tables — so no extra synchronous disk flush lands on the hot edit path.Adversarial verification (two independent reviewers ran before this PR opened)
They confirmed the fixes sound — reentrancy ruled out for every call path, the multi-acquire cycle-free, rollback restoring all three tables with the #5606 load-heal pairing intact — and caught three things that materially improved this PR:
db.transactioncallback that calls a queue-taking message API would deadlock the store (no live path exists today — they checked all 21 transaction call sites). Mitigated the way the repo already handles its metadata-queue ordering: the lock order is now documented onwithPatchQueue, and it's stated here for reviewers.One reviewer concern analyzed and accepted with a correction: "edits now stall behind long transactions like profile imports." Pre-change, every message write already waited out active transactions at the store's writable gate — the stall is not new; what's new is only that short edit-transactions serialize among themselves, which is negligible.
Validation done
message-edit-delete-races— four blocks (queue-hold for single and bulk delete, mid-edit delete injection, mid-edit flush tear), each A/B-verified red against pre-fix codelazy-chat-units,message-sharding(lazy + eager),autonomous-check-storage,open-issuesManually verify
Summary by CodeRabbit
Bug Fixes
Tests
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.