Fix JSON file corruption in save_payload_as_json when merge=True#97
Open
jasonwbarnett wants to merge 2 commits intobuildkite:mainfrom
Open
Fix JSON file corruption in save_payload_as_json when merge=True#97jasonwbarnett wants to merge 2 commits intobuildkite:mainfrom
jasonwbarnett wants to merge 2 commits intobuildkite:mainfrom
Conversation
The file write was happening outside the FileLock when merge=True, causing JSON corruption when multiple pytest processes (e.g., from pants running tests in parallel) try to merge results into the same file concurrently. Both the read and write must be inside the lock to ensure atomicity. Move the write inside the lock block when merge=True, and use an else branch for non-merge writes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Spawns 10 threads that simultaneously call save_payload_as_json with merge=True on the same file. Verifies the result is valid JSON containing all 10 entries. This test fails with the buggy code (write outside lock) and passes with the fix (write inside lock), confirming the race condition is resolved. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.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
save_payload_as_json, whenmerge=True, the file read happens inside theFileLockbut the file write happens outside it. This creates a race condition where multiple concurrent processes can read the file, then all try to write simultaneously, resulting in JSON corruption or lost data.FileLockcontext manager whenmerge=True, ensuring the read-modify-write cycle is atomic. Non-merge writes don't need the lock and are handled in a separateelsebranch.Before (buggy)
After (fixed)
Test plan
save_jsontests pass (test_save_json_payload_without_merge,test_save_json_payload_with_merge,test_save_json_payload_with_non_existent_file,test_save_json_payload_with_invalid_file,test_save_json_payload_with_large_data)