Skip to content

fix(fileops): remove temp file when atomic_write_file fails#215

Merged
mimi1vx merged 1 commit into
openSUSE:mainfrom
plusky:fix/atomic-write-temp-cleanup
Jun 23, 2026
Merged

fix(fileops): remove temp file when atomic_write_file fails#215
mimi1vx merged 1 commit into
openSUSE:mainfrom
plusky:fix/atomic-write-temp-cleanup

Conversation

@plusky

@plusky plusky commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

atomic_write_file creates a mkstemp temp file, writes to it, then moves it
into place:

fd, fname = mkstemp(dir=path.parent)
with os.fdopen(fd, "w") as f:
    f.write(data)
move(fname, path)

If the write or the move raises (e.g. a permission or I/O error on the
destination), 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 file
before re-raising (on a successful move it is already gone).

Test

test_atomic_write_cleans_temp_on_move_failure patches move to raise and
asserts 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

@codecov

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.44%. Comparing base (af6ceb2) to head (5708fda).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

@plusky plusky force-pushed the fix/atomic-write-temp-cleanup branch 3 times, most recently from 56fbf69 to a0d7837 Compare June 23, 2026 08:45
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).
@mimi1vx mimi1vx force-pushed the fix/atomic-write-temp-cleanup branch from a0d7837 to 5708fda Compare June 23, 2026 10:08
@mimi1vx mimi1vx merged commit 9d273e1 into openSUSE:main Jun 23, 2026
4 checks passed
@plusky plusky deleted the fix/atomic-write-temp-cleanup branch June 23, 2026 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants