diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index c5a411b59..a12d2d6f4 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -357,8 +357,10 @@ def make_numbered_dir_with_cleanup( for i in range(10): try: p = make_numbered_dir(root, prefix, mode) - lock_path = create_cleanup_lock(p) - register_cleanup_lock_removal(lock_path) + # Do not lock the current dir when keep is 0 + if keep != 0: + lock_path = create_cleanup_lock(p) + register_cleanup_lock_removal(lock_path) except Exception as exc: e = exc else: diff --git a/src/_pytest/tmpdir.py b/src/_pytest/tmpdir.py index db41b3c64..fc454be7a 100644 --- a/src/_pytest/tmpdir.py +++ b/src/_pytest/tmpdir.py @@ -154,10 +154,13 @@ class TempPathFactory: ) if (rootdir_stat.st_mode & 0o077) != 0: os.chmod(rootdir, rootdir_stat.st_mode & ~0o077) + keep = self._retention_count + if self._retention_policy == "none": + keep = 0 basetemp = make_numbered_dir_with_cleanup( prefix="pytest-", root=rootdir, - keep=self._retention_count, + keep=keep, lock_timeout=LOCK_TIMEOUT, mode=0o700, )