fix(core): use atomic writes in storage to prevent data corruption on crash#13745
Open
vaab wants to merge 1 commit intoanomalyco:devfrom
Open
fix(core): use atomic writes in storage to prevent data corruption on crash#13745vaab wants to merge 1 commit intoanomalyco:devfrom
vaab wants to merge 1 commit intoanomalyco:devfrom
Conversation
… crash Bun.write() truncates the target file before writing. If the process crashes between truncate and write, the file ends up filled with null bytes. This corrupts session/message JSON and can make entire sessions unloadable. Replace all Bun.write() calls in storage.ts with an atomic helper that writes to a dotfile sibling then renames into place. Clean up orphaned temp files on startup.
Contributor
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found one potentially related PR: Related PR:
Why it's related: |
Collaborator
|
Are you sure you are still facing issues? We switched to sqlite so a lot of this code doesnt run anymore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Bun.write(path, data)truncates the target file before writing. If the process crashes between truncate and write, the file ends up filled with null bytes — valid size, zero content. This corrupts session/message JSON files and can make entire sessions unloadable.Fixes #7607, #9673, #10904.
Related PRs: #7734 and #11646 address the same root cause with a broader scope (CLI repair/restore, retry/backoff, fsync, quarantine). This PR is a minimal fix focused only on preventing the corruption.
See also #13032 which adds graceful degradation on corrupted reads — complementary to this fix.
The fix is the standard write-to-temp-then-rename pattern:
.basename.random.tmp) in the same directoryfs.rename()atomically to the target (atomic on same filesystem, POSIX guarantee).tmpfiles left by a previous crashDotfile prefix ensures
Bun.Glob("**/*")inStorage.list()never picks up temp files (it skips dotfiles by default).All writes in
storage.tsare covered:Storage.write(),Storage.update(), migrations, and the migration index.How did you verify your code works?
O_TRUNC+ crash patternBun.Globignores dotfiles with a manual test