fix: don't leak or mask errors when rewrite cleanup fails on Windows - #686
Open
MohammedAlkindi wants to merge 1 commit into
Open
fix: don't leak or mask errors when rewrite cleanup fails on Windows#686MohammedAlkindi wants to merge 1 commit into
MohammedAlkindi wants to merge 1 commit into
Conversation
When set_key or unset_key fails after rewrite() has restored the original file mode, the temporary file has already been chmod'ed to that mode. On Windows a mode without the owner-write bit sets the read-only attribute, so the os.unlink in the cleanup path fails too. The temporary file is left next to the .env file and the cleanup error replaces the real one in the traceback. Reset the mode and retry before giving up, and never let a cleanup failure propagate in place of the error that caused the rewrite to be abandoned.
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.
When
set_keyorunset_keyfails afterrewrite()has restored the original file mode, the temporary file has already beenchmoded to that mode. On Windows a mode without the owner-write bit sets the read-only attribute, so theos.unlinkin the cleanup path fails too. The.tmp_*file is left next to the user's.env, and the cleanup error replaces the real one in the traceback.A read-only
.envis the easiest way in:Before, on Windows, that raises from
pathlib'sos.unlinkand the message names only a temp file the caller has never heard of, with.tmp_*left behind. After, it raises fromos.replacewith both paths in the message and nothing is left behind.This keeps the existing behaviour that
set_keyon a read-only file raisesPermissionError(test_set_key_permission_errorcovers it). That test was passing for the wrong reason on Windows: it caught the cleanup's error, not the one fromos.replace. I have not tried to make the write succeed by clearing the read-only attribute, which seems like a separate call and against the intent of the mode-preservation work in 790c5c0.Two tests: one asserts no temp file survives the failure, one mocks
os.replaceandPath.unlinkto assert a cleanup failure cannot stand in for the original error. The second is platform-independent; the first bites on Windows, where CI covers 3.10 and 3.14.Found while looking into #554. That report cites the old
shutil.movecall, which 790c5c0 already replaced withos.replace, and I could not reproduce its symptom on a native Windows host. This is a different bug in the same function.