The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): drop the PHASE=executor_wait queue-latency diagnostic
kshitijk4poord503f4f6fc0c
diff --git a/gateway/run.py b/gateway/run.pyindex 207c97b8a34..9a17aab1e26 100644--- a/gateway/run.py+++ b/gateway/run.py@@ -85,8 +85,6 @@ def _get_or_create_pool(setattr(owner, attr, executor)return executor--_EXECUTOR_WAIT_WARN_SECS = 5.0# End reasons meaning the USER deliberately closed this thread. Shared by _classify_completion_target and# _resolve_async_delegation_session so they never disagree (else a "delivered" reason is acked, then lost)._USER_BOUNDARY_END_REASONS = ("session_reset", "user_exit", "session_switch", "new_session")@@ -4310,9 +4308,11 @@ class GatewayRunner(async def _run_in_executor_with_context(self, func, *args):"""Run blocking work in the thread pool while preserving session contextvars."""- return await self._submit_with_context(self._get_executor(), "turn", func, *args)+ loop = asyncio.get_running_loop()+ ctx = copy_context()+ return await loop.run_in_executor(self._get_executor(), ctx.run, func, *args)- async def _run_housekeeping_in_executor(self, label, func, *args):+ async def _run_housekeeping_in_executor(self, func, *args):"""Run best-effort session housekeeping off the TURN pool.Callers of this helper bound their await with ``asyncio.wait_for`` and, on timeout, log@@ -4324,39 +4324,9 @@ class GatewayRunner(its own bounded pool; exhausting that one delays only more housekeeping, which isbest-effort by construction."""- return await self._submit_with_context(- self._get_housekeeping_executor(), label, func, *args- )-- async def _submit_with_context(self, executor, pool, func, *args):- """Submit to ``executor`` preserving contextvars, logging excessive queue latency.-- Queue latency on these pools is otherwise invisible: nothing between "inbound message"- and the turn body names the wait, so a saturated pool looks like a hung agent.- """loop = asyncio.get_running_loop()- ctx = copy_context()- submitted_at = time.monotonic()- warn_after = _EXECUTOR_WAIT_WARN_SECS-- def _timed(*call_args):- waited = time.monotonic() - submitted_at- if waited >= warn_after:- try:- max_workers = getattr(executor, "_max_workers", -1)- inflight = len(getattr(executor, "_threads", ()) or ())- queued = executor._work_queue.qsize()- except Exception:- max_workers = inflight = queued = -1- logger.warning(- "PHASE=executor_wait pool=%s key=%s waited=%.1f inflight=%s queued=%s "- "max_workers=%s",- pool, getattr(func, "__name__", type(func).__name__), waited,- inflight, queued, max_workers,- )- return ctx.run(func, *call_args)-- return await loop.run_in_executor(executor, _timed, *args)+ return await loop.run_in_executor(+ self._get_housekeeping_executor(), copy_context().run, func, *args)def _get_executor(self) -> concurrent.futures.ThreadPoolExecutor:"""Return the gateway-owned executor for blocking agent work."""diff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.pyindex 5731b1e71ea..e5daab7d0f4 100644--- a/gateway/run_shutdown.py+++ b/gateway/run_shutdown.py@@ -1221,8 +1221,7 @@ class GatewayShutdownMixin:try:await asyncio.wait_for(- self._run_housekeeping_in_executor(- "finalize", self._run_release_in_profile_scope, _call, (), session_key),+ self._run_housekeeping_in_executor(self._run_release_in_profile_scope, _call, (), session_key),timeout=self._FINALIZE_TIMEOUT_S,)except asyncio.TimeoutError:@@ -1253,8 +1252,7 @@ class GatewayShutdownMixin:try:await asyncio.wait_for(self._run_housekeeping_in_executor(- "cleanup", self._run_release_in_profile_scope,- self._cleanup_agent_resources, (agent,), session_key,+ self._run_release_in_profile_scope, self._cleanup_agent_resources, (agent,), session_key,),timeout=self._CLEANUP_TIMEOUT_S,)diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.pyindex dd872896d94..c35965b1483 100644--- a/gateway/slash_commands_session.py+++ b/gateway/slash_commands_session.py@@ -127,8 +127,7 @@ class GatewaySessionCommandsMixin:returntry:await asyncio.wait_for(- self._run_housekeeping_in_executor(- "cleanup", self._cleanup_agent_resources, _old_agent),+ self._run_housekeeping_in_executor(self._cleanup_agent_resources, _old_agent),timeout=_RESET_CLEANUP_TIMEOUT_S)except asyncio.TimeoutError:logger.warning(diff --git a/tests/gateway/test_executor_pool_isolation.py b/tests/gateway/test_executor_pool_isolation.pyindex 051f68c96f4..2372a55c0e4 100644--- a/tests/gateway/test_executor_pool_isolation.py+++ b/tests/gateway/test_executor_pool_isolation.py@@ -44,7 +44,6 @@ def _runner(cleanup=None, *, cleanup_timeout=1.0):for name in ("_get_executor","_get_housekeeping_executor",- "_submit_with_context","_run_in_executor_with_context","_run_housekeeping_in_executor","_shutdown_executor",@@ -66,9 +65,7 @@ async def _abandon_housekeeping(runner, count, timeout):async def one():try:await asyncio.wait_for(- runner._run_housekeeping_in_executor(- "cleanup", runner._cleanup_agent_resources, object()- ),+ runner._run_housekeeping_in_executor(runner._cleanup_agent_resources, object()),timeout=timeout,)except asyncio.TimeoutError:@@ -217,7 +214,7 @@ def test_contextvars_survive_both_pools():async def exercise():probe.set("scoped")turn = await runner._run_in_executor_with_context(probe.get)- hk = await runner._run_housekeeping_in_executor("cleanup", probe.get)+ hk = await runner._run_housekeeping_in_executor(probe.get)return turn, hktry:@@ -262,7 +259,7 @@ def test_args_results_and_errors_round_trip():async def exercise():assert await runner._run_in_executor_with_context(lambda a, b: a + b, 2, 3) == 5with pytest.raises(ValueError, match="boom"):- await runner._run_housekeeping_in_executor("cleanup", _raise_boom)+ await runner._run_housekeeping_in_executor(_raise_boom)try:asyncio.run(exercise())