diff --git a/AUTHORS b/AUTHORS index 14c46557129..75f292dcf3c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -47,6 +47,7 @@ Brian Maissy Brian Okken Brianna Laugher Bruno Oliveira +Caitlin Macleod Cal Leeming Carl Friedrich Bolz Carlos Jenkins @@ -177,6 +178,7 @@ Matt Williams Matthias Hafner Maxim Filipenko mbyt +Michael Abel Michael Aquilina Michael Birtwell Michael Droettboom diff --git a/changelog/1120.bugfix.rst b/changelog/1120.bugfix.rst new file mode 100644 index 00000000000..6f04e09462f --- /dev/null +++ b/changelog/1120.bugfix.rst @@ -0,0 +1,2 @@ +Fix issue where directories from ``tmpdir`` are not removed properly when +multiple instances of pytest are running in parallel. diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index 8d25b21dd7d..a290b3dfa32 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -296,6 +296,15 @@ def make_numbered_dir_with_cleanup( ) -> Path: """creates a numbered dir with a cleanup lock and removes old ones""" e = None + # Register a cleanup for program exit + consider_lock_dead_if_created_before = p.stat().st_mtime - lock_timeout + atexit.register( + cleanup_numbered_dir, + root, + prefix, + keep, + consider_lock_dead_if_created_before, + ) for i in range(10): try: p = make_numbered_dir(root, prefix) @@ -304,13 +313,6 @@ def make_numbered_dir_with_cleanup( except Exception as exc: e = exc else: - consider_lock_dead_if_created_before = p.stat().st_mtime - lock_timeout - cleanup_numbered_dir( - root=root, - prefix=prefix, - keep=keep, - consider_lock_dead_if_created_before=consider_lock_dead_if_created_before, - ) return p assert e is not None raise e