Skip to content

The agent that grows with you

fix(telemetry): graceful os._exit stops stamp the process exit marker clean

teknium1e50614ed3920
diff --git a/gateway/run.py b/gateway/run.py
index 1fc53559a3b..76765f35173 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -6150,6 +6150,8 @@ def _exit_after_graceful_shutdown(exit_code: int) -> None:
def _mark_exited() -> None:
# Single funnel every graceful exit passes through, so the next boot's unclean-death detector
# fires only for genuine SIGKILL/OOM/VM deaths. Ownership-guarded against an old --replace life.
+ from hermes_cli.observability.shared_metrics_process import stamp_exit
+ stamp_exit("clean") # the exit-metrics marker too: os._exit skips its atexit stamp (never raises)
from gateway.lifecycle_ledger import mark_exited
mark_exited(exit_code, reason="graceful_shutdown")
diff --git a/hermes_cli/web_server_lifecycle.py b/hermes_cli/web_server_lifecycle.py
index 89f59725e84..e51196af244 100644
--- a/hermes_cli/web_server_lifecycle.py
+++ b/hermes_cli/web_server_lifecycle.py
@@ -340,6 +340,13 @@ def _start_parent_death_watchdog() -> None:
)
except Exception:
pass
+ # Reaping ourselves when the owning Desktop is gone is the designed stop, not a kill; os._exit
+ # skips the atexit stamp that would otherwise say so.
+ try:
+ from hermes_cli.observability.shared_metrics_process import stamp_exit
+ stamp_exit("clean")
+ except Exception:
+ pass
# os._exit skips every cleanup: a foreground command in its own process group would outlive us.
try:
from tools.environments.base import kill_live_foreground_processes
diff --git a/tests/hermes_cli/test_shared_metrics_reliability.py b/tests/hermes_cli/test_shared_metrics_reliability.py
index dc9e1602fd9..376f9210079 100644
--- a/tests/hermes_cli/test_shared_metrics_reliability.py
+++ b/tests/hermes_cli/test_shared_metrics_reliability.py
@@ -171,3 +171,25 @@ def test_v3_schema_accepts_exactly_the_contract_values():
dims = by_name[metric]["properties"]["dimensions"]["properties"]
assert {field: set(spec["enum"]) for field, spec in dims.items()} == {
field: set(values) for field, values in contract._COUNTER_DIMENSION_VALUES[metric].items()}
+
+
+def test_graceful_gateway_exit_stamps_the_marker_clean(marks, monkeypatch):
+ import gateway.run as gateway_run
+
+ monkeypatch.setattr(process_metrics, "_STATE", {})
+ monkeypatch.setattr(sys, "excepthook", lambda *a: None)
+ monkeypatch.setattr(process_metrics.atexit, "register", lambda *a, **k: None)
+ monkeypatch.setattr(process_metrics.threading, "Thread", lambda **k: SimpleNamespace(start=lambda: None))
+ process_metrics.begin_process("gateway")
+
+ class _Exited(Exception):
+ pass
+
+ def fake_exit(code):
+ raise _Exited(code)
+
+ monkeypatch.setattr(gateway_run.os, "_exit", fake_exit) # os._exit skips atexit: no stamp from there
+ with pytest.raises(_Exited):
+ gateway_run._exit_after_graceful_shutdown(1)
+ marker = process_metrics.markers_dir(marks.home) / f"gateway-{os.getpid()}.json"
+ assert json.loads(marker.read_text())["state"] == "clean"
diff --git a/tui_gateway/entry.py b/tui_gateway/entry.py
index d81f6fa7a98..196db5f9c0e 100644
--- a/tui_gateway/entry.py
+++ b/tui_gateway/entry.py
@@ -104,6 +104,9 @@ def _hard_exit() -> None:
"""The grace timer's ``os._exit``. The flush runs first and the graceful foreground kill
(TERM, wait, KILL) after it, so a SIGTERM-ignoring command is usually still alive here:
SIGKILL its tree now or it outlives us, reparented to init."""
+ with suppress(Exception): # only armed by a termination signal (a requested stop); os._exit skips atexit
+ from hermes_cli.observability.shared_metrics_process import stamp_exit
+ stamp_exit("clean")
with suppress(Exception):
from tools.environments.base import kill_live_foreground_processes
kill_live_foreground_processes(now=True)