Repository navigation
🐛 fix(util): make safe_delete's error handler actually run - #3222
Merged
Merged
Conversation
safe_delete passes a chmod-and-retry handler to shutil.rmtree while also setting ignore_errors=True — and rmtree unconditionally replaces any handler with a no-op when ignore_errors is set, so the handler has never run. Every deletion failure was silently swallowed instead. This matters most on Windows, where the read-only attribute blocks os.unlink, and virtualenv itself marks every wheel-image file read-only via set_tree. So --reset-app-data quietly kept the image trees it claimed to remove, recreating an environment with --clear could leave read-only files from the old one behind, and uninstalling a previous seed package could leave a mix of old and new files. Found by asking what the ty: ignore[call-non-callable] on the retry call was hiding; reproduced on Windows before the fix (rmtree left the tree, handler invoked zero times). Dropping ignore_errors lets the handler do what it was written to do: clear the read-only bit and retry, and surface anything it cannot fix instead of pretending the delete worked. A FileNotFoundError guard keeps AppDataDiskFolder.reset a no-op when nothing was ever cached — the one caller without an exists() check. Typing func as a real callable removes the ty suppression that was papering over all this.
Now that the handler runs, its retry is reachable - and rmtree does not only report os.unlink and os.rmdir. On Linux and macOS it deletes through directory file descriptors and reports os.open, so func(path) raised "TypeError: open() missing required argument 'flags'" on top of the PermissionError that caused it. os.scandir and os.lstat take a lone path but delete nothing, so retrying one of those returned from the handler and told rmtree the failure was dealt with - the swallowing this fix set out to remove, through another door. Retry only the two callables a path can replay and re-raise the rest. Clearing the read-only bit now keeps the other mode bits instead of replacing the mode with S_IWUSR, which left a directory at 0o200 - neither listable nor traversable - whenever the retry went on to fail. Both new tests are POSIX-only and fail before this change: the first on TypeError instead of PermissionError, the second on the discarded mode.
gaborbernat
enabled auto-merge (squash)
August 25, 2026 04:08
gaborbernat
approved these changes
Aug 25, 2026
halstead
pushed a commit
to openembedded/meta-openembedded
that referenced
this pull request
Nov 10, 2026
Analysis: - The advisory [1] describes a deletion-error callback changing the permissions of a symlink target. - In 21.2.1, safe_delete() always passes ignore_errors=True to shutil.rmtree(), which disables the supplied error callback. This remains true after the existing security backports. - Upstream enabled that callback later in [2]. The chmod recovery path fixed by [3] is therefore unreachable in this recipe. Reference: [1] GHSA-43mp-35q3-x3fq [2] pypa/virtualenv#3222 [3] pypa/virtualenv@2bfdc02 Signed-off-by: Hetvi Thakar <[email protected]> Signed-off-by: Anuj Mittal <[email protected]>
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.
Thanks for contributing, make sure you address all the checklists (for details on how see development documentation)
tox -e fix)docs/changelogfoldersafe_deletehandsshutil.rmtreea chmod-and-retry handler and setsignore_errors=Trueat the same time:rmtreeoverwrites whatever handler it is given whenignore_errorsis set —onexc = _onexc = lambda *args: None— so this handler has never run. Every deletion failure was swallowed andsafe_deletereturned as if it had worked.That matters on Windows, where the read-only attribute blocks
os.unlink, and virtualenv marks every file in a wheel image read-only itself viaset_tree. So:--reset-app-datakept the image trees it reported removing--clearcould leave read-only files from the old one behindI found this by asking what the
# ty: ignore[call-non-callable]on thefunc(path)line was hiding. The suppression was there becausefuncis annotatedobject, andfuncis annotatedobjectbecause nothing ever called it. Before the fix, on Windows,rmtreeleft the tree in place and the handler was invoked zero times.Dropping
ignore_errorslets the handler do what it was written to do: clear the read-only bit, retry, and surface anything it genuinely cannot delete instead of pretending the delete succeeded. Typingfuncproperly removes thetysuppression —ty check src/virtualenvstill passes without it.One caller needed a guard.
AppDataDiskFolder.resetis the only one that callssafe_deletewithout anexists()check first, so a missing directory used to be absorbed byignore_errors.onerrornow returns early onFileNotFoundErrorto keep that a no-op.Three tests: a read-only tree is actually removed, a missing path stays a no-op, and an undeletable entry now raises instead of being swallowed. The first and third fail on
main. The third is Windows-only, since it needs an open file handle to block deletion; the first passes on POSIX either way, because there deleting a file depends on the parent directory's permissions rather than the file's own.tests/unitis green locally on Windows / 3.13 (296 passed, 31 skipped), along with ruff andty.