Skip to content

The agent that grows with you

fix(review): gateway restart's orphan reap is best-effort and runs on every restarting path (#125394)

teknium1e824425e8c15
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index b32f158ce28..177de214732 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -5492,15 +5492,19 @@ def _cmd_restart(args):
_restart_all(system)
return
+ # Reap orphans only past the refusal guards above (#125394): the Desktop backend used to reap
+ # before spawning this command, so a refused restart still cost the profile its gateway. From
+ # here every path restarts (service manager, external-supervisor hand-back, manual fallback),
+ # and the reap stays best-effort: a scan failure must not abort the restart (#77276).
+ try:
+ _reap_unsupervised_gateway_orphans()
+ except Exception as exc:
+ logger.debug("orphan reap before gateway restart failed: %s", exc)
+
# The Windows restart path handles both registered installs and detached restarts.
kind = _installed_service_kind_for(is_windows)
service_configured = kind is not None and (kind != "windows" or _gw_windows().is_installed())
if kind is not None:
- # Reap orphans only past the refusal guards above (#125394): the Desktop backend used to
- # reap before spawning this command, so a refused restart still cost the profile its
- # gateway. A service-manager restart never sweeps orphans itself (#77276); the manual
- # fallback below reaps inside stop_profile_gateway().
- _reap_unsupervised_gateway_orphans()
swallow = (RuntimeError, OSError) if kind == "windows" else ()
try:
_service_call(kind, "restart", system)
diff --git a/tests/hermes_cli/test_spawn_gateway_restart_reap.py b/tests/hermes_cli/test_spawn_gateway_restart_reap.py
index 16dd77c4b22..5ad7dba45cc 100644
--- a/tests/hermes_cli/test_spawn_gateway_restart_reap.py
+++ b/tests/hermes_cli/test_spawn_gateway_restart_reap.py
@@ -55,3 +55,22 @@ def test_cli_restart_reaps_after_the_refusal_guard(monkeypatch):
gw._cmd_restart(SimpleNamespace(system=False, all=False, force=False))
assert order == ["guard", "reap", "restart"]
+
+
+def test_cli_restart_reap_failure_does_not_block_restart(monkeypatch):
+ """The reap is best-effort: a scan error must not abort the restart it precedes."""
+ order: list[str] = []
+ monkeypatch.setattr(gw, "_refuse_from_inside_gateway", lambda *a, **k: None)
+ monkeypatch.setattr(gw, "_guard_named_profile_under_multiplexer", lambda **k: None)
+ monkeypatch.setattr(gw, "_dispatch_via_service_manager_if_s6", lambda *a, **k: False)
+ monkeypatch.setattr(gw, "_installed_service_kind_for", lambda windows: "launchd")
+
+ def _boom(*a, **k):
+ raise OSError("permission denied")
+
+ monkeypatch.setattr(gw, "_reap_unsupervised_gateway_orphans", _boom)
+ monkeypatch.setattr(gw, "_service_call", lambda *a, **k: order.append("restart"))
+
+ gw._cmd_restart(SimpleNamespace(system=False, all=False, force=False))
+
+ assert order == ["restart"]