Skip to content

The agent that grows with you

fix(process-identity): an exited, unreaped process is not alive

teknium112615c8faabe
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.py
index c1d95206439..f87793dafb1 100644
--- a/hermes_cli/process_identity.py
+++ b/hermes_cli/process_identity.py
@@ -192,6 +192,9 @@ def _pid_alive_matches(pid: int, create_time: Optional[float], *, strict: bool =
if strict:
return (create_time is not None and proc.create_time() == create_time
and proc.is_running() and proc.status() != psutil.STATUS_ZOMBIE)
+ # An exited process its parent has not reaped yet runs nothing and holds nothing: dead.
+ if proc.status() == psutil.STATUS_ZOMBIE:
+ return False
return _same_incarnation(proc, create_time)
except psutil.NoSuchProcess:
return False
diff --git a/tests/hermes_cli/test_process_identity.py b/tests/hermes_cli/test_process_identity.py
index ad3c18dc70b..d65fb549002 100644
--- a/tests/hermes_cli/test_process_identity.py
+++ b/tests/hermes_cli/test_process_identity.py
@@ -40,9 +40,10 @@ def _fake_psutil(procs: dict[int, float]):
proc = MagicMock()
proc.pid = pid
proc.create_time.return_value = procs[pid]
+ proc.status.return_value = "running"
return proc
- return types.SimpleNamespace(Process=_process, NoSuchProcess=_FakeNoSuchProcess)
+ return types.SimpleNamespace(Process=_process, NoSuchProcess=_FakeNoSuchProcess, STATUS_ZOMBIE="zombie")
# ---------------------------------------------------------------------------
@@ -284,3 +285,25 @@ def test_desktop_ssh_backend_spawn_shape_is_desktop_owned(monkeypatch):
assert pi.is_desktop_owned_backend() is True
# The bare inherited flag (a Desktop terminal pane running `hermes serve`) is still not ownership.
assert pi.is_desktop_owned_backend(["serve", "--host", "127.0.0.1", "--port", "0"]) is False
+
+
[email protected](sys.platform == "win32", reason="zombies are a POSIX process state")
+def test_an_exited_unreaped_child_is_not_alive():
+ """A zombie runs nothing and holds nothing, but psutil still resolves it with its original
+ create_time. Read as alive, a dashboard the updater had just stopped stayed a "pre-update
+ survivor" until its parent happened to reap it, and the update exited 1 (handoff e2e)."""
+ import subprocess
+ import time
+
+ import psutil
+
+ child = subprocess.Popen([sys.executable, "-c", "pass"])
+ try:
+ created = psutil.Process(child.pid).create_time()
+ deadline = time.monotonic() + 10
+ while psutil.Process(child.pid).status() != psutil.STATUS_ZOMBIE:
+ assert time.monotonic() < deadline, "child never exited"
+ time.sleep(0.02)
+ assert pi._pid_alive_matches(child.pid, created) is False
+ finally:
+ child.wait()