Files
ai-agent/backend/test_waiting_busy.py
gabrielvidalandClaude Opus 5.5 003edde5f1 perf(inbox): /api/inbox/waiting waits ≤ 1 s for a run that is going, then answers with the last result
Callers parked on the per-person lock behind a slow provider run, each
holding a worker thread, while the ask-form provider's script called back
into the hub for a thread of its own: the pool filled up (2026-11-27).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 16:24:22 +01:00

38 lines
1.3 KiB
Python

"""plugins/waiting.waiting_for: a caller doesn't park a pool thread behind a
slow run when an earlier answer exists (2026-11-27 pool exhaustion)."""
import threading
import time
from types import SimpleNamespace
from plugins import waiting as waiting_mod
def test_a_second_call_answers_with_the_last_result_while_a_run_is_slow(monkeypatch):
caller = SimpleNamespace(is_owner=True, id="owner")
gate, entered = threading.Event(), threading.Event()
def slow_collect(deps, c, uid):
entered.set()
gate.wait(5)
return [{"id": "fresh"}]
monkeypatch.setattr(waiting_mod, "_collect_waiting", slow_collect)
monkeypatch.setattr(waiting_mod, "WAIT_FOR_RUN_SECS", 0.2)
uid = waiting_mod.OWNER_UID
with waiting_mod._lock:
waiting_mod._waiting_last[uid] = (time.monotonic() - 60, [{"id": "previous"}])
first: list = []
t = threading.Thread(target=lambda: first.append(waiting_mod.waiting_for(None, caller)))
t.start()
assert entered.wait(2)
t0 = time.monotonic()
second = waiting_mod.waiting_for(None, caller)
assert time.monotonic() - t0 < 1.0
assert second == [{"id": "previous"}]
gate.set()
t.join(2)
assert first == [[{"id": "fresh"}]]
with waiting_mod._lock:
waiting_mod._waiting_last.pop(uid, None)