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

ensure_path_nonexistent doesn't remove symlink #275

Closed
zagy opened this issue Jan 31, 2022 · 3 comments · Fixed by #429
Closed

ensure_path_nonexistent doesn't remove symlink #275

zagy opened this issue Jan 31, 2022 · 3 comments · Fixed by #429

Comments

@zagy
Copy link
Member

zagy commented Jan 31, 2022

  File "/srv/s-oldb/deployment/.batou/00086ca4/lib/python3.8/site-packages/batou/component.py", line 320, in deploy
    sub_component.deploy(predict_only)
  File "/srv/s-oldb/deployment/.batou/00086ca4/lib/python3.8/site-packages/batou/component.py", line 320, in deploy
    sub_component.deploy(predict_only)
  File "/srv/s-oldb/deployment/.batou/00086ca4/lib/python3.8/site-packages/batou/component.py", line 339, in deploy
    self.update()
  File "/srv/s-oldb/deployment/.batou/00086ca4/lib/python3.8/site-packages/batou/lib/file.py", line 616, in update
    ensure_path_nonexistent(self.target)
  File "/srv/s-oldb/deployment/.batou/00086ca4/lib/python3.8/site-packages/batou/lib/file.py", line 23, in ensure_path_nonexistent
    shutil.rmtree(path)
  File "/run/current-system/sw/lib/python3.8/shutil.py", line 728, in rmtree
    onerror(os.path.islink, path, sys.exc_info())
  File "/run/current-system/sw/lib/python3.8/shutil.py", line 726, in rmtree
    raise OSError("Cannot call rmtree on a symbolic link")
OSError: Cannot call rmtree on a symbolic link
@zagy
Copy link
Member Author

zagy commented Jan 31, 2022

This could be a race condition due to parallel job execution and nfs.

@ctheune ctheune added this to the batou 2.4 milestone Aug 24, 2022
@elikoga
Copy link
Member

elikoga commented Dec 19, 2023

def ensure_path_nonexistent(path):
if not os.path.lexists(path):
return
if os.path.islink(path):
os.unlink(path)
elif os.path.isdir(path):
shutil.rmtree(path)
else:
os.unlink(path)

This has to be a race condition where 24 is evaluated as false but somewhere during execution of 26 and 27, os.path.islink(path) turns true

Not sure what to do about this. I think this is a valid error and I can't think of anything batou can do in that situation right now

@elikoga
Copy link
Member

elikoga commented Dec 20, 2023

What about an unconditional os.rename to a random name at the beginning? If it fails due to src not existing, we are happy. If it does succeed we can remove the renamed file or directory.

If successful, the renaming will be an atomic operation (this is a POSIX requirement).
from https://docs.python.org/3/library/os.html#os.rename

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 a pull request may close this issue.

3 participants