Skip to content

The agent that grows with you

fix(gateway): shutdown flush cannot outlive the launchd exit budget

kshitijk4poord27ee657b147
diff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.py
index ef8b1688739..723b3cf7c7d 100644
--- a/gateway/run_shutdown.py
+++ b/gateway/run_shutdown.py
@@ -2034,7 +2034,13 @@ class GatewayShutdownMixin:
self._update_runtime_status("stopped", self._exit_reason)
try:
from gateway.status import flush_runtime_status_async
- if not await flush_runtime_status_async(timeout=2.0):
+ # Never outlive the launchd exit budget (``_launchd_exit_timeout_s`` is set by the
+ # supervised-restart path when it exists; a plain 2 s bound otherwise).
+ flush_timeout = 2.0
+ budget = getattr(self, "_launchd_exit_timeout_s", None)
+ if isinstance(budget, (int, float)) and budget > 0:
+ flush_timeout = max(0.0, min(flush_timeout, budget - ctx.elapsed()))
+ if not await flush_runtime_status_async(timeout=flush_timeout):
logger.warning("Timed out flushing terminal gateway runtime status")
except Exception:
logger.debug("Failed to flush terminal gateway runtime status", exc_info=True)