Skip to content

Commit

Permalink
[4.6] Review rm_rf handling of FileNotFoundErrors (pytest-dev#6050)
Browse files Browse the repository at this point in the history
[4.6] Review rm_rf handling of FileNotFoundErrors
  • Loading branch information
nicoddemus authored Oct 23, 2019
2 parents e89efa8 + 0084fd9 commit 3edf417
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions changelog/6044.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Properly ignore ``FileNotFoundError`` (``OSError.errno == NOENT`` in Python 2) exceptions when trying to remove old temporary directories,
for instance when multiple processes try to remove the same directory (common with ``pytest-xdist``
for example).
27 changes: 21 additions & 6 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,38 @@ def ensure_reset_dir(path):


def on_rm_rf_error(func, path, exc, **kwargs):
"""Handles known read-only errors during rmtree."""
"""Handles known read-only errors during rmtree.
The returned value is used only by our own tests.
"""
start_path = kwargs["start_path"]
excvalue = exc[1]
exctype, excvalue = exc[:2]

# another process removed the file in the middle of the "rm_rf" (xdist for example)
# more context: https://github.com/pytest-dev/pytest/issues/5974#issuecomment-543799018
if isinstance(excvalue, OSError) and excvalue.errno == errno.ENOENT:
return False

if not isinstance(excvalue, OSError) or excvalue.errno not in (
errno.EACCES,
errno.EPERM,
):
warnings.warn(
PytestWarning("(rm_rf) error removing {}: {}".format(path, excvalue))
PytestWarning(
"(rm_rf) error removing {}\n{}: {}".format(path, exctype, excvalue)
)
)
return
return False

if func not in (os.rmdir, os.remove, os.unlink):
warnings.warn(
PytestWarning("(rm_rf) error removing {}: {}".format(path, excvalue))
PytestWarning(
"(rm_rf) unknown function {} when removing {}:\n{}: {}".format(
path, func, exctype, excvalue
)
)
)
return
return False

# Chmod + retry.
import stat
Expand All @@ -86,6 +100,7 @@ def chmod_rw(p):
chmod_rw(str(path))

func(path)
return True


def rm_rf(path):
Expand Down
7 changes: 6 additions & 1 deletion testing/test_tmpdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,14 @@ def test_on_rm_rf_error(self, tmp_path):
on_rm_rf_error(os.unlink, str(fn), exc_info, start_path=tmp_path)
assert fn.is_file()

# we ignore FileNotFoundError
file_not_found = OSError()
file_not_found.errno = errno.ENOENT
exc_info = (None, file_not_found, None)
assert not on_rm_rf_error(None, str(fn), exc_info, start_path=tmp_path)

permission_error = OSError()
permission_error.errno = errno.EACCES

# unknown function
with pytest.warns(pytest.PytestWarning):
exc_info = (None, permission_error, None)
Expand Down

0 comments on commit 3edf417

Please sign in to comment.