Skip to content
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

fix: avoid races in filesystem ops #2287

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lgarrison
Copy link

Use exist_ok and missing_ok in pathlib operations where we can.

I didn't do an exhaustive audit of pathlib usage, but I did look at places where the code was using path.exists(). There's still a couple of places with this pattern:

if path.exists():
    shutil.rmtree(path)

I don't think there's an elegant solution that will work across Python versions currently, although in 3.13 I think one can do:

try:
    shutil.rmtree(path)
except FileNotFoundError:
    pass

Because: "Changed in version 3.13: rmtree() now ignores FileNotFoundError exceptions for all but the top-level path." (https://docs.python.org/3.13/library/shutil.html#shutil.rmtree)

Fixes #2279.

@henryiii
Copy link
Contributor

I think this is tripping up a test because we were catching an exception somewhere that has now changed type? (from a quick glance)

if dest.exists():
dest.unlink()
dest.unlink(missing_ok=True)
Copy link
Contributor

@joerick joerick Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one won't work, because missing_ok was added in Python 3.8, and the repair script runs in the build, so runs on 3.6 and 3.7.

Use `exist_ok` and `missing_ok` in pathlib operations where we can.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

race condition in output dir creation
3 participants