Skip to content

The agent that grows with you

fix(gateway): a one-shot worker that cannot start gives its admission back

unsupportedpastels69a63f80b2ef
diff --git a/gateway/session_llm_oneshot.py b/gateway/session_llm_oneshot.py
index c640bbf09d3..efafc45e720 100644
--- a/gateway/session_llm_oneshot.py
+++ b/gateway/session_llm_oneshot.py
@@ -68,7 +68,16 @@ class _Pool:
if spawn:
# Daemon: a call stuck on its model must not hold up interpreter exit (it writes no state).
from agent.memory_provider import spawn_context_thread
- spawn_context_thread(self.drain, name='hermes-oneshot').start()
+ try:
+ spawn_context_thread(self.drain, name='hermes-oneshot').start()
+ except RuntimeError:
+ # No new worker (thread exhaustion). A live worker drains this call; with none it
+ # would never run, so it leaves the queue with its slot and the error goes out.
+ with self.lock:
+ self.threads -= 1
+ orphaned = not self.threads
+ if orphaned and call.cancel():
+ raise
return call
def release(self, call):
diff --git a/tests/gateway/test_session_llm_oneshot_pool.py b/tests/gateway/test_session_llm_oneshot_pool.py
index d6ff068b33e..43f153ca2e3 100644
--- a/tests/gateway/test_session_llm_oneshot_pool.py
+++ b/tests/gateway/test_session_llm_oneshot_pool.py
@@ -183,3 +183,25 @@ def test_the_call_runs_in_the_handlers_context_and_its_result_is_checked_before_
assert frames['scoped']['result'] == {'text': 'secondary-profile'}
assert frames['checked']['error']['code'] == 5030
assert 'violates its contract' in frames['checked']['error']['message']
+
+
+def test_a_worker_that_cannot_start_gives_its_slot_back(monkeypatch):
+ """Thread exhaustion at spawn: the call fails with the start error, and its admission and the
+ worker count go back, so the owner does not answer ``oneshot_busy`` for good after 16 of them."""
+ from agent import memory_provider
+ real = memory_provider.spawn_context_thread
+
+ class _Unstartable:
+ def start(self):
+ raise RuntimeError("can't start new thread")
+ monkeypatch.setattr(memory_provider, 'spawn_context_thread', lambda *args, **kwargs: _Unstartable())
+ pool, refusals = oneshot._Pool(), []
+ for _ in range(oneshot._ADMITTED + 1):
+ try:
+ pool.submit(lambda: {'text': 'never'})
+ except Exception as exc: # the start error, or a busy refusal once slots leak
+ refusals.append(type(exc).__name__)
+ assert refusals == ['RuntimeError'] * (oneshot._ADMITTED + 1)
+ assert (pool.calls, pool.queue, pool.threads) == (set(), {}, 0)
+ monkeypatch.setattr(memory_provider, 'spawn_context_thread', real)
+ assert pool.submit(lambda: {'text': 'ok'}).result(timeout=10) == {'text': 'ok'}