fix(core): clean up orphaned temp files on atomic write failure - #6068
Open
SHAI-nikhil-chaudhary wants to merge 1 commit into
Open
fix(core): clean up orphaned temp files on atomic write failure#6068SHAI-nikhil-chaudhary wants to merge 1 commit into
SHAI-nikhil-chaudhary wants to merge 1 commit into
Conversation
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
atomic_write_json/atomic_write_textwrite to a uniquely-named.tmp.<uuid>file and thenos.replace()it onto the target. If anything fails between creating the temp file and thesuccessful replace, the temp file is left on disk. The two cases that reach this in practice:
json.dump()raises (e.g. asetin the payload) after the temp filehas already been opened, so a partial or empty
.tmp.<uuid>is orphaned.os.replace()raises (permissions, cross-device, target locked), leaving thefully-written temp file behind.
Because the suffix is a UUID, every failure orphans a new file rather than overwriting the last
one, so a repeatedly-failing write accumulates junk next to the real data file indefinitely.
This PR wraps the write-and-replace in
try/finallyso the temp path is unlinked on any failurepath. The success path is unchanged: after
os.replace()succeeds the temp path no longer exists,and the cleanup is a no-op guarded against
FileNotFoundError. The exception still propagates —callers see the same
TypeError/OSErrorthey see today.Target branch
dev, notmain.Linked Issue
Fixes #6001
Type of Change
Checklist
devdocker compose uporuvicorn app:app) and verified the change works end-to-end.How to Test
1. Confirm the app still writes normally.
Exercise any flow that persists state through
atomic_io(e.g. saving settings), then confirm thetarget file is written and no
.tmp.*files are left beside it:2. Serialization failure —
atomic_write_json.From the project root:
Expected:
leftover: []. Ondevtoday this prints one orphaned temp file, and a second runprints two.
3. Replace failure —
atomic_write_text.Force
os.replace()to fail after a successful write:Expected:
leftover: [], andtest_target.txtis either absent or still holds its previouscontents — the failed write must not have clobbered it.
4. Confirm the exception is unchanged. Both snippets above must still raise (
TypeError,PermissionError). Cleanup must not swallow the error.Visual / UI changes
N/A — this touches
core/atomic_io.pyonly; nothing renders.