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.
This commit is contained in:
Robert C. Martin
2026-11-18 10:42:45 -06:00
parent 830ec7e9c3
commit 29afb42712
2 changed files with 115 additions and 5 deletions
+25 -5
View File
@@ -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: