The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a client mid-attach still holds a gateway retiring for a restart
Tekniume07a672e8275
diff --git a/gateway/run_idle_exit.py b/gateway/run_idle_exit.pyindex 7f0e2615c5e..2bf9c70008a 100644--- a/gateway/run_idle_exit.py+++ b/gateway/run_idle_exit.py@@ -149,12 +149,16 @@ def _store_gates_busy(homes) -> str | None:return None+def _attaching(runner) -> bool:+ tickets = getattr(runner, "session_ticket_store", None)+ return tickets is not None and tickets.outstanding()++def _client_busy(runner, window: float) -> str | None:clients = getattr(runner, "_idle_exit_clients", 0)if clients:return f"{clients} client(s) attached"- tickets = getattr(runner, "session_ticket_store", None)- if tickets is not None and tickets.outstanding():+ if _attaching(runner):return "client attaching"quiet = time.monotonic() - getattr(runner, "_idle_exit_last_activity", 0.0)if quiet < window:@@ -167,10 +171,14 @@ def busy_reason(runner, *, window: float, count_clients: bool = True) -> str | N``count_clients=False``: a restart is pending, and an attached or returning client is exactly whoneeds the fresh gateway; every duty nothing would restart (adapters, cron, admissions) still counts.+ A client mid-attach (unredeemed ticket) still holds it: its first connect would be refused with+ no rediscovery, while an attached client waits out the drain and starts the fresh gateway."""try:if count_clients and (reason := _client_busy(runner, window)):return reason+ if not count_clients and _attaching(runner):+ return "client attaching"if runner._draining or not runner._running:return "already stopping"if runner._active_work_count():diff --git a/tests/gateway/test_unmanaged_idle_exit_restart_pending.py b/tests/gateway/test_unmanaged_idle_exit_restart_pending.pyindex 24022506982..8cf89b53f45 100644--- a/tests/gateway/test_unmanaged_idle_exit_restart_pending.py+++ b/tests/gateway/test_unmanaged_idle_exit_restart_pending.py@@ -57,7 +57,21 @@ async def test_pending_restart_retires_once_the_turn_ends_even_with_a_client_attawait async_wait_until(lambda: work[1] >= 4, timeout=10, what="two restart probes")assert not watcher.done() and runner.session_runtime_descriptor["state"] == "ready" # turn in flight+ # A client mid-attach (minted, not redeemed) holds it: its refused first connect never rediscovers.+ store = runner.session_ticket_store+ store.mint(profile_id=next(iter(store.profile_ids)), subject="cli", purpose="interactive")+ checks = [0]+ real_outstanding = store.outstanding++ def outstanding():+ checks[0] += 1+ return real_outstanding()+ monkeypatch.setattr(store, "outstanding", outstanding)work[0] = 0+ await async_wait_until(lambda: checks[0] >= 4, timeout=10, what="probes while attaching")+ assert not watcher.done() and stops == []++ store.revoke()await asyncio.wait_for(watcher, 5)await runner._idle_exit_stop_taskassert stops == [True]