Repository navigation
fix(data): reject destructive JSON store shrink - #1461
mikemikimike wants to merge 8 commits into
Conversation
fbe6bf8 to
4b3fd75
Compare
keepIfShrinking exists because the wallet was destroyed by two components each saving their own stale view over the other's — so when a write is about to halve a store, the old contents are copied to .prev first. Every failure on the way to that copy returned silently and let the write through, which is the one case where the guard was needed and the one case it did nothing: the store shrank, the copy was not there, and nothing said so. It refuses the write now. Not being able to take the safety copy is exactly when not to do the dangerous thing, and a caller that gets an error can be told; a caller whose data is gone cannot. And the check is serialised with the write. Stat, read, copy, replace was four steps with no lock, so two concurrent shrinking writers could interleave and the copy could be of a file the other had already replaced — the .prev being written to protect against a lost store would hold the lost store. The existing test only asserted the copy was kept; it now also asserts the write that triggered it actually landed, so "preserve the old contents" cannot quietly become "refuse everything". Co-authored-by: mikemikimike <186855910+mikemikimike@users.noreply.github.com> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P2r4ca9UPPf7FDk7y8eJLr
|
Merged as a345e79 — thank you, this is a good catch. The argument that convinced me is that every failure on the way to the copy returned silently and let the write through, so the guard did nothing in exactly the case it exists for: the store shrank, The Carried in by hand rather than through the merge button, because the branch was several days behind and I wanted to run the full suite against current main — your commit is credited as co-author. Two notes, neither blocking:
Generated by Claude Code |
|
@mikemikimike — thank you for your contribution to Mu. We're requesting your explicit permission to relicense your JSON-store shrink-safety contribution in #1461, incorporated in commit a345e79, including the portions retained or modified in later Mu versions. The proposal is in #2180: future Mu runtime releases would be source-available, free for personal/internal use, with a paid licence required for commercial resale or hosting. Micro would also offer separate proprietary commercial licences. MUCP and the SDKs remain permissively licensed. Existing AGPL grants would remain valid. If you agree, please reply with the following confirmation (identifying the rights-holding entity if you are acting for one):
This request concerns only the identified contribution, not future contributions. Attribution will be preserved. If any portion belongs to an employer, co-author or another party and you cannot grant permission for it, please identify that limitation instead. If you would prefer a separate agreement or do not agree, please say so; silence will not be treated as consent. For reference, the current draft is Mu Runtime Licence draft 1.0. |
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.