Skip to content

The agent that grows with you

fix(update): the pause force-kills only the processes it discovered

Teknium2dc8d7799c06
diff --git a/hermes_cli/update_cmd_windows.py b/hermes_cli/update_cmd_windows.py
index 8ef69c9e23c..2e0c1dd7714 100644
--- a/hermes_cli/update_cmd_windows.py
+++ b/hermes_cli/update_cmd_windows.py
@@ -954,6 +954,8 @@ def _pause_windows_gateways_for_update() -> dict | None:
"Could not capture argv for unmapped gateway %s: %s", int(pid))}
for pid in unmapped_pids
]
+ from gateway.status import get_process_start_time
+ born = {int(pid): get_process_start_time(int(pid)) for pid in running_pids} # the processes discovered
intended = {
"resume_needed": True, "unmapped_pids": unmapped_pids, "unmapped": unmapped,
"profiles": {str(profile_processes[pid].profile): int(pid) for pid in running_pids
@@ -972,7 +974,7 @@ def _pause_windows_gateways_for_update() -> dict | None:
pause_record.mark_stop_requested(
intended, running_pids, markers=_planned_stop_markers(running_pids, profile_processes, service_gateway_pids))
profiles = _stop_windows_gateways(running_pids, profile_processes, service_gateway_pids, unmapped_pids, unmapped,
- on_request=lambda pid: pause_record.mark_stop_sent(intended, pid))
+ on_request=lambda pid: pause_record.mark_stop_sent(intended, pid), born=born)
token = {"resume_needed": True, "profiles": profiles, "unmapped_pids": unmapped_pids, "unmapped": unmapped}
# Every profile with ANY live gateway at discovery counts as running: service-supervised ones skip the
# socket pause (absent from ``profiles``) but the SCM restart brings them back, not a cold-start.
@@ -1004,25 +1006,34 @@ def _cold_start_pause_token(adopted: dict | None, claims: list) -> dict | None:
def _stop_windows_gateways(running_pids, profile_processes, service_gateway_pids, unmapped_pids, unmapped,
- on_request=None) -> dict:
+ on_request=None, born=None) -> dict:
"""Socket-pause the mapped gateways, drain, then tree-kill survivors; the paused ``profiles``.
- A force-killed process needs no stop record: dead, it is owed; alive, it was never stopped."""
+ A force-killed process needs no stop record: dead, it is owed; alive, it was never stopped.
+
+ The kill is authorized by each process's birth as discovered (*born*, a mapped gateway's
+ strict PID-file identity first), never re-read after the drain: a PID that exited and was
+ reused meanwhile reads its replacement's own birth. No discovered birth refuses the kill."""
from gateway.status import get_process_start_time, terminate_pid
from hermes_cli.update_cmd import _m
+ mapped = [int(p) for p in running_pids if int(p) not in service_gateway_pids and int(p) in profile_processes]
+ born = {**{int(k): v for k, v in (born or {}).items()},
+ **{p: int(round(float(profile_processes[p].create_time) * 100)) for p in mapped
+ if float(getattr(profile_processes[p], "create_time", 0) or 0) > 0}}
+ # Resolve venv-side launchers before any request: the launcher keeps ``.pyd`` mapped and would trip the
+ # venv-holder guard, so it is killed with the survivors, under the identity it has now.
+ launcher_pids = _m()._venv_launcher_ancestors(mapped)
+ born.update({int(p): get_process_start_time(int(p)) for p in launcher_pids if int(p) not in born})
profiles, mapped_pids, socket_acks = _request_socket_pauses(
running_pids, profile_processes, service_gateway_pids, on_request=on_request)
- # Resolve venv-side launchers BEFORE draining: a dead worker's parent cannot be recovered (NoSuchProcess).
- # The launcher keeps ``.pyd`` mapped and would trip the venv-holder guard; it is killed with the survivors.
- launcher_pids = _m()._venv_launcher_ancestors(mapped_pids)
print("→ Stopping Windows gateway process(es) before updating Hermes...")
drain_timeout = _gateway_drain_timeout(socket_acks)
survivors = _m()._wait_for_windows_update_gateway_exit(mapped_pids, timeout=drain_timeout)
- # Tree-kill survivors, unmapped gateways, and pre-drain launchers; a launcher
- # already gone with its worker raises ProcessLookupError and is skipped.
+ # Tree-kill survivors, unmapped gateways, and pre-request launchers; one already gone raises
+ # ProcessLookupError, one whose birth differs (or was never read) is refused, and both are skipped.
force_killed = []
for pid in sorted(set(survivors).union(unmapped_pids).union(launcher_pids)):
with suppress(ProcessLookupError, PermissionError, OSError):
- terminate_pid(int(pid), force=True, expected_start_time=get_process_start_time(int(pid)))
+ terminate_pid(int(pid), force=True, expected_start_time=born.get(int(pid)))
force_killed.append(int(pid))
if profiles:
print(f" ✓ Paused gateway profile(s): {', '.join(sorted(profiles))}")
diff --git a/tests/hermes_cli/test_update_pause_debt_windows_live.py b/tests/hermes_cli/test_update_pause_debt_windows_live.py
index 95507c05f94..875952d7019 100644
--- a/tests/hermes_cli/test_update_pause_debt_windows_live.py
+++ b/tests/hermes_cli/test_update_pause_debt_windows_live.py
@@ -14,6 +14,7 @@ import os
import subprocess
import sys
import textwrap
+import time
import uuid
from contextlib import suppress
from pathlib import Path
@@ -202,3 +203,35 @@ def test_a_running_service_owes_its_gateway_until_that_gateway_is_ready(tmp_path
def _logs(home: Path) -> str:
return "\n".join(f"--- {p}\n{p.read_text(encoding='utf-8-sig', errors='replace')[-3000:]}"
for p in sorted((home / "logs").glob("*.log")))
+
+
+# --- force-kill identity ------------------------------------------------------------------------
+
[email protected]("discovered", ["this process", "an earlier process at this pid"])
+def test_a_pause_force_kills_only_the_process_it_discovered(tmp_path, monkeypatch, discovered):
+ from hermes_cli import update_cmd_windows as w
+ from hermes_cli.gateway import ProfileGatewayProcess
+
+ home = tmp_path / "home"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setenv("HERMES_RESTART_DRAIN_TIMEOUT", "1")
+ # Never answers its pause, so it survives the drain and reaches the force-kill.
+ victim = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(600)"], stdin=subprocess.DEVNULL)
+ try:
+ born = psutil.Process(victim.pid).create_time()
+ # The gateway discovered at this PID: this very process, or one born earlier that exited
+ # during the drain while Windows handed its PID to the process running now.
+ seen = born if discovered == "this process" else born - 5.0
+ found = ProfileGatewayProcess(profile="r8kill", path=home, pid=victim.pid, create_time=seen)
+ w._stop_windows_gateways([victim.pid], {victim.pid: found}, set(), [], [])
+ deadline = time.monotonic() + 15
+ while victim.poll() is None and time.monotonic() < deadline:
+ time.sleep(0.1)
+ if discovered == "this process":
+ assert victim.poll() is not None, "the discovered gateway survived its force-kill"
+ else:
+ assert victim.poll() is None, "force-kill took a process born after discovery (a reused PID)"
+ finally:
+ subprocess.run(["taskkill", "/T", "/F", "/PID", str(victim.pid)], capture_output=True, check=False)
+ victim.wait(timeout=30)