[codex] Use atomic write in _update_upload_status to prevent file corruption - #350
Open
rahulrao85 wants to merge 1 commit into
Open
[codex] Use atomic write in _update_upload_status to prevent file corruption#350rahulrao85 wants to merge 1 commit into
rahulrao85 wants to merge 1 commit into
Conversation
…file corruption The _update_upload_status function used seek(0) -> json.dump -> truncate() to rewrite the session JSON file in-place. If json.dump raised an exception mid-write (e.g. serialization error), the file was left with a corrupted mix of old and new data at a wrong truncation point. Replace with a temp-file + os.replace pattern: the full JSON is written to a .tmp sibling file first, then atomically swapped into place. On failure the .tmp file is cleaned up and the original file remains untouched. Signed-off-by: Rahul Rao <rahulrao85@gmail.com>
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
Replace the
seek(0) → json.dump → truncate()in-place rewrite pattern in_update_upload_status()with a temp-file +os.replace()atomic write pattern.Problem
The previous implementation used:
If
json.dump()raised an exception mid-write (e.g. serialization error for a non-serializable value), the file was left with partially-overwritten data at a wrong truncation position — corrupting the session JSON file permanently. Both the org and personal uploaders use this same function, so a crash in either process would corrupt the shared session file.Fix
.tmpsibling file usingjson.dumpos.replace(tmp_path, session_file)If
json.dumpcrashes, the.tmpfile is cleaned up and the originalsession_fileremains untouched.os.replace()is atomic on POSIX and modern Windows (NTFS atomic rename), so readers always see a consistent file.Validation
os.replaceis used instead ofshutil.movefor cross-filesystem safety (ensures atomic rename when source and dest are on the same filesystem)exceptblock prevents orphaned.tmpfiles