From 29afb427129b6bf03699286966fc55ec8a4830d7 Mon Sep 17 00:00:00 2001 From: "Robert C. Martin" Date: Wed, 18 Nov 2026 10:42:45 -0600 Subject: [PATCH] Retry worker cleanup when Gradle writes after the client exits. Why: delete_tree walked the worker tree and called rmdir once. Gradle can still be flushing a test report then, so a directory that looked empty raises OSError [Errno 39] Directory not empty (or a file vanishes before unlink) and the whole mutation run aborts. What: delete_tree retries the walk, with injectable sleep and attempt count so tests do not wait. A vanished file is unlinked with missing_ok. Symlinks are still unlinked, never followed into the real project. Proof: six new tests (late file, vanished file, missing path, regular file, symlink left alone, exhausted retries re-raise). 78 tests were green before; 84 pass now. crapper: delete_tree 5.0, _delete_once 5.0, every function in workers.py under 10. dryer: no candidates. mutator on the nine changed sites: 9 killed, 0 survived, 0 uncovered. --- src/mutator/workers.py | 30 +++++++++++--- tests/test_workers.py | 90 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 tests/test_workers.py diff --git a/src/mutator/workers.py b/src/mutator/workers.py index b1f154c..a3721f3 100644 --- a/src/mutator/workers.py +++ b/src/mutator/workers.py @@ -11,6 +11,7 @@ from __future__ import annotations import os import re import sys +import time import uuid from concurrent.futures import ThreadPoolExecutor from pathlib import Path @@ -93,20 +94,39 @@ def symlink(link: Path, target: Path) -> None: link.symlink_to(target.resolve()) -def delete_tree(path: Path) -> None: - """Remove a worker tree without following a link into the real project.""" +def delete_tree(path: Path, attempts: int = 20, sleep=time.sleep) -> None: + """Remove a worker tree without following a link into the real project. + Gradle can still be flushing a test report when the client exits, so a + directory that looked empty can gain a file before ``rmdir``. Retry the + walk instead of aborting the file's mutation run. + """ + + last: OSError | None = None + for attempt in range(attempts): + try: + _delete_once(path) + return + except OSError as error: + last = error + if attempt + 1 < attempts: + sleep(0.25 * (attempt + 1)) + if last is not None: + raise last + + +def _delete_once(path: Path) -> None: if path.is_symlink(): - path.unlink() + path.unlink(missing_ok=True) return if not path.exists(): return if path.is_dir(): for child in list(path.iterdir()): - delete_tree(child) + _delete_once(child) path.rmdir() return - path.unlink() + path.unlink(missing_ok=True) def _copy_configs(worker: Path, root: Path, relative: str) -> None: diff --git a/tests/test_workers.py b/tests/test_workers.py new file mode 100644 index 0000000..2fd14ec --- /dev/null +++ b/tests/test_workers.py @@ -0,0 +1,90 @@ +import errno +from pathlib import Path + +import pytest + +from mutator.workers import delete_tree + + +def test_delete_tree_retries_when_a_directory_gains_a_file(tmp_path, monkeypatch): + tree = tmp_path / "worker" + tree.mkdir() + (tree / "old-report.xml").write_text("done", encoding="utf-8") + injected = {"count": 0} + original_rmdir = Path.rmdir + + def rmdir(self): + if self == tree and injected["count"] == 0: + injected["count"] += 1 + (self / "late-report.xml").write_text("flushed", encoding="utf-8") + raise OSError(errno.ENOTEMPTY, "Directory not empty") + return original_rmdir(self) + + monkeypatch.setattr(Path, "rmdir", rmdir) + delete_tree(tree, attempts=3, sleep=lambda _: None) + assert not tree.exists() + assert injected["count"] == 1 + + +def test_delete_tree_tolerates_a_file_that_vanishes(tmp_path, monkeypatch): + tree = tmp_path / "worker" + tree.mkdir() + vanishing = tree / "report.xml" + vanishing.write_text("temp", encoding="utf-8") + original_unlink = Path.unlink + + def unlink(self, *args, **kwargs): + if self == vanishing and vanishing.exists(): + original_unlink(self) + return original_unlink(self, *args, **kwargs) + + monkeypatch.setattr(Path, "unlink", unlink) + delete_tree(tree, attempts=1, sleep=lambda _: None) + assert not tree.exists() + + +def test_delete_tree_ignores_a_missing_path(tmp_path): + missing = tmp_path / "already-gone" + delete_tree(missing, attempts=1, sleep=lambda _: None) + assert not missing.exists() + + +def test_delete_tree_removes_a_regular_file(tmp_path): + path = tmp_path / "worker-file" + path.write_text("x", encoding="utf-8") + delete_tree(path, attempts=1, sleep=lambda _: None) + assert not path.exists() + + +def test_delete_tree_unlinks_a_symlink_without_following_it(tmp_path): + real = tmp_path / "project" + real.mkdir() + kept = real / "important.txt" + kept.write_text("keep", encoding="utf-8") + tree = tmp_path / "worker" + tree.mkdir() + (tree / "src").symlink_to(real) + link_root = tmp_path / "worker-link" + link_root.symlink_to(real) + delete_tree(tree, attempts=1, sleep=lambda _: None) + delete_tree(link_root, attempts=1, sleep=lambda _: None) + assert not tree.exists() + assert not link_root.exists() + assert kept.read_text(encoding="utf-8") == "keep" + + +def test_delete_tree_reraises_after_the_attempts_are_exhausted(tmp_path, monkeypatch): + tree = tmp_path / "worker" + tree.mkdir() + (tree / "stuck.txt").write_text("x", encoding="utf-8") + + def rmdir(self): + raise OSError(errno.ENOTEMPTY, "Directory not empty") + + monkeypatch.setattr(Path, "rmdir", rmdir) + sleeps: list[float] = [] + with pytest.raises(OSError) as caught: + delete_tree(tree, attempts=3, sleep=sleeps.append) + assert caught.value.errno == errno.ENOTEMPTY + assert sleeps == [0.25, 0.5] + assert tree.exists()