Skip to content

The agent that grows with you

fix(update): a stopped-but-unreaped dashboard is no longer a pre-update survivor

kshitijk4poor06fbb71c26ae
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.py
index c1d95206439..d220b8a4eee 100644
--- a/hermes_cli/process_identity.py
+++ b/hermes_cli/process_identity.py
@@ -192,7 +192,11 @@ 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)
- return _same_incarnation(proc, create_time)
+ # A zombie keeps its create_time until its parent reaps it, but it is already dead: the
+ # dashboard stop check (``gateway.status._pid_exists``) books it stopped, so the ledger must
+ # not report it as a live pre-update survivor. Windows has no zombies (and status() is slow there).
+ return _same_incarnation(proc, create_time) and (
+ os.name == "nt" or proc.status() != getattr(psutil, "STATUS_ZOMBIE", "zombie"))
except psutil.NoSuchProcess:
return False
except Exception:
diff --git a/tests/hermes_cli/test_process_identity.py b/tests/hermes_cli/test_process_identity.py
index ad3c18dc70b..603a58dc7f7 100644
--- a/tests/hermes_cli/test_process_identity.py
+++ b/tests/hermes_cli/test_process_identity.py
@@ -17,7 +17,9 @@ from __future__ import annotations
import json
import os
import stat
+import subprocess
import sys
+import time
import types
from pathlib import Path
from unittest.mock import MagicMock, patch
@@ -228,6 +230,29 @@ def test_ledger_entries_filters_dead_reused_and_foreign(tmp_path):
assert [e["pid"] for e in live] == [100]
+def test_ledger_entries_excludes_a_killed_but_unreaped_process(tmp_path):
+ """A zombie is dead even though it keeps its create_time until reaped: ``hermes update``
+ books the stopped dashboard gone, then must not find it again as a pre-update survivor."""
+ import psutil
+
+ child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"])
+ try:
+ created = psutil.Process(child.pid).create_time()
+ child.terminate()
+ deadline = time.monotonic() + 10
+ while psutil.Process(child.pid).status() != psutil.STATUS_ZOMBIE:
+ assert time.monotonic() < deadline, "child never became a zombie"
+ time.sleep(0.05)
+ ledger = tmp_path / "spawn-ledger.json"
+ ledger.write_text(json.dumps([_entry(child.pid, created, purpose="dashboard")]), encoding="utf-8")
+ with patch.object(pi, "_ledger_path", return_value=ledger):
+ assert pi.ledger_entries(project_root=Path("/x/install")) == []
+ finally:
+ child.kill()
+ child.wait()
+
+
def test_spawner_is_dead_tristate():
fake = _fake_psutil({500: 5.0})
with patch.dict(sys.modules, {"psutil": fake}):