test(shell): handle process reaping during PID checks

This commit is contained in:
Xubin Ren committed 2026-10-18 15:55:24 +08:00
1 parent 8adab28aee
commit 6330b748de
1 file changed
+16 -1
+16 -1
View File
@@ -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: