Skip to content

The agent that grows with you

fix(gateway-stop): settle within the leash left; keep state.db open for live authority work

Tekniuma40307a6cc1a
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index f66c13baef6..b96fda35ea1 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -193,6 +193,9 @@ def _authority_tasks(authority):
# How long a Stopped turn gets to settle durably before shutdown refuses to release ownership: a managed worker
# acknowledges Stop within STOP_ACK_SECONDS (30) or is terminated, then closed (<=10).
TURN_SETTLE_SECONDS = 45.0
+# What a stop keeps of its watchdog leash for the phases after the settle (adapter teardown, executor
+# quiesce, SessionDB close, exit-state writes): the settle never spends it.
+STOP_TAIL_RESERVE_SECONDS = 10.0
def managed_turn_count(runner):
@@ -448,25 +451,28 @@ async def drain_gateway_runtime(runner):
adapter.set_message_handler(None)
-async def settle_gateway_runtime(runner):
+async def settle_gateway_runtime(runner, budget=None):
"""Keep authority tasks alive until their last durable settlement write, within a bound.
A managed turn still running here gets its Stop first (its worker acknowledges or is
terminated, and the admission settles interrupted or ``unknown``); a task that still misses
- TURN_SETTLE_SECONDS is logged and left running for recovery. The stop sequence continues:
+ TURN_SETTLE_SECONDS (clamped to ``budget``, what the stop's watchdog leash has left, minus
+ STOP_TAIL_RESERVE_SECONDS) is logged and left running for recovery. The stop sequence continues:
the executor quiesce skips the DB close while a writer is live, and process exit releases
the ownership lock, so raising here would only skip teardown and disarm the watchdog."""
authorities = _authorities(runner)
stop_managed_turns(runner)
tasks = [task for authority in authorities for task in _authority_tasks(authority)]
+ wait = TURN_SETTLE_SECONDS if budget is None else max(0.0, min(TURN_SETTLE_SECONDS,
+ budget - STOP_TAIL_RESERVE_SECONDS))
if tasks:
- done, pending = await asyncio.wait(tasks, timeout=TURN_SETTLE_SECONDS)
+ done, pending = await asyncio.wait(tasks, timeout=wait) if wait > 0 else (set(), set(tasks))
for task in done:
if not task.cancelled():
task.exception()
if pending:
logger.warning('%d authority task(s) did not settle within %.0fs; left for recovery',
- len(pending), TURN_SETTLE_SECONDS)
+ len(pending), wait)
from gateway.session_runtime_workers import join_authority_work
for authority in authorities:
try:
diff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.py
index 2dbc5fe2cbf..c74ecc258a4 100644
--- a/gateway/run_shutdown.py
+++ b/gateway/run_shutdown.py
@@ -2085,11 +2085,11 @@ class GatewayShutdownMixin(GatewaySessionEndMixin):
_cron_live = self._active_cron_job_count()
_api_live = ctx.api_live
_api_worker_live = self._active_api_worker_count()
- _deferred_live = ctx.deferred_count()
+ _deferred_live = ctx.deferred_count() + managed_turn_count(self) # + authority turns/mutations
if _cron_live or _api_live or _api_worker_live or _deferred_live:
logger.warning(
"Shutdown phase: %d cron job(s) / %d API-server run(s) / %d API-server worker(s) / "
- "%d deferred worker(s) still running after the executor quiesce — skipping the SessionDB "
+ "%d deferred/authority worker(s) still running after the executor quiesce — skipping the SessionDB "
"close/checkpoint, leaving state.db open for the live writer (#102198, #116535)",
_cron_live, _api_live, _api_worker_live, _deferred_live,
)
@@ -2231,8 +2231,8 @@ class GatewayShutdownMixin(GatewaySessionEndMixin):
await GatewayRunner._stop_drain_active_work(self, timeout, ctx)
if ctx.timed_out:
await GatewayRunner._stop_interrupt_remaining_work(self, ctx)
- from gateway.run_runtime import settle_gateway_runtime
- await settle_gateway_runtime(self)
+ from gateway.run_runtime import settle_gateway_runtime # within what the leash has left
+ await settle_gateway_runtime(self, budget=_effective_watchdog_leash(self) - ctx.elapsed())
await GatewayRunner._stop_finalize_agents_and_adapters(self, ctx)
await GatewayRunner._stop_release_runtime_state(self, ctx)
GatewayRunner._stop_quiesce_and_close_session_dbs(self, timeout, ctx)
diff --git a/tests/gateway/test_shutdown_settle_leash.py b/tests/gateway/test_shutdown_settle_leash.py
new file mode 100644
index 00000000000..599a024596d
--- /dev/null
+++ b/tests/gateway/test_shutdown_settle_leash.py
@@ -0,0 +1,39 @@
+"""A gateway stop settles authority work within the watchdog leash it has left, and never closes
+state.db under an authority turn that outlived the settle (N59).
+
+The settle used to wait a flat TURN_SETTLE_SECONDS (45 s) whatever the drain and interrupt phases had
+already spent, so the stop ran past its watchdog leash; and the SessionDB close gate counted executor,
+cron, API and deferred workers but not the authority's still-running turn, so it closed and
+checkpointed state.db under a live writer.
+"""
+import asyncio
+import time
+from types import SimpleNamespace
+
+import pytest
+
+import gateway.run as gw_mod
+from gateway import run_shutdown
+from tests.gateway.test_shutdown_executor_quiesce import _FakeGateway
+
+
+async def test_stop_settles_within_the_leash_and_keeps_state_db_open_for_a_live_turn(monkeypatch):
+ events = []
+ gw = _FakeGateway(events)
+ # A 12 s watchdog leash: the settle may spend at most what is left of it minus the tail reserve.
+ monkeypatch.setattr(run_shutdown, "resolve_shutdown_watchdog_delay", lambda drain, **_k: 12.0)
+ never = asyncio.Event()
+ turn = asyncio.create_task(never.wait()) # an executing turn that ignores its Stop
+ live = SimpleNamespace(task=turn, route="r", event_stream=SimpleNamespace(execution={"execution_generation": 1}))
+ gw.session_authority = SimpleNamespace(
+ profile_id="default", sessions={"s": live}, _managed_workers={}, adopted={}, pending_stops={},
+ runner=gw)
+ started = time.monotonic()
+ try:
+ async with asyncio.timeout(10):
+ await gw_mod.GatewayRunner.stop(gw)
+ finally:
+ turn.cancel()
+ assert time.monotonic() - started < 6, "the settle overran the stop's watchdog leash"
+ assert "close:session_db" not in events, f"state.db closed under a live authority turn: {events}"