-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-81441: shutil.rmtree() FileNotFoundError race condition #14064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
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
directory entries in shutil.rmtree(), with tests
if given path does not exist
Co-Authored-By: Serhiy Storchaka <[email protected]>
through to the end of the function
Hi, are you guys planning to merge this? |
Tests did not work because the order of deletion is unspecified, and other file or directory was already deleted when the error handler was called. Now the error handler is called on the first attempt to delete, and it deletes at least one file and directory and keeps at least one file and directory, so many cases are covered. |
serhiy-storchaka
approved these changes
Dec 5, 2023
aisk
pushed a commit
to aisk/cpython
that referenced
this pull request
Feb 11, 2024
…honGH-14064) Ignore missing files and directories while enumerating directory entries in shutil.rmtree(). Co-authored-by: Serhiy Storchaka <[email protected]>
Fabcien
pushed a commit
to Bitcoin-ABC/bitcoin-abc
that referenced
this pull request
Apr 16, 2024
Summary: When the `daemon stop` command in called in the `fulcrum_service`'s tear-down, it returns before all the the threads are terminated. In particular the wallet files still need to be saved before the daemon process exits. This causes a race condition when the test fixture deletes the data directory while `WalletStorage._write` creates a tmp file then deletes it. See python/cpython#14064 for a description of how `shutil.rmtree` ends up raising a `FileNotFoundError`. This issue will go away in Python 3.13 when `shutil.rmtree` no longer fails because it failed to delete an already deleted file. In the meantime ignore these failures. I first tried a different approach: wait for the daemon process to actually terminate before deleting the data. However detecting the correct deamon process is not trivial, and if done wrong could cause more flakiness. Test Plan: `pytest electrumabc/tests/regtest` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Subscribers: Fabien Differential Revision: https://reviews.bitcoinabc.org/D15989
Fabcien
pushed a commit
to Bitcoin-ABC/ElectrumABC
that referenced
this pull request
Apr 17, 2024
Summary: When the `daemon stop` command in called in the `fulcrum_service`'s tear-down, it returns before all the the threads are terminated. In particular the wallet files still need to be saved before the daemon process exits. This causes a race condition when the test fixture deletes the data directory while `WalletStorage._write` creates a tmp file then deletes it. See python/cpython#14064 for a description of how `shutil.rmtree` ends up raising a `FileNotFoundError`. This issue will go away in Python 3.13 when `shutil.rmtree` no longer fails because it failed to delete an already deleted file. In the meantime ignore these failures. I first tried a different approach: wait for the daemon process to actually terminate before deleting the data. However detecting the correct deamon process is not trivial, and if done wrong could cause more flakiness. Test Plan: `pytest electrumabc/tests/regtest` Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Subscribers: Fabien Differential Revision: https://reviews.bitcoinabc.org/D15989
github-cerc-io
pushed a commit
to cerc-io/stack-orchestrator
that referenced
this pull request
Aug 28, 2024
github-cerc-io
pushed a commit
to cerc-io/stack-orchestrator
that referenced
this pull request
Aug 28, 2024
Otherwise we sometimes see errors like: ``` cerc-webapp-deployer: File "/root/.shiv/laconic-so_0f937aa98c2748ef9af8585d6f441dbc01546ace0d6660cbb159d1e5040aeddf/site-packages/stack_orchestrator/deploy/webapp/deploy_webapp_from_registry.py", line 671, in command cerc-webapp-deployer: shutil.rmtree(tempdir) cerc-webapp-deployer: File "/usr/lib/python3.10/shutil.py", line 725, in rmtree cerc-webapp-deployer: _rmtree_safe_fd(fd, path, onerror) cerc-webapp-deployer: File "/usr/lib/python3.10/shutil.py", line 681, in _rmtree_safe_fd cerc-webapp-deployer: onerror(os.unlink, fullname, sys.exc_info()) cerc-webapp-deployer: File "/usr/lib/python3.10/shutil.py", line 679, in _rmtree_safe_fd cerc-webapp-deployer: os.unlink(entry.name, dir_fd=topfd) cerc-webapp-deployer: FileNotFoundError: [Errno 2] No such file or directory: 'S.gpg-agent.extra' ``` Reviewed-on: https://git.vdb.to/cerc-io/stack-orchestrator/pulls/941 Co-authored-by: Thomas E Lackey <[email protected]> Co-committed-by: Thomas E Lackey <[email protected]>
Glyphack
pushed a commit
to Glyphack/cpython
that referenced
this pull request
Sep 2, 2024
…honGH-14064) Ignore missing files and directories while enumerating directory entries in shutil.rmtree(). Co-authored-by: Serhiy Storchaka <[email protected]>
mped-oticon
pushed a commit
to ArtifactLabs/git-recycle-bin
that referenced
this pull request
Nov 4, 2024
Python's shutil.rmtree() has a FileNotFoundError race condition, which has only been recently fixed upstream as of 3.11.8; see [1]. We have run into this race during FLOW-219, so we must workaround it since a very modern python can't be assumed. The race seems to be occur about git's garbage collection lock file since it only exists temporarily. rbgit is already ephemeral, so we don't even need GC. [1] python/cpython#14064
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.
shutil.rmtree() is susceptible to a race condition that can needlessly raise OSError:
The fix is to catch and ignore FileNotFoundError exceptions for all but the top-level path passed to shutil.rmtree(). This provides the same behavior as "rm -r".
I added an explicit check for path existence at the beginning of shutil.rmtree() so that it raises FileNotFoundError immediately to replace the previous implicit checks that are no longer considered errors.
https://bugs.python.org/issue37260