test(shell): treat vanished Linux proc entries as stopped
This commit is contained in:
1 parent
da673d3b5a
commit
42276f4884
1 file changed
+9
-6
@@ -89,21 +89,24 @@ def _pid_running(pid: int) -> bool:
|
|||||||
os.kill(pid, 0)
|
os.kill(pid, 0)
|
||||||
except ProcessLookupError:
|
except ProcessLookupError:
|
||||||
return False
|
return False
|
||||||
|
if sys.platform != "linux":
|
||||||
|
return True
|
||||||
# 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")
|
||||||
try:
|
try:
|
||||||
if stat.exists() and stat.read_text().rsplit(")", 1)[1].split()[0] == "Z":
|
return stat.read_text().rsplit(")", 1)[1].split()[0] != "Z"
|
||||||
return False
|
|
||||||
except (FileNotFoundError, ProcessLookupError):
|
except (FileNotFoundError, ProcessLookupError):
|
||||||
return False
|
return False
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("error", [FileNotFoundError, ProcessLookupError])
|
@pytest.mark.parametrize(
|
||||||
def test_pid_probe_handles_process_reaped_during_stat_read(monkeypatch, error) -> None:
|
("error", "stat_exists"),
|
||||||
|
[(FileNotFoundError, True), (ProcessLookupError, True), (FileNotFoundError, False)],
|
||||||
|
)
|
||||||
|
def test_pid_probe_handles_process_reaped_during_stat_read(monkeypatch, error, stat_exists) -> None:
|
||||||
monkeypatch.setattr(sys, "platform", "linux")
|
monkeypatch.setattr(sys, "platform", "linux")
|
||||||
monkeypatch.setattr(os, "kill", lambda _pid, _signal: None)
|
monkeypatch.setattr(os, "kill", lambda _pid, _signal: None)
|
||||||
monkeypatch.setattr(Path, "exists", lambda _path: True)
|
monkeypatch.setattr(Path, "exists", lambda _path: stat_exists)
|
||||||
|
|
||||||
def reaped(_path):
|
def reaped(_path):
|
||||||
raise error("process reaped after existence check")
|
raise error("process reaped after existence check")
|
||||||
|
|||||||
Reference in new issue
Block a user