fix(desktop): don't reap the healthy standalone gateway on Windows desktop startup

This commit is contained in:
butbutbutbutbutbut
2026-08-16 01:42:27 +08:00
committed by kshitij
parent e7f6e22c3b
commit 1ec4b569f2
3 changed files with 30 additions and 4 deletions
+10 -1
View File
@@ -2288,10 +2288,19 @@ def _reap_unsupervised_gateway_orphans(extra_exclude: set | None = None) -> bool
# the Scheduled-Task bootstrap's argv (``gateway run``) matches the
# gateway scan — killing that bootstrap takes the detached gateway it
# spawned down with it.
#
# Probe the record with cleanup_stale=False so the sweep never deletes
# the registration it is about to use as exclusion evidence. With the
# default cleanup, a record that fails liveness validation is unlinked
# mid-sweep; the recorded PID then never joins the exclusion set, and a
# healthy standalone gateway (no service supervisor — e.g. `hermes
# gateway run` on Windows) is hard-killed by the sweep. On Windows
# SIGTERM is TerminateProcess, so the gateway's own planned-stop watcher
# never gets a chance to shut down gracefully.
try:
from gateway.status import get_running_pid
recorded = get_running_pid()
recorded = get_running_pid(cleanup_stale=False)
if recorded and recorded > 0:
own.add(recorded)
try:
+14 -1
View File
@@ -471,10 +471,23 @@ async def _lifespan(app: "FastAPI"):
# the old gateway to launchd (PPID=1). It keeps holding the QQ
# WebSocket, and a newly forked gateway then races the same credential,
# splitting messages across parallel session trees (#77276).
#
# Only reap when no live, registered gateway is in the record: a
# healthy standalone gateway (launched via `hermes gateway run`, no
# service supervisor) is not an orphan, and the sweep would kill it.
# On Windows every layer of the launcher chain (stub -> venv python ->
# runtime python) carries "gateway run" in its command line, so
# find_gateway_pids() matches processes the pidfile exclusion cannot
# see, and os.kill(SIGTERM) is a hard TerminateProcess — the
# gateway's planned-stop watcher (0.5s poll) has no time to drain.
# cleanup_stale=False keeps the probe from deleting the registration
# record it is checking.
try:
from gateway.status import get_running_pid
from hermes_cli.gateway import _reap_unsupervised_gateway_orphans
_reap_unsupervised_gateway_orphans()
if get_running_pid(cleanup_stale=False) is None:
_reap_unsupervised_gateway_orphans()
except Exception:
_log.exception("Desktop startup: orphan gateway reap failed")
+6 -2
View File
@@ -677,7 +677,9 @@ class TestReapUnsupervisedGatewayOrphansWindows:
self._install_fake_psutil(monkeypatch, [recorded, bootstrap])
# get_running_pid() returns the recorded healthy gateway PID.
monkeypatch.setattr("gateway.status.get_running_pid", lambda: recorded_pid)
monkeypatch.setattr(
"gateway.status.get_running_pid", lambda cleanup_stale=True: recorded_pid
)
# find_gateway_pids returns the recorded PID, its bootstrap parent
# and a real orphan. The reaper should only kill the orphan.
@@ -720,7 +722,9 @@ class TestReapUnsupervisedGatewayOrphansWindows:
recorded = SimpleNamespace(pid=recorded_pid, parent=lambda: bootstrap)
self._install_fake_psutil(monkeypatch, [recorded, bootstrap])
monkeypatch.setattr("gateway.status.get_running_pid", lambda: recorded_pid)
monkeypatch.setattr(
"gateway.status.get_running_pid", lambda cleanup_stale=True: recorded_pid
)
# find_gateway_pids would return the recorded PID and its bootstrap
# parent, but both are excluded.