Skip to content

The agent that grows with you

fix(tui): the legacy slash-worker watchdog flushes the opt-in metrics before os._exit

kshitijk4poor3460122ede00
diff --git a/tests/hermes_cli/test_shared_metrics_exit_flush.py b/tests/hermes_cli/test_shared_metrics_exit_flush.py
index 72b491b3126..f74bf26576d 100644
--- a/tests/hermes_cli/test_shared_metrics_exit_flush.py
+++ b/tests/hermes_cli/test_shared_metrics_exit_flush.py
@@ -153,6 +153,7 @@ def test_sigtermed_kanban_worker_writes_the_open_sessions_summary(metrics_home,
assert "hermes.session.count" in _metric_names(home)
[email protected](os.name != "posix", reason="the legacy parent watchdog is POSIX-only")
def test_orphaned_slash_worker_writes_the_open_sessions_summary(metrics_home, monkeypatch):
from tui_gateway import slash_worker
@@ -160,10 +161,10 @@ def test_orphaned_slash_worker_writes_the_open_sessions_summary(metrics_home, mo
_finished_task_in_open_session()
_wait_for(home, "hermes.task_run.finished")
exited = threading.Event()
- monkeypatch.setattr(slash_worker, "_is_orphaned", lambda ppid: True)
monkeypatch.setattr(slash_worker.os, "_exit", lambda code: exited.set())
- slash_worker._start_parent_death_watchdog(1)
+ # A worker a pre-update gateway launched (--parent-pid) whose parent is gone: the live ppid differs.
+ slash_worker._start_legacy_parent_watchdog(os.getppid() + 1_000_000, poll_s=0.01, grace_s=0)
assert exited.wait(10)
assert "hermes.session.count" in _metric_names(home)
diff --git a/tui_gateway/slash_worker.py b/tui_gateway/slash_worker.py
index 46a9960ec6c..202a7611f92 100644
--- a/tui_gateway/slash_worker.py
+++ b/tui_gateway/slash_worker.py
@@ -56,9 +56,11 @@ def _start_legacy_parent_watchdog(parent_pid: int, *, poll_s: float = 2.0, grace
deadline = time.monotonic() + grace_s
while _in_flight.is_set() and time.monotonic() < deadline:
time.sleep(0.05)
+ from hermes_cli.observability.shared_metrics_process import flush_before_hard_exit
+ flush_before_hard_exit() # os._exit skips the opt-in metrics runtimes' atexit flush (bounded)
os._exit(0)
- # health: allow HX012 -- the loop only reads getppid() and calls os._exit; no profile-scoped state
+ # health: allow HX012 -- the loop reads getppid(), flushes the opt-in metrics and calls os._exit
threading.Thread(target=_loop, daemon=True, name="slash-legacy-parent-watchdog").start()