Skip to content

[FIX] Also allow errno.EBUSY during emptydirs on NFS #3357

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
merged 5 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions nipype/utils/filemanip.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,13 @@ def emptydirs(path, noexist_ok=False):
try:
shutil.rmtree(path)
except OSError as ex:
elcont = os.listdir(path)
if ex.errno == errno.ENOTEMPTY and not elcont:
elcont = [
Path(root) / file
for root, _, files in os.walk(path)
for file in files
if not file.startswith(".nfs")
]
if ex.errno in [errno.ENOTEMPTY, errno.EBUSY] and not elcont:
fmlogger.warning(
"An exception was raised trying to remove old %s, but the path"
" seems empty. Is it an NFS mount?. Passing the exception.",
Expand All @@ -793,7 +798,7 @@ def emptydirs(path, noexist_ok=False):
else:
raise ex

os.makedirs(path)
os.makedirs(path, exist_ok=True)


def silentrm(filename):
Expand Down
24 changes: 24 additions & 0 deletions nipype/utils/tests/test_filemanip.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
savepkl,
path_resolve,
write_rst_list,
emptydirs,
)


Expand Down Expand Up @@ -670,3 +671,26 @@ def test_write_rst_list(tmp_path, items, expected):
else:
with pytest.raises(expected):
write_rst_list(items)


def nfs_unlink(pathlike, *, dir_fd=None):
if dir_fd is None:
path = Path(pathlike)
deleted = path.with_name(".nfs00000000")
path.rename(deleted)
else:
os.rename(pathlike, ".nfs1111111111", src_dir_fd=dir_fd, dst_dir_fd=dir_fd)
Comment on lines +677 to +682
Copy link
Member

Choose a reason for hiding this comment

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

I do like your idea of avoiding branching. What if we just construct the target correctly?

Suggested change
if dir_fd is None:
path = Path(pathlike)
deleted = path.with_name(".nfs00000000")
path.rename(deleted)
else:
os.rename(pathlike, ".nfs1111111111", src_dir_fd=dir_fd, dst_dir_fd=dir_fd)
target = os.path.join(os.path.dirname(pathlike), ".nfs00000000")
os.rename(pathlike, target, src_dir_fd=dir_fd, dst_dir_fd=dir_fd)

Demo:

>>> [os.path.join(os.path.dirname(pathlike), ".nfs00000000")
...  for pathlike in ["path/subdir/busyfile", "busyfile"]]
['path/subdir/.nfs00000000', '.nfs00000000']



def test_emptydirs_dangling_nfs(tmp_path):
busyfile = tmp_path / "base" / "subdir" / "busyfile"
busyfile.parent.mkdir(parents=True)
busyfile.touch()

with mock.patch("os.unlink") as mocked:
mocked.side_effect = nfs_unlink
emptydirs(tmp_path / "base")

assert Path.exists(tmp_path / "base")
assert not busyfile.exists()
assert busyfile.parent.exists() # Couldn't remove