The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): launchd wrapper forwards SIGUSR1 so sibling restarts are planned exits (#101426)
kshitijk4poor151497688c72
diff --git a/hermes_cli/stderr_timestamp.py b/hermes_cli/stderr_timestamp.pyindex bc9d1c9fd06..4f1cb232594 100644--- a/hermes_cli/stderr_timestamp.py+++ b/hermes_cli/stderr_timestamp.py@@ -49,7 +49,18 @@ def _install_signal_forwarders(proc: subprocess.Popen[bytes]) -> dict[int, objecpassprevious: dict[int, object] = {}- for signum in (signal.SIGTERM, signal.SIGINT, getattr(signal, "SIGHUP", None)):+ # SIGUSR1 is the gateway's drain-aware restart request. launchd owns THIS wrapper's PID,+ # so `hermes update` signals us, not the gateway; an unforwarded SIGUSR1 kills the wrapper+ # (Python's default action), launchd tears the group down with SIGTERM and applies its+ # ~60 s crash back-off per sibling profile (#101426).+ forwarded = (+ signal.SIGTERM,+ signal.SIGINT,+ getattr(signal, "SIGHUP", None),+ getattr(signal, "SIGUSR1", None),+ getattr(signal, "SIGUSR2", None),+ )+ for signum in forwarded:if signum is not None:try:previous[signum] = signal.getsignal(signum)diff --git a/tests/hermes_cli/test_stderr_timestamp.py b/tests/hermes_cli/test_stderr_timestamp.pyindex d67cb6cac26..c4ef78cc024 100644--- a/tests/hermes_cli/test_stderr_timestamp.py+++ b/tests/hermes_cli/test_stderr_timestamp.py@@ -213,3 +213,43 @@ def test_main_maps_gateway_ex_config_to_clean_stop(tmp_path):assert rc_restart == GATEWAY_SERVICE_RESTART_EXIT_CODEassert rc_other == GATEWAY_FATAL_CONFIG_EXIT_CODE++[email protected]_only+def test_wrapper_forwards_sigusr1_restart_request_to_child(tmp_path):+ """Regression for #101426: launchd owns the wrapper's PID, so ``hermes update`` sends its+ drain-aware SIGUSR1 to the wrapper. It must reach the gateway child and the wrapper must+ report the child's planned exit code — not die of the signal itself (which makes launchd+ treat the restart as a crash and apply its back-off to every sibling profile)."""+ import os+ import signal+ import subprocess+ import time++ log_path = tmp_path / "gateway.error.log"+ ready = tmp_path / "ready"+ child = (+ "import os, signal, sys, time, pathlib\n"+ f"signal.signal(signal.SIGUSR1, lambda *_: (sys.stderr.write('restart requested\\n'), sys.exit({GATEWAY_SERVICE_RESTART_EXIT_CODE})))\n"+ f"pathlib.Path({str(ready)!r}).write_text('1')\n"+ "time.sleep(20)\n"+ "sys.exit(1)\n"+ )+ wrapper = subprocess.Popen(+ [sys.executable, "-m", "hermes_cli.stderr_timestamp", "--error-log", str(log_path), "--",+ sys.executable, "-c", child],+ stderr=subprocess.DEVNULL,+ )+ try:+ deadline = time.monotonic() + 10+ while not ready.exists():+ assert time.monotonic() < deadline, "child never started"+ time.sleep(0.05)+ os.kill(wrapper.pid, signal.SIGUSR1)+ rc = wrapper.wait(timeout=10)+ finally:+ if wrapper.poll() is None:+ wrapper.kill()++ assert rc == GATEWAY_SERVICE_RESTART_EXIT_CODE+ assert "restart requested" in log_path.read_text(encoding="utf-8")