The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): launchd exit-timeout loader lives with the other timeout loaders
kshitijk4poor33e9503386ca
diff --git a/gateway/restart.py b/gateway/restart.pyindex 761c3a0e6d5..647db0021b3 100644--- a/gateway/restart.py+++ b/gateway/restart.py@@ -123,11 +123,7 @@ def read_launchd_exit_timeout_s(if _getuid is None:return Noneuid = _getuid()- try:- resolved_uid = int(uid)- except (TypeError, ValueError):- return None- domain = "system" if resolved_uid == 0 else f"gui/{resolved_uid}"+ domain = "system" if uid == 0 else f"gui/{uid}"try:proc = run(["launchctl", "print", f"{domain}/{label}"],@@ -139,9 +135,9 @@ def read_launchd_exit_timeout_s()except (OSError, subprocess.SubprocessError, ValueError):return None- if getattr(proc, "returncode", 1) != 0:+ if proc.returncode != 0:return None- return parse_launchd_exit_timeout(getattr(proc, "stdout", ""))+ return parse_launchd_exit_timeout(proc.stdout)def resolve_launchd_capped_drain(diff --git a/gateway/run.py b/gateway/run.pyindex 77e370a075b..8f8007d0f9a 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -2173,9 +2173,7 @@ from gateway.restart import (DEFAULT_GATEWAY_CRON_DRAIN_TIMEOUT,DEFAULT_GATEWAY_RESTART_AFTER_TURN_TIMEOUT,DEFAULT_GATEWAY_RESTART_DRAIN_TIMEOUT,- DEFAULT_GATEWAY_SIGNAL_INTERRUPT_GRACE_TIMEOUT,- read_launchd_exit_timeout_s,- resolve_launchd_capped_drain)+ DEFAULT_GATEWAY_SIGNAL_INTERRUPT_GRACE_TIMEOUT)logger = logging.getLogger(__name__)@@ -3500,36 +3498,6 @@ class GatewayRunner(self._provider_routing = self._load_provider_routing()self._fallback_model = self._load_fallback_model()- @staticmethod- def _load_launchd_exit_timeout(drain_timeout: float) -> Optional[float]:- """Read the live launchd ``ExitTimeOut`` this job runs under, if any.-- launchd is the one supervisor the gateway cannot size from config: the per-user (gui)- domain clamps ``ExitTimeOut`` (measured 60s on macOS 26), and any signal-driven stop that- drains past it is SIGKILLed mid-teardown — the unclean-exit half of the state.db- corruption class. Returns ``None`` (fail-open, drain unchanged) when not launchd-owned or- when ``launchctl print`` is unavailable. Logs a WARNING when the configured drain exceeds- the live budget so the misconfiguration is visible at boot, not at the next SIGKILL.- """- # read_launchd_exit_timeout_s is already fail-open (returns None on any probe failure).- exit_timeout = read_launchd_exit_timeout_s()- if exit_timeout is None:- return None- effective = resolve_launchd_capped_drain(drain_timeout, exit_timeout)- if effective < drain_timeout:- logger.warning(- "restart_drain_timeout=%.0fs exceeds the live launchd exit timeout (%.0fs) for %s; "- "signal-driven stops will drain at most %.0fs so teardown finishes before launchd "- "SIGKILLs (launchd clamps ExitTimeOut in the per-user domain).",- drain_timeout, exit_timeout, os.environ.get("XPC_SERVICE_NAME", "this job"), effective,- )- else:- logger.info(- "launchd exit timeout for %s is %.0fs (drain %.0fs fits)",- os.environ.get("XPC_SERVICE_NAME", "this job"), exit_timeout, drain_timeout,- )- return exit_timeout-def _init_session_store(self) -> None:"""Build the SessionStore (with process-registry reset guard), its async facade and the router."""from tools.process_registry import process_registrydiff --git a/gateway/run_config_loaders.py b/gateway/run_config_loaders.pyindex 2c9eb06ed00..9c7a4e74b71 100644--- a/gateway/run_config_loaders.py+++ b/gateway/run_config_loaders.py@@ -21,7 +21,8 @@ from gateway.restart import (DEFAULT_GATEWAY_RESTART_AFTER_TURN_TIMEOUT, DEFAULT_GATEWAY_RESTART_DRAIN_TIMEOUT,DEFAULT_GATEWAY_SIGNAL_INTERRUPT_GRACE_TIMEOUT, parse_cron_drain_timeout,parse_restart_after_turn_timeout, parse_restart_drain_timeout,- parse_signal_interrupt_grace_timeout,+ parse_signal_interrupt_grace_timeout, read_launchd_exit_timeout_s,+ resolve_launchd_capped_drain,)from gateway.session import SessionSourcefrom gateway.session_state import SERVICE_TIER_UNSET as _SERVICE_TIER_UNSET@@ -325,6 +326,36 @@ class GatewayConfigLoadersMixin:cls._warn_unparsable_timeout("restart_drain_timeout", raw, DEFAULT_GATEWAY_RESTART_DRAIN_TIMEOUT)return value+ @staticmethod+ def _load_launchd_exit_timeout(drain_timeout: float) -> Optional[float]:+ """Read the live launchd ``ExitTimeOut`` this job runs under, if any.++ launchd is the one supervisor the gateway cannot size from config: the per-user (gui)+ domain clamps ``ExitTimeOut`` (measured 60s on macOS 26), and any signal-driven stop that+ drains past it is SIGKILLed mid-teardown — the unclean-exit half of the state.db+ corruption class. Returns ``None`` (fail-open, drain unchanged) when not launchd-owned or+ when ``launchctl print`` is unavailable. Logs a WARNING when the configured drain exceeds+ the live budget so the misconfiguration is visible at boot, not at the next SIGKILL.+ """+ # read_launchd_exit_timeout_s is already fail-open (returns None on any probe failure).+ exit_timeout = read_launchd_exit_timeout_s()+ if exit_timeout is None:+ return None+ effective = resolve_launchd_capped_drain(drain_timeout, exit_timeout)+ if effective < drain_timeout:+ logger.warning(+ "restart_drain_timeout=%.0fs exceeds the live launchd exit timeout (%.0fs) for %s; "+ "signal-driven stops will drain at most %.0fs so teardown finishes before launchd "+ "SIGKILLs (launchd clamps ExitTimeOut in the per-user domain).",+ drain_timeout, exit_timeout, os.environ.get("XPC_SERVICE_NAME", "this job"), effective,+ )+ else:+ logger.info(+ "launchd exit timeout for %s is %.0fs (drain %.0fs fits)",+ os.environ.get("XPC_SERVICE_NAME", "this job"), exit_timeout, drain_timeout,+ )+ return exit_timeout+@classmethoddef _load_env_or_agent_cfg_timeout(cls, env_var: str, cfg_key: str, parse, default: float) -> float:"""Env var (non-empty) else ``agent.<cfg_key>``; warn once when a supplied value fails to parse.