test(shell): handle process reaping during PID checks
This commit is contained in:
1 parent
8adab28aee
commit
6330b748de
1 file changed
+16
-1
@@ -91,12 +91,27 @@ def _pid_running(pid: int) -> bool:
|
|||||||
return False
|
return False
|
||||||
# A killed grandchild can remain a zombie until its new parent reaps it.
|
# A killed grandchild can remain a zombie until its new parent reaps it.
|
||||||
stat = Path(f"/proc/{pid}/stat")
|
stat = Path(f"/proc/{pid}/stat")
|
||||||
with suppress(FileNotFoundError):
|
try:
|
||||||
if stat.exists() and stat.read_text().rsplit(")", 1)[1].split()[0] == "Z":
|
if stat.exists() and stat.read_text().rsplit(")", 1)[1].split()[0] == "Z":
|
||||||
return False
|
return False
|
||||||
|
except (FileNotFoundError, ProcessLookupError):
|
||||||
|
return False
|
||||||
return True
|
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:
|
async def _pid_stopped(pid: int, *, timeout: float = 6) -> None:
|
||||||
deadline = time.monotonic() + timeout
|
deadline = time.monotonic() + timeout
|
||||||
while _pid_running(pid) and time.monotonic() < deadline:
|
while _pid_running(pid) and time.monotonic() < deadline:
|
||||||
|
|||||||
Reference in new issue
Block a user