Skip to content

The agent that grows with you

fix(gateway): SIGUSR2 dumps thread stacks without killing the gateway

teknium14da4cc5c370d
diff --git a/gateway/run_startup.py b/gateway/run_startup.py
index e6684c1ca19..cd06d6d4987 100644
--- a/gateway/run_startup.py
+++ b/gateway/run_startup.py
@@ -731,11 +731,13 @@ class GatewayStartupMixin:
with _log_suppressed(logging.DEBUG, "faulthandler.enable() unavailable", exc_info=True):
faulthandler.enable(file=self._open_faulthandler_log(), all_threads=True)
# SIGUSR2 stack dump to file for service managers that drop stderr; POSIX-only.
+ # chain=False: SIGUSR2's default disposition is "terminate", so chaining to it
+ # dumps the stacks and then kills the gateway the operator was trying to inspect.
_sigusr2 = getattr(signal, "SIGUSR2", None)
if _sigusr2 is not None and hasattr(faulthandler, "register"):
with _log_suppressed(logging.DEBUG, "Could not set up faulthandler file logging", exc_info=True):
faulthandler.register(
- _sigusr2, file=self._open_faulthandler_log(), all_threads=True, chain=True,
+ _sigusr2, file=self._open_faulthandler_log(), all_threads=True, chain=False,
)
def _start_log_startup_environment(self) -> None:
diff --git a/tests/gateway/test_faulthandler_no_stderr.py b/tests/gateway/test_faulthandler_no_stderr.py
index 7548ef54852..4d80553b3d6 100644
--- a/tests/gateway/test_faulthandler_no_stderr.py
+++ b/tests/gateway/test_faulthandler_no_stderr.py
@@ -32,3 +32,42 @@ def test_faulthandler_enable_falls_back_when_stderr_is_none(tmp_path):
sys.stderr = real_stderr
if was_enabled:
faulthandler.enable()
+
+
+_SIGUSR2_CHILD = """
+import os, signal, sys, time
+from types import SimpleNamespace
+sys.path.insert(0, sys.argv[2])
+from gateway.run_startup import GatewayStartupMixin
+runner = object.__new__(GatewayStartupMixin)
+runner.config = SimpleNamespace(log_dir=sys.argv[1])
+runner._start_install_faulthandler()
+print("ready", flush=True)
+time.sleep(30)
+"""
+
+
[email protected](not hasattr(__import__("signal"), "SIGUSR2"), reason="POSIX-only signal")
+def test_sigusr2_stack_dump_leaves_the_gateway_running(tmp_path):
+ """#110437 — SIGUSR2 must dump stacks to the log and return; chaining to the
+ signal's default disposition (terminate) killed the process being inspected."""
+ import os
+ import signal
+ import subprocess
+ import time
+
+ repo_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+ child = subprocess.Popen(
+ [sys.executable, "-c", _SIGUSR2_CHILD, str(tmp_path), repo_root],
+ stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True,
+ )
+ try:
+ assert child.stdout.readline().strip() == "ready"
+ child.send_signal(signal.SIGUSR2)
+ time.sleep(0.5)
+ assert child.poll() is None, f"gateway died on SIGUSR2 (rc={child.returncode})"
+ finally:
+ child.kill()
+ child.wait()
+ dump = (tmp_path / "gateway_faulthandler.log").read_text(encoding="utf-8")
+ assert "Thread 0x" in dump or "Current thread" in dump
diff --git a/website/docs/user-guide/messaging/index.md b/website/docs/user-guide/messaging/index.md
index 58516bf71d3..6953dd1f7eb 100644
--- a/website/docs/user-guide/messaging/index.md
+++ b/website/docs/user-guide/messaging/index.md
@@ -177,6 +177,13 @@ hermes gateway status # Check default service status
hermes gateway status --system # Linux only: inspect the system service explicitly
```
+### Stack dump on demand (`SIGUSR2`)
+
+On Linux and macOS, `kill -USR2 <gateway pid>` appends a dump of every thread's
+stack to `~/.hermes/logs/gateway_faulthandler.log` and the gateway keeps
+running — use it to see what a stalled or misbehaving gateway is doing without
+restarting it.
+
### Optional Linux event-loop watchdog
A systemd-managed gateway can opt into process recovery when Python's asyncio