From 9488950a9d84c142f3f0604ea4f146b511bdc666 Mon Sep 17 00:00:00 2001 From: Kshitij Kapoor Date: Sat, 22 Aug 2026 20:07:09 +0530 Subject: [PATCH] fix(gateway): build reaper exclusion from raw registration records MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /simplify-code efficiency reviewer (verified): cleanup_stale=False does NOT deliver the exclusion the salvaged fix intended — get_running_pid returns None whenever a record fails liveness VALIDATION (start-time mismatch, argv drift, lock hiccup) regardless of the flag, which only controls unlinking. In exactly the at-risk scenario the recorded PID still never joined the exclusion set. Exclusion evidence now comes from the RAW pidfile + lock records (no validation, no unlink side effects); the validated non-destructive probe is kept for the runtime-status fallback PID. For a KILL exclusion list this is strictly safer: a stale recorded PID at worst spares one process for one sweep, while a validation false-negative would TerminateProcess a live gateway. Regression test reworked to drive the real function semantics (raw record present, validation rejects); mutation-checked: removing the raw-record read fails the test. --- hermes_cli/gateway.py | 38 +++++++++++++++++++-------- tests/hermes_cli/test_gateway.py | 44 +++++++++++++++++++------------- 2 files changed, 54 insertions(+), 28 deletions(-) diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py index b20b1731b8..8d0fd26721 100644 --- a/hermes_cli/gateway.py +++ b/hermes_cli/gateway.py @@ -2289,19 +2289,37 @@ def _reap_unsupervised_gateway_orphans(extra_exclude: set | None = None) -> bool # 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 + # Exclusion evidence comes from the RAW registration record, not the + # liveness-validated probe. ``get_running_pid`` (any flags) returns + # None whenever a record fails validation — start-time mismatch after + # PID-reuse checks, argv drift, lock hiccups — which is exactly when 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. + # gateway run` on Windows) is at risk: its PID never joins the + # exclusion set and the sweep hard-kills it. On Windows SIGTERM is + # TerminateProcess, so the gateway's planned-stop watcher never gets a + # chance to drain. Reading the raw pidfile + lock records (no + # validation, no unlink side effects) is strictly safer for a KILL + # exclusion list: a stale recorded PID at worst spares one process this + # sweep, while a validation false-negative would kill a live gateway. + # The validated probe is still consulted for the runtime-status + # fallback PID it can surface when no pidfile exists. try: - from gateway.status import get_running_pid + from gateway.status import ( + _pid_from_record, + _read_gateway_lock_record, + _read_pid_record, + get_running_pid, + ) - recorded = get_running_pid(cleanup_stale=False) - if recorded and recorded > 0: + recorded_pids = set() + for _record in (_read_pid_record(), _read_gateway_lock_record()): + _raw_pid = _pid_from_record(_record) + if _raw_pid and _raw_pid > 0: + recorded_pids.add(_raw_pid) + _probed = get_running_pid(cleanup_stale=False) + if _probed and _probed > 0: + recorded_pids.add(_probed) + for recorded in recorded_pids: own.add(recorded) try: import psutil # type: ignore diff --git a/tests/hermes_cli/test_gateway.py b/tests/hermes_cli/test_gateway.py index 50671f85f1..05b03c340f 100644 --- a/tests/hermes_cli/test_gateway.py +++ b/tests/hermes_cli/test_gateway.py @@ -746,14 +746,18 @@ class TestReapUnsupervisedGatewayOrphansWindows: assert result is False # no orphans reaped assert killed_pids == [] # nothing was killed - def test_windows_probe_never_cleans_stale_registration(self, monkeypatch): - """The reaper's registration probe must pass cleanup_stale=False. + def test_windows_raw_record_supplies_exclusion_when_validation_fails( + self, monkeypatch + ): + """A registration that fails liveness VALIDATION must still shield + the recorded PID from the sweep. - With the destructive default, a registration that fails liveness - validation is unlinked mid-sweep, the recorded PID never joins the - exclusion set, and a healthy standalone gateway (`hermes gateway - run`, no service supervisor) is hard-killed. On Windows SIGTERM is - TerminateProcess — no graceful drain (#87158). + get_running_pid returns None whenever the record fails validation + (start-time mismatch, argv drift, lock hiccup) — regardless of + cleanup_stale, which only controls unlinking. The exclusion set is + therefore built from the RAW pidfile/lock records: a stale recorded + PID at worst spares one process, while a validation false-negative + would TerminateProcess a healthy standalone gateway (#87158). """ recorded_pid = 52615 @@ -764,17 +768,21 @@ class TestReapUnsupervisedGatewayOrphansWindows: recorded = SimpleNamespace(pid=recorded_pid, parent=lambda: None) self._install_fake_psutil(monkeypatch, [recorded]) - # Simulate the failing-liveness-validation registration: the probe - # only returns the recorded PID when asked NOT to clean up; the - # destructive default path would unlink the record and return None. - seen_kwargs = [] + # The raw record is present on disk; the validated probe rejects it. + monkeypatch.setattr( + "gateway.status._read_pid_record", lambda *a, **k: {"pid": recorded_pid} + ) + monkeypatch.setattr( + "gateway.status._read_gateway_lock_record", lambda *a, **k: None + ) + probe_kwargs = [] - def probing_get_running_pid(cleanup_stale=True): - seen_kwargs.append(cleanup_stale) - return None if cleanup_stale else recorded_pid + def failing_validation(*a, cleanup_stale=True, **k): + probe_kwargs.append(cleanup_stale) + return None monkeypatch.setattr( - "gateway.status.get_running_pid", probing_get_running_pid + "gateway.status.get_running_pid", failing_validation ) monkeypatch.setattr( gateway, @@ -791,9 +799,9 @@ class TestReapUnsupervisedGatewayOrphansWindows: result = gateway._reap_unsupervised_gateway_orphans() - assert seen_kwargs == [False], ( - "reaper probe must use cleanup_stale=False so the sweep never " - f"deletes its own exclusion evidence, got {seen_kwargs}" + assert probe_kwargs == [False], ( + "the fallback probe must still be non-destructive so the sweep " + f"never unlinks the record it just read, got {probe_kwargs}" ) assert result is False assert killed_pids == [] # the standalone gateway survived