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

Make ensure_path_nonexistent race condition aware #429

Merged
merged 7 commits into from
Jan 29, 2024

Conversation

elikoga
Copy link
Member

@elikoga elikoga commented Dec 20, 2023

This is susceptible to the same race conditions as the implementation from before, however it is able to notice the race condition and fail accordingly.

closes #275

@elikoga elikoga requested a review from ctheune as a code owner December 20, 2023 23:16
src/batou/lib/file.py Outdated Show resolved Hide resolved
path_basename = os.path.basename(os.path.normpath(path))

with tempfile.TemporaryDirectory(dir=parent_dir) as temp_dir:
os.rename(path, os.path.join(temp_dir, path_basename))
Copy link
Member

Choose a reason for hiding this comment

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

This can fail because the whole idea of a race condition means someone else might be doing unexpected stuff, so failing renames should be ignored if the path to delete is gone in any case.

Copy link
Member Author

Choose a reason for hiding this comment

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

This context manager is for deleting the Temporary Directory. No assumption that os.rename may fail, but the happy path thankfully includes a deletion of the tmpdir

I can split it up if this is misleading

Copy link
Member

Choose a reason for hiding this comment

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

The rename needs to be wrapped in a try/except. Ignore for FileNotFound, re-raise in other cases.

src/batou/lib/file.py Outdated Show resolved Hide resolved
@ctheune ctheune merged commit cef05c6 into main Jan 29, 2024
11 checks passed
@ctheune ctheune deleted the 275-ensure-path-nonexistent-race-condition-aware branch January 29, 2024 14:05
ctheune added a commit that referenced this pull request Jan 30, 2024
…nt-race-condition-aware

Make ensure_path_nonexistent race condition aware
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.

ensure_path_nonexistent doesn't remove symlink
2 participants