Skip to content

The agent that grows with you

fix(gateway): publish watchdog degradation before exit

joaomarcosb013f461f953
diff --git a/gateway/shutdown_watchdog.py b/gateway/shutdown_watchdog.py
index 528c8e8a01e..b892bcfe733 100644
--- a/gateway/shutdown_watchdog.py
+++ b/gateway/shutdown_watchdog.py
@@ -146,7 +146,22 @@ def start_loop_liveness_watchdog(
def _mark_exited_quietly(exit_code: int, reason: str) -> None:
- """Best-effort lifecycle-ledger stamp so the next boot names the watchdog, not SIGKILL/OOM."""
+ """Publish terminal watchdog evidence to both lifecycle stores, best effort.
+
+ ``gateway_state.json`` and the lifecycle sentinel are independent records. The
+ liveness watchdog already had authority to terminate a wedged process, but only
+ updated the sentinel; status readers therefore observed ``running`` until a
+ replacement process booted. Keep these terminal writes together so every
+ watchdog exit exposes the same degraded reason before ``os._exit`` bypasses normal
+ teardown.
+ """
+ with contextlib.suppress(Exception):
+ from gateway.status import write_runtime_status
+ write_runtime_status(
+ gateway_state="degraded",
+ exit_reason=reason,
+ restart_requested=exit_code == GATEWAY_SERVICE_RESTART_EXIT_CODE,
+ )
with contextlib.suppress(Exception):
from gateway.lifecycle_ledger import mark_exited
mark_exited(exit_code, reason=reason)
diff --git a/tests/gateway/test_loop_liveness_watchdog.py b/tests/gateway/test_loop_liveness_watchdog.py
index 4d6ecc93856..e1d7ee94187 100644
--- a/tests/gateway/test_loop_liveness_watchdog.py
+++ b/tests/gateway/test_loop_liveness_watchdog.py
@@ -278,6 +278,38 @@ def test_load_gateway_config_bridges_loop_watchdog_keys(tmp_path, monkeypatch):
assert cfg.loop_watchdog_max_strikes == 12
+def test_loop_liveness_watchdog_marks_runtime_degraded_before_restart():
+ """The terminal watchdog observation must be visible before ``os._exit``."""
+ from gateway.status import read_runtime_status, write_runtime_status
+
+ write_runtime_status(gateway_state="running", exit_reason=None)
+ loop = MagicMock(spec=asyncio.AbstractEventLoop)
+ fired = threading.Event()
+ exit_codes = []
+
+ def fake_exit(code: int) -> None:
+ exit_codes.append(code)
+ fired.set()
+
+ with (
+ patch("gateway.shutdown_watchdog.faulthandler.dump_traceback"),
+ patch("gateway.shutdown_watchdog.os._exit", side_effect=fake_exit),
+ ):
+ handle = start_loop_liveness_watchdog(
+ loop, probe_interval=0.01, probe_timeout=0.01, max_strikes=2
+ )
+ assert handle is not None
+ assert fired.wait(timeout=2.0), "watchdog did not reach its restart exit"
+ handle.stop()
+ handle.join(timeout=1.0)
+
+ record = read_runtime_status()
+ assert exit_codes == [75]
+ assert record["gateway_state"] == "degraded"
+ assert record["exit_reason"] == "loop_liveness_watchdog"
+ assert record["restart_requested"] is True
+
+
def test_gateway_runner_liveness_guards_start_and_stop():
from gateway.run import GatewayRunner
@@ -316,6 +348,8 @@ def test_gateway_runner_liveness_guards_start_and_stop():
floor_timer.cancel.assert_called_once_with()
assert runner._loop_liveness_watchdog is None
assert runner._loop_floor_timer_handle is None
+
+
def test_heartbeat_write_does_not_block_the_loop_it_monitors():
"""The heartbeat write must not freeze the loop the watchdog is watching.