Repository navigation
tempfile.TemporaryDirectory fails to delete itself #74168
Description
Activity
There's a known issue with
shutil.rmtreeon Windows, in that it fails intermittently.The issue is well known (https://mail.python.org/pipermail/python-dev/2013-September/128353.html), and the agreement is that it cannot be cleanly solved inside
shutiland should instead be solved by the calling app. Specifically, python devs themselves faced it in their test suite and solved it by retrying delete.However, what to do about
tempfile.TemporaryDirectory? Is it considered the calling app, and therefore should retry delete when it callsshutil.rmtreein itscleanupmethod?I don't think
tempfileis protected by the same argument thatshutil.rmtreeis protected, in that it's too messy to solve it in the standard library. My rationale is that while it's very easy for the end user to retryshutil.rmtree, it's far more difficult to fix the problem withtempfile.TempDirectorynot deleting itself - how would the end user retry thecleanupmethod (which is called fromweakref.finalizer)?So perhaps the retry loop should be added to
cleanup.- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Apr 4, 2017 A simpler approach would be to simply ignore the error when it occurs in TemporaryDirectory._cleanup. There are no absolute guarantees about tempfile always cleaning up -- just a best effort. The complexity (and potential delays) of a retry loop seem more risky than simply occasionally not cleaning up -- that happens anyways, for a variety of reasons.
In addition to transient failures, this can also occur when, for example, files opened in the temporary directory (perhaps by library or application code not under direct control of the caller) haven't been properly cleaned up and their file handles don't get closed, resulting in permissions errors trying to delete them (particularly on platforms like Windows, that automatically lock files when opening them).
This case came up in e.g. this recent PR and rendered
tempfile.TemporaryDirectoryunusable for such use cases, forcing a reversion to the lower-leveltempfile.mkdtempwithout the cleaner, more robust and easier to interpret high-level interface that the former provides. Wrapping awithstatement in a try-finally is syntactically ugly and semantically incongruous, and requires a second shutil.rmtree(..., ignore_errors=True)call to clean up in a best-effort manner, while when manually callingcleanup()` in a try-except, the finalizer still gets executed at a a non-deterministic later time when Python exits, raising an error.Therefore, in the spirit of Guido's statements above in terms of providing a "best-effort" cleanup, I propose (and am willing to submit a PR to implement) adding an
ignore_errorsbool parameter (defaulting to False, of course, for backward compat--and should it be keyword only likeerrorstoTemporaryFile?) to thetempfile.TemporaryDirectoryconstructor, which gets passed toshutil.rmtreeon cleanup. This would not only address both cases here, but also one of the two discussed by Anthony Sotitle on BPO-25024, in a cleaner and simpler fashion that would take advantage of existingtempfile.TemporaryDirectoryfunctionality and behavior.Would a PR be accepted on this? If so, any specific guidance on tests and whether to mention it in What's New, etc., would be appreciated. Thanks!
SGTM
- added 2 commits that reference this issue
on May 11, 2022 - added a commit that references this issue
on May 11, 2022 - added a commit that references this issue
on Feb 18, 2024
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: