Inaccessible lock files now imply temporary directories can't be removed
Fix #7500 Co-authored-by: Ran Benita <ran@unusedvar.com>
This commit is contained in:
parent
41c40efe80
commit
e7c42ae62b
|
@ -0,0 +1,2 @@
|
|||
:fixture:`tmpdir` and :fixture:`tmp_path` no longer raise an error if the lock to check for
|
||||
stale temporary directories is not accessible.
|
|
@ -286,12 +286,17 @@ def maybe_delete_a_numbered_dir(path: Path) -> None:
|
|||
|
||||
|
||||
def ensure_deletable(path: Path, consider_lock_dead_if_created_before: float) -> bool:
|
||||
"""checks if a lock exists and breaks it if its considered dead"""
|
||||
"""checks if `path` is deletable based on whether the lock file is expired"""
|
||||
if path.is_symlink():
|
||||
return False
|
||||
lock = get_lock_path(path)
|
||||
if not lock.exists():
|
||||
return True
|
||||
try:
|
||||
if not lock.is_file():
|
||||
return True
|
||||
except OSError:
|
||||
# we might not have access to the lock file at all, in this case assume
|
||||
# we don't have access to the entire directory (#7491).
|
||||
return False
|
||||
try:
|
||||
lock_time = lock.stat().st_mtime
|
||||
except Exception:
|
||||
|
|
|
@ -358,17 +358,25 @@ def test_get_extended_length_path_str():
|
|||
|
||||
|
||||
def test_suppress_error_removing_lock(tmp_path):
|
||||
"""ensure_deletable should not raise an exception if the lock file cannot be removed (#5456)"""
|
||||
"""ensure_deletable should be resilient if lock file cannot be removed (#5456, #7491)"""
|
||||
path = tmp_path / "dir"
|
||||
path.mkdir()
|
||||
lock = get_lock_path(path)
|
||||
lock.touch()
|
||||
mtime = lock.stat().st_mtime
|
||||
|
||||
with unittest.mock.patch.object(Path, "unlink", side_effect=OSError):
|
||||
with unittest.mock.patch.object(Path, "unlink", side_effect=OSError) as m:
|
||||
assert not ensure_deletable(
|
||||
path, consider_lock_dead_if_created_before=mtime + 30
|
||||
)
|
||||
assert m.call_count == 1
|
||||
assert lock.is_file()
|
||||
|
||||
with unittest.mock.patch.object(Path, "is_file", side_effect=OSError) as m:
|
||||
assert not ensure_deletable(
|
||||
path, consider_lock_dead_if_created_before=mtime + 30
|
||||
)
|
||||
assert m.call_count == 1
|
||||
assert lock.is_file()
|
||||
|
||||
# check now that we can remove the lock file in normal circumstances
|
||||
|
|
Loading…
Reference in New Issue