fix: atomically persist index JSON via temp-file-then-rename#31
Open
mikemolinet wants to merge 1 commit intosrobinson:mainfrom
Open
fix: atomically persist index JSON via temp-file-then-rename#31mikemolinet wants to merge 1 commit intosrobinson:mainfrom
mikemolinet wants to merge 1 commit intosrobinson:mainfrom
Conversation
Writes to .mdm/indexes/*.json and .mdm/config.json now go through a per-save unique temp file that is renamed to the final path on completion. A process killed mid-write no longer leaves a truncated JSON file that forces users to rm -rf .mdm/ and rebuild. - Refactors writeJsonFile; all save* helpers route through it, so no call-site changes are needed. - Unique temp suffix (pid + random bytes) prevents concurrent saves from colliding on a shared .tmp path. - Leftover .tmp is best-effort unlinked on failure; the original FileWriteError still propagates. - Adds tests covering the no-leftover-tmp invariant, the stale-tmp-doesn't-break-reads-or-saves invariant, and the cleanup-on-rename-failure invariant.
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.
Summary
Index JSON files (
.mdm/indexes/*.json,.mdm/config.json) are now written atomically via a per-save unique.tmpfile plusfs.rename. A process killed mid-write no longer leaves truncated JSON that surfaces asIndexCorruptedErrorand forces a fullrm -rf .mdm/+ reindex. Scope is process-kill, not OS crash.Changes
writeJsonFile(src/index/storage.ts) now writes to${path}.${pid}.${random}.tmpand renames topathon completion. On error, best-effort unlinks the tmp; the originalFileWriteErrorstill propagates.saveDocumentIndex,saveSectionIndex,saveLinkIndex,saveConfig) route throughwriteJsonFile— no call-site changes.Test plan
pnpm testpasses, including the three new tests.pnpm typecheckandpnpm formatclean.main, runmdm indexagainst a large repo, Ctrl-C during save → subsequentmdmcommands error withIndexCorruptedError. On this branch, same kill sequence → subsequentmdmcommands read the previous index cleanly.