From 6330b748de8d34f27447b95034e58612b8124de6 Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Sun, 18 Oct 2026 15:32:18 +0800 Subject: [PATCH] test(shell): handle process reaping during PID checks --- tests/tools/test_shell_execution_budgets.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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: