fix(fileops): remove temp file when atomic_write_file fails#215
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #215 +/- ##
=======================================
Coverage 86.43% 86.44%
=======================================
Files 159 159
Lines 8832 8838 +6
=======================================
+ Hits 7634 7640 +6
Misses 1198 1198 ☔ View full report in Codecov by Harness. |
56fbf69 to
a0d7837
Compare
atomic_write_file created a mkstemp temp file, wrote to it, then move()'d it into place. If the write or the move raised, the temp file was left littering the destination directory. Wrap write+move in try/except and unlink the temp on any failure before re-raising (on a successful move it is already gone).
a0d7837 to
5708fda
Compare
mimi1vx
approved these changes
Jun 23, 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.
atomic_write_filecreates amkstemptemp file, writes to it, thenmoves itinto place:
If the write or the
moveraises (e.g. a permission or I/O error on thedestination), the temp file is left behind in the destination directory. Not
incorrect output, but litter accumulates on the error path.
Fix
Wrap the write + move in
try/except; on any failure unlink the temp filebefore re-raising (on a successful move it is already gone).
Test
test_atomic_write_cleans_temp_on_move_failurepatchesmoveto raise andasserts the directory listing is unchanged (no temp leaked) and no partial
target exists. Verified it fails on the old code and passes on the fix.
Gates: ruff format/check clean, ty clean, pytest green.
🤖 Generated with Claude Code