diff --git a/tests/tools/test_shell_execution_budgets.py b/tests/tools/test_shell_execution_budgets.py index 1891ec4d5..3d4f0a365 100644 --- a/tests/tools/test_shell_execution_budgets.py +++ b/tests/tools/test_shell_execution_budgets.py @@ -91,12 +91,27 @@ def _pid_running(pid: int) -> bool: return False # A killed grandchild can remain a zombie until its new parent reaps it. stat = Path(f"/proc/{pid}/stat") - with suppress(FileNotFoundError): + try: if stat.exists() and stat.read_text().rsplit(")", 1)[1].split()[0] == "Z": return False + except (FileNotFoundError, ProcessLookupError): + return False return True +@pytest.mark.parametrize("error", [FileNotFoundError, ProcessLookupError]) +def test_pid_probe_handles_process_reaped_during_stat_read(monkeypatch, error) -> None: + monkeypatch.setattr(sys, "platform", "linux") + monkeypatch.setattr(os, "kill", lambda _pid, _signal: None) + monkeypatch.setattr(Path, "exists", lambda _path: True) + + def reaped(_path): + raise error("process reaped after existence check") + + monkeypatch.setattr(Path, "read_text", reaped) + assert not _pid_running(12345) + + async def _pid_stopped(pid: int, *, timeout: float = 6) -> None: deadline = time.monotonic() + timeout while _pid_running(pid) and time.monotonic() < deadline: