Skip to content

Fix access denied error when deleting a stale temporary directory #4267

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 1 commit into from
Oct 30, 2018
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
1 change: 1 addition & 0 deletions changelog/4262.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix access denied error when deleting stale directories created by ``tmpdir`` / ``tmp_path``.
24 changes: 17 additions & 7 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,29 @@ def cleanup_on_exit(lock_path=lock_path, original_pid=pid):


def maybe_delete_a_numbered_dir(path):
"""removes a numbered directory if its lock can be obtained"""
"""removes a numbered directory if its lock can be obtained and it does not seem to be in use"""
lock_path = None
try:
create_cleanup_lock(path)
lock_path = create_cleanup_lock(path)
parent = path.parent

garbage = parent.joinpath("garbage-{}".format(uuid.uuid4()))
path.rename(garbage)
rmtree(garbage, force=True)
except (OSError, EnvironmentError):
# known races:
# * other process did a cleanup at the same time
# * deletable folder was found
# * process cwd (Windows)
return
parent = path.parent

garbage = parent.joinpath("garbage-{}".format(uuid.uuid4()))
path.rename(garbage)
rmtree(garbage, force=True)
finally:
# if we created the lock, ensure we remove it even if we failed
# to properly remove the numbered dir
if lock_path is not None:
try:
lock_path.unlink()
except (OSError, IOError):
pass


def ensure_deletable(path, consider_lock_dead_if_created_before):
Expand Down
18 changes: 18 additions & 0 deletions testing/test_paths.py → testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import pytest
from _pytest.pathlib import fnmatch_ex
from _pytest.pathlib import get_lock_path
from _pytest.pathlib import maybe_delete_a_numbered_dir
from _pytest.pathlib import Path


class TestPort:
Expand Down Expand Up @@ -66,3 +69,18 @@ def test_matching(self, match, pattern, path):
)
def test_not_matching(self, match, pattern, path):
assert not match(pattern, path)


def test_access_denied_during_cleanup(tmp_path, monkeypatch):
"""Ensure that deleting a numbered dir does not fail because of OSErrors (#4262)."""
path = tmp_path / "temp-1"
path.mkdir()

def renamed_failed(*args):
raise OSError("access denied")

monkeypatch.setattr(Path, "rename", renamed_failed)

lock_path = get_lock_path(path)
maybe_delete_a_numbered_dir(path)
assert not lock_path.is_file()