Skip to content

The agent that grows with you

fix(gateway): count wedged housekeeping workers in the shutdown live-worker total

kshitijk4poor9436dfe88b33
diff --git a/gateway/run.py b/gateway/run.py
index 98a552c75f2..2e6e8fb738c 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -4337,10 +4337,11 @@ class GatewayRunner(
return executor
def _shutdown_executor(self, drain_timeout: float = 0.0) -> int:
- """Stop the gateway-owned executor; returns the number of worker threads still running.
+ """Stop the gateway-owned pools; returns the number of worker threads still running.
``drain_timeout=0`` is fire-and-forget; shutdown passes a bounded budget so blocking DB work
cannot outlive ``SessionDB.close()``. ``cancel_futures`` only drops unstarted work and cancelling
- a ``run_in_executor`` awaitable does not stop its thread, so running workers are joined."""
+ a ``run_in_executor`` awaitable does not stop its thread, so running workers are joined — on
+ the turn pool AND the housekeeping pool, both of which write to SessionDB."""
lock = getattr(self, "_executor_lock", None)
if lock is None:
return 0
@@ -4350,22 +4351,19 @@ class GatewayRunner(
self._executor = None
housekeeping = getattr(self, "_housekeeping_executor", None)
self._housekeeping_executor = None
- # Housekeeping is best-effort and nothing drains it, so it gets no drain budget — but a
- # pool nobody drains must still be shut down, or its threads outlive the runner.
- if housekeeping is not None:
+ # Housekeeping workers run SessionDB writes too (session finalize, agent cleanup), so a wedged
+ # one is exactly the mid-write worker the #101093 skip-close heuristic exists for. Both pools
+ # are therefore joined under the SAME drain deadline and both contribute to the live count.
+ workers: list = []
+ for pool in (executor, housekeeping):
+ if pool is None:
+ continue
try:
- housekeeping.shutdown(wait=False, cancel_futures=True)
+ pool.shutdown(wait=False, cancel_futures=True)
except TypeError:
- housekeeping.shutdown(wait=False)
- if executor is None:
- return 0
- try:
- executor.shutdown(wait=False, cancel_futures=True)
- except TypeError:
- executor.shutdown(wait=False)
-
- # shutdown() has no timeout, so join workers directly; `_threads` is absent on test doubles (no wait).
- workers = list(getattr(executor, "_threads", None) or ())
+ pool.shutdown(wait=False)
+ # shutdown() has no timeout, so join workers directly; `_threads` is absent on test doubles (no wait).
+ workers.extend(getattr(pool, "_threads", None) or ())
deadline = time.monotonic() + max(float(drain_timeout or 0.0), 0.0)
for worker in workers:
remaining = deadline - time.monotonic()
diff --git a/tests/gateway/test_executor_pool_isolation.py b/tests/gateway/test_executor_pool_isolation.py
index d06d7d00c05..1c91ca04ba3 100644
--- a/tests/gateway/test_executor_pool_isolation.py
+++ b/tests/gateway/test_executor_pool_isolation.py
@@ -108,13 +108,31 @@ def test_abandoned_housekeeping_cannot_delay_a_turn_body():
def test_shutdown_stops_the_housekeeping_pool():
- """A pool nobody drains must still be shut down, or its threads outlive the runner."""
+ """A pool nobody drains must still be shut down, or its threads outlive the runner — and a
+ wedged housekeeping worker must be counted as live, or the #101093 skip-close heuristic in
+ ``_stop_quiesce_and_close_session_dbs`` closes SessionDB under a mid-write worker."""
+ wedge = threading.Event()
+ entered = threading.Event()
+
+ def wedged(_agent):
+ entered.set()
+ assert wedge.wait(60), "wedge never released"
+
runner = _runner()
hk = runner._get_housekeeping_executor()
assert not hk._shutdown
+ hk.submit(wedged, object())
+ assert entered.wait(5), "wedged housekeeping item never started"
- runner._shutdown_executor()
+ try:
+ started = time.monotonic()
+ live = runner._shutdown_executor(drain_timeout=0.2)
+ elapsed = time.monotonic() - started
+ finally:
+ wedge.set()
+ assert live == 1, f"wedged housekeeping worker not reported as live (got {live})"
+ assert elapsed < 5, f"drain deadline not honoured for the housekeeping pool ({elapsed:.2f}s)"
assert hk._shutdown, "housekeeping pool was left running at shutdown"
assert runner._housekeeping_executor is None
with pytest.raises(RuntimeError):