Skip to content

Commit

Permalink
[FIX] Also allow errno.EBUSY during emptydirs on NFS (#3357)
Browse files Browse the repository at this point in the history
* Also allow `errno.EBUSY` during `emptydirs` on NFS

- Can occur if a file is still open somewhere, so NFS will rename it to
  a hidden file in the same directory
- When `shutil` tries to delete that hidden file, we get an `OSError`
  with `errno.EBUSY`

* Ignore `.nfs` placeholder files when catching the error

- I forgot that `os.listdir` also lists hidden files in the previous
  commit

* Add unit test for `emptydirs` on NFS

- With mock for NFS silly-rename (yes, it's really called that) behavior

* Run `black`

* Update nipype/utils/tests/test_filemanip.py

Handle mock test case when no `dir_fd` is passed
  • Loading branch information
HippocampusGirl authored Oct 13, 2021
1 parent 4e10801 commit 4d915a8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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)


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

0 comments on commit 4d915a8

Please sign in to comment.