fix(data): reject destructive JSON store shrink - #1461
Open
mikemikimike wants to merge 4 commits into
Open
Conversation
mikemikimike
force-pushed
the
fix/protect-json-store-shrinks
branch
from
August 23, 2026 16:44
fbe6bf8 to
4b3fd75
Compare
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.
Related issue
Related to #1458
Background
A whole-file JSON store can be overwritten by a stale or partial in-memory view. The existing shrink check preserved a
.prevcopy but still replaced the live store, so the running instance could immediately lose the data it needed.Changes
Compatibility
Normal writes and legitimate changes below the shrink threshold are unchanged. Callers that attempt an unexpectedly destructive JSON write now receive an error and must repair or explicitly handle the operation.
The mutex protects concurrent writers in one process. Cross-process writers and equal-sized stale snapshots would require a file-lock or versioned compare-and-swap protocol and are outside this focused change.
Verification
GOPROXY=https://goproxy.cn,direct go test ./internal/data -run 'TestAWriteThatLosesMostOfAStoreKeepsACopy|TestSaveJSONRejectsShrinkWhenPreviousCopyCannotBeWritten|TestSaveJSONPreservesPreviousContentsOnMarshalFailure|TestSaveJSONRoundTripLeavesNoTempFiles' -count=1— passed.gofmt -w internal/data/data.go internal/data/atomic_test.go— passed.git diff --check— passed.GOPROXY=https://goproxy.cn,direct go test ./internal/data -count=1— not green on Windows because existing tests expect Unix file modes (0600) and SQLite cleanup encounters a locked file.GOPROXY=https://goproxy.cn,direct go test ./...— not green on Windows due existing platform-specificsyscall.Statfs_t/syscall.Statfsbuild errors, file-lock failures, permission-mode failures, and proxy environment assumptions.No Docker service is required for this unit-level change.