mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #634] [MERGED] fix(file-storage): make atomicWriteFile crash-safe and self-heal stale .bak #4707
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#4707
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/634
Author: @Romuromylus
Created: 5/10/2026
Status: ✅ Merged
Merged: 5/10/2026
Merged by: @Romuromylus
Base:
main← Head:fix/file-storage-crash-safety📝 Commits (2)
e940667fix(file-storage): make atomicWriteFile crash-safe and self-heal stale .bakd67d4f4fix(file-storage): address CodeRabbit feedback📊 Changes
1 file changed (+120 additions, -11 deletions)
View changed files
📝
packages/server/src/db/file-backed-store.ts(+120 -11)📄 Description
Linked issue
Closes #631
Why this change
A hard crash mid-write could destroy data in two compounding ways:
manifest.jsonandmanifest.json.bakcould be left filled with NUL bytes, blocking server startup until the user manually moved the corrupt files aside..bakfallback, the recovered backup could be silently weeks out of date —copyFileSyncerrors during.bakrefresh were swallowed, so failures had been quietly accumulating without anyone noticing. A real user reported losing about a week of chat messages and character edits this way after a single hard crash.Root cause is in
atomicWriteFile:.bakwas written via plaincopyFileSyncwith nofsync, in the same call that wrote the main file. NTFS allocates blocks and updates metadata immediately, but defers data writes through the cache manager — so a crash before the cache flushes leaves the file at the right size but zero-filled. And since the.bakand main writes ran back-to-back, both were simultaneously dirty in the page cache during the write window.What changed
packages/server/src/db/file-backed-store.ts:atomicWriteFileis now actually crash-safe..bakis written via the sametmp + fsync + renamepattern as the main file, so the backup is durable on disk before the main file is touched. Best-effort directoryfsyncafter the main rename. Future hard crashes lose at most the last few seconds of in-flight writes, not arbitrarily-old data..bakrefresh when main is NUL-corrupted. A first-byte check (looksNulFilled) preventsatomicWriteFilefrom copying a NUL-filled main into.bak. Without this, the heal save right after a recovery would destroy the very backup we just used to recover, opening a brief window where a second crash would lose data..bakrefresh failures now log aterrorlevel instead of being swallowed. Any failure to durably refresh the backup is a real durability regression and should be visible to operators.parseJsonFilereports staleness on fallback. When the main file is corrupt and we fall back to.bak, the log line now includes the modification-time delta between the two files (backup is 7d 4h older). Users see at startup if their recovered backup is significantly stale, instead of finding out from missing data days later.parseJsonFilerecovers from.bak, the affected manifest / table is marked dirty so the next save rewrites the corrupt main file. Previously the corrupt main file persisted on disk forever, leaving the user permanently in a state where main is junk and.bakis load-bearing — every subsequent crash would trigger the fallback again.manifest.jsonandmanifest.json.bakare unparseable at startup (the original bug pattern), log the error and continue with an empty manifest instead of crashing bootstrap. The manifest is regenerated from on-disktables/*.jsonfiles on the next save, so blocking startup over a corrupt-but-recoverable manifest is overly strict — and this also unblocks recovery for users already hit by the existing bug, not just future writes.Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
manifest.json(or anytables/*.json) to NUL bytes of the original size, then restart and confirm the server recovers from.bakwith anerror-level log line that includes a staleness delta, and that the next save rewrites both main and.bak.manifest.jsonandmanifest.json.bak. Server should log an error and continue with an empty manifest, then write a fresh one on the next save (table data unaffected).Docs and release impact
Summary by CodeRabbit
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.