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

hermes update stops a manual dashboard, respawns it, then asks the spawn
ledger which pre-update serve/dashboard processes are still alive. The
stop check (gateway.status._pid_exists) counts a zombie as gone, but the
ledger's non-strict liveness check (_pid_alive_matches -> _same_incarnation)
only compared create_time, which a zombie keeps until its parent reaps it.
When the survivor probe ran inside that window the old dashboard was booked
"unaccounted" and the update exited 1 although the replacement dashboard was
serving. The handoff e2e (test_update_exit_status_matches_what_came_back)
hits it intermittently since #124940 made the respawn work.

Treat a zombie as dead on the non-strict path too, like the strict path and
_pid_exists already do. Windows has no zombies, so it skips the status read.
This commit is contained in:
kshitijk4poor
2026-09-28 02:07:19 +05:30
committed by Teknium
parent 78999579c8
commit 06fbb71c26
2 changed files with 30 additions and 1 deletions

View File

@@ -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:

View File

@@ -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]
@pytest.mark.platforms("posix")
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}):