The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): thread watchdog leash also fits launchd's exit budget
kshitijk4poordc0f03ace56a
diff --git a/gateway/restart.py b/gateway/restart.pyindex eecf22e5b6e..a0e0f55a7cf 100644--- a/gateway/restart.py+++ b/gateway/restart.py@@ -67,6 +67,10 @@ SYSTEMD_TIMEOUT_STOP_SEC_FLOOR = 60.0# unclean-exit half of the state.db corruption class.LAUNCHD_GUI_EXIT_TIMEOUT_CLAMP_S = 60LAUNCHD_STOP_CLEANUP_RESERVE_S = 10.0+# How far before launchd's SIGKILL the thread watchdog must fire so its+# faulthandler dump + os._exit actually land (the dump is sub-second; the+# margin covers a slow disk).+LAUNCHD_WATCHDOG_DUMP_MARGIN_S = 2.0_LAUNCHD_EXIT_TIMEOUT_RE = re.compile(r"^\s*exit timeout\s*=\s*(\d+)\s*$", re.MULTILINE)@@ -185,6 +189,35 @@ def effective_stop_drain_timeout(runner: object) -> float:return resolve_launchd_capped_drain(drain, getattr(runner, "_launchd_exit_timeout_s", None))+def effective_stop_watchdog_delay(runner: object, watchdog_delay: float) -> float:+ """Thread-watchdog leash for the stop in progress on ``runner``.++ ``watchdog_delay`` is the supervisor-agnostic leash (effective drain ++ grace). Under launchd a signal-driven stop is SIGKILLed at the live+ ``ExitTimeOut`` — with a 60s budget the default leash (50 + 60 = 110s)+ can never fire, so the forensic stack dump and ``os._exit`` the watchdog+ exists for are lost. Clamp the leash to ``ExitTimeOut -+ LAUNCHD_WATCHDOG_DUMP_MARGIN_S`` so the dump lands before SIGKILL. Same+ duck-typing / fail-open rules as :func:`effective_stop_drain_timeout`.+ """+ try:+ leash = max(float(watchdog_delay), 0.0)+ except (TypeError, ValueError):+ leash = 0.0+ if not getattr(runner, "_stop_requested_by_signal", False):+ return leash+ budget = getattr(runner, "_launchd_exit_timeout_s", None)+ if budget is None:+ return leash+ try:+ budget = float(budget)+ except (TypeError, ValueError):+ return leash+ if budget <= 0.0:+ return leash+ return min(leash, max(budget - LAUNCHD_WATCHDOG_DUMP_MARGIN_S, 0.0))++_TRUTHY = {"1", "true", "yes", "on"}diff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.pyindex f27429f90e9..0668949a3ef 100644--- a/gateway/run_shutdown.py+++ b/gateway/run_shutdown.py@@ -23,7 +23,7 @@ from typing import Any, Callable, Dict, Optionalfrom gateway.config import Platformfrom gateway.restart import (DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT, GATEWAY_SERVICE_RESTART_EXIT_CODE,- effective_stop_drain_timeout, resolve_cron_drain_budget+ effective_stop_drain_timeout, effective_stop_watchdog_delay, resolve_cron_drain_budget)from gateway.run_common import _UNSETfrom gateway.shutdown_watchdog import arm_shutdown_watchdog, resolve_shutdown_watchdog_delay@@ -2042,7 +2042,7 @@ class GatewayShutdownMixin:"restart_drain_timeout": self._restart_drain_timeout,"effective_drain_timeout": effective_stop_drain_timeout(self),"launchd_exit_timeout_s": getattr(self, "_launchd_exit_timeout_s", None),- "watchdog_delay_s": resolve_shutdown_watchdog_delay(effective_stop_drain_timeout(self)),+ "watchdog_delay_s": effective_stop_watchdog_delay(self, resolve_shutdown_watchdog_delay(effective_stop_drain_timeout(self))),"phase_elapsed_s": ctx.elapsed() if ctx.started_at is not None else None,}@@ -2062,7 +2062,7 @@ class GatewayShutdownMixin:)if not os.environ.get("PYTEST_CURRENT_TEST"):arm_shutdown_watchdog(- resolve_shutdown_watchdog_delay(effective_stop_drain_timeout(self)), done_event=_watchdog_done,+ effective_stop_watchdog_delay(self, resolve_shutdown_watchdog_delay(effective_stop_drain_timeout(self))), done_event=_watchdog_done,snapshot_fn=lambda: GatewayRunner._shutdown_watchdog_snapshot(self, ctx), exit_code=1,)try: