fix: use atomic writes for persistent JSON state files#27913
Open
vanthinh6886 wants to merge 1 commit into
Open
fix: use atomic writes for persistent JSON state files#27913vanthinh6886 wants to merge 1 commit into
vanthinh6886 wants to merge 1 commit into
Conversation
Replace direct write_text() calls with the existing atomic_json_write() helper (utils.py) which uses temp file + fsync + os.replace to prevent corruption on crash or power loss. Files fixed: - tools/skills_hub.py: SkillLockFile.save() and TapsFile.save() (installed skills registry and taps config) - gateway/sticker_cache.py: _save_cache() (sticker description cache) - tools/environments/base.py: _save_json_store() (generic JSON store used by environment backends)
Collaborator
|
Board James triage pass: this PR's |
This was referenced May 18, 2026
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.
Problem
Several files write persistent JSON state (installed skills registry, taps config, sticker cache, environment store) using
write_text()directly. If the process crashes or loses power mid-write, the file is left in a partially-written state — corrupted JSON that causesJSONDecodeErroron next read.Fix
Replace
write_text()with the existingatomic_json_write()helper fromutils.py, which uses:tempfile.mkstemp()— write to a temp filef.flush()+os.fsync()— ensure data hits diskos.replace()— atomically swap temp file into placeThis is the same pattern already used by
gateway/status.py,gateway/pairing.py, andcron/jobs.py.Files Changed
tools/skills_hub.pySkillLockFile.save()— installed skills registrytools/skills_hub.pyTapsFile.save()— skill taps configurationgateway/sticker_cache.py_save_cache()— sticker description cachetools/environments/base.py_save_json_store()— generic env backend storeBefore vs After
JSONDecodeErroron next readTests
Existing tests cover the happy path. Atomic write correctness is already validated by the
atomic_json_write()implementation and its existing test coverage intests/test_utils.py.