Skip to content

The agent that grows with you

fix(update): snapshot venv launchers before the gateway drain

iso2kxd358edd9169e
diff --git a/hermes_cli/update_cmd.py b/hermes_cli/update_cmd.py
index 2e0895ba778..a7b51e120ad 100644
--- a/hermes_cli/update_cmd.py
+++ b/hermes_cli/update_cmd.py
@@ -2869,6 +2869,20 @@ def _pause_windows_gateways_for_update() -> dict | None:
mapped_pids.append(int(pid))
_write_update_planned_stop_marker(Path(proc.path), int(pid))
+ # Resolve each mapped worker's venv-side launcher BEFORE draining: the
+ # drain stops tracking a PID exactly when it dies, so a gracefully
+ # drained worker is gone by the time the wait returns — and a dead pid's
+ # parent cannot be recovered (psutil raises NoSuchProcess). The snapshot
+ # is stopped after the drain alongside the survivors.
+ #
+ # Why launchers matter: the drain targets the PID that wrote the PID
+ # file (the uv-side worker). On Windows that worker's parent is usually
+ # the venv-side ``python.exe`` launcher, which keeps venv ``.pyd`` files
+ # mapped and is what ``_detect_venv_python_processes()`` reports
+ # downstream. Left alive, it trips the venv-holder guard and aborts the
+ # update even though the gateway itself is stopped.
+ launcher_pids = _m()._venv_launcher_ancestors(mapped_pids)
+
print("→ Stopping Windows gateway process(es) before updating Hermes...")
try:
drain_timeout = max(float(_get_restart_drain_timeout()), 1.0)
@@ -2895,16 +2909,11 @@ def _pause_windows_gateways_for_update() -> dict | None:
logger.debug("Could not capture argv for unmapped gateway %s: %s", pid, exc)
unmapped.append({"pid": int(pid), "argv": argv})
- # A gateway's graceful drain above targets the PID that wrote the PID file
- # (the uv-side worker). On Windows that worker's parent is usually the
- # venv-side ``python.exe`` launcher, which keeps venv ``.pyd`` files mapped
- # and is what ``_detect_venv_python_processes()`` reports downstream. Left
- # alive, it trips the venv-holder guard and aborts the update even though
- # the gateway itself is stopped. Force-kill those launchers alongside the
- # survivors; ``terminate_pid(force=True)`` is a tree kill, so a launcher
- # that outlived its worker takes any stragglers with it.
- launcher_pids = _m()._venv_launcher_ancestors(mapped_pids)
-
+ # Stop drain survivors, unmapped gateways, and the pre-drain launcher
+ # snapshot. ``terminate_pid(force=True)`` is a tree kill, so a launcher
+ # that outlived its worker takes any stragglers with it; a launcher that
+ # already exited with its drained worker raises ProcessLookupError below
+ # and is skipped.
force_killed = []
for pid in sorted(set(survivors).union(unmapped_pids).union(launcher_pids)):
try:
diff --git a/tests/hermes_cli/test_update_concurrent_quarantine.py b/tests/hermes_cli/test_update_concurrent_quarantine.py
index 92d381f8641..784c9f9bc41 100644
--- a/tests/hermes_cli/test_update_concurrent_quarantine.py
+++ b/tests/hermes_cli/test_update_concurrent_quarantine.py
@@ -283,16 +283,23 @@ def test_pause_windows_gateways_for_update_stops_profile_and_unmapped_pids(
# ---------------------------------------------------------------------------
-def _fake_psutil_tree(tree, venv_exe, worker_exe):
+def _fake_psutil_tree(tree, venv_exe, worker_exe, dead=None):
"""Build a psutil stand-in where ``tree`` maps worker pid -> parent pid.
Parents whose pid is even are venv-side (``venv_exe``); odd parents are
- unrelated ancestors (``worker_exe``) that must NOT be returned.
+ unrelated ancestors (``worker_exe``) that must NOT be returned. Pids in
+ ``dead`` (a live reference — later additions count) are uninspectable:
+ construction raises, exactly like psutil.NoSuchProcess for an exited
+ process.
"""
+ dead_set = dead if dead is not None else set()
+
class FakeProc:
def __init__(self, pid):
self.pid = pid
+ if pid in dead_set:
+ raise ValueError(f"process {pid} has exited")
if pid not in tree and pid not in tree.values():
raise ValueError(f"no such pid {pid}")
@@ -375,13 +382,26 @@ def test_pause_kill_set_covers_venv_guard_abort_set(
gateway_mod, "find_profile_gateway_processes", lambda **_k: [profile_proc]
)
monkeypatch.setattr(gateway_mod, "_get_restart_drain_timeout", lambda: 0.1)
- # Graceful drain succeeds: the worker exits, leaving zero survivors. This is
+ # Graceful drain succeeds: the worker exits, leaving zero survivors — and
+ # an exited worker is UNINSPECTABLE afterwards, exactly like the real
+ # process table. Resolving the launcher after this point is impossible,
+ # so the pause must snapshot launcher ancestors before draining. This is
# precisely the case that used to leave the launcher alive and abort.
+ drained_dead: set[int] = set()
+
+ def _drain_marks_workers_dead(pids, *, timeout):
+ drained_dead.update(int(p) for p in pids)
+ return set()
+
monkeypatch.setattr(
- cli_main, "_wait_for_windows_update_gateway_exit", lambda pids, *, timeout: set()
+ cli_main,
+ "_wait_for_windows_update_gateway_exit",
+ _drain_marks_workers_dead,
)
- fake = _fake_psutil_tree({worker_pid: launcher_pid}, venv_exe, worker_exe)
+ fake = _fake_psutil_tree(
+ {worker_pid: launcher_pid}, venv_exe, worker_exe, dead=drained_dead
+ )
monkeypatch.setitem(sys.modules, "psutil", fake)
terminated = []