Files
ai-agent/backend/test_runner_slots.py
gabrielvidalandClaude Opus 5.5 bf8d306d34 fix(plugins): a request's top-level plugin scripts share 12 slots (503 past 5 s)
So scripts can never take all 40 worker threads. Background threads (cron,
schedules) and nested calls (call_depth > 0) are not counted.

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

35 lines
1.3 KiB
Python

"""plugins/runner.run_script: a request's top-level scripts share a few slots
(2026-11-27 pool exhaustion); background threads and nested calls don't."""
import threading
import pytest
from plugins import runner
def test_request_threads_share_the_slots_background_ones_do_not(monkeypatch):
gate = threading.Event()
monkeypatch.setattr(runner, "_run_script", lambda *a, **k: gate.wait(5) and {"ok": True})
monkeypatch.setattr(runner, "_request_slots", threading.BoundedSemaphore(1))
monkeypatch.setattr(runner, "REQUEST_SCRIPT_WAIT_SECS", 0.1)
out: list = []
first = threading.Thread(target=lambda: out.append(runner.run_script()),
name="AnyIO worker thread")
first.start()
errors: list = []
def second():
try:
runner.run_script()
except runner.ActionError as e:
errors.append(e)
t2 = threading.Thread(target=second, name="AnyIO worker thread")
t2.start(); t2.join(2)
assert len(errors) == 1 and "busy" in str(errors[0].args or errors[0])
# A nested call and a background thread are never refused.
gate.set()
assert runner.run_script(call_depth=1) == {"ok": True}
assert runner.run_script() == {"ok": True}
first.join(2)
assert out == [{"ok": True}]