~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""The AnyIO worker pool that serves sync routes must be tunable.
Starlette runs every plain ``def`` handler via ``anyio.to_thread.run_sync``,
whose default ``CapacityLimiter`` allows 40 threads. That pool is SHARED with
async dependency solving and response validation, so exhausting it degrades
async routes too.
This app is unusually exposed to that default:
* ~248 sync routes against ~65 async ones, so most traffic takes a worker;
* each sync request holds its worker for the full handler duration, including
a first-call SQLCipher open (PBKDF2 key derivation);
* it runs with ``workers=1`` (required for Socket.IO without Redis), so there
is no second process to absorb overflow.
Measured symptom: ``/api/v1/health`` went from 0.8ms to 9.8s at 80 concurrent
requests — past the 8s Docker healthcheck timeout, so the container reports
*unhealthy* under load rather than merely slow.
The knob defaults to AnyIO's value, i.e. no behaviour change. What these tests
pin is that the lever exists and is wired, because the real migration hazard
is not the number — it is that a framework default nobody knows about governs
the app's concurrency ceiling.
"""
import asyncio
import anyio.to_thread
import pytest
from local_deep_research.settings.env_registry import registry
class TestThreadpoolSettingIsRegistered:
def test_setting_exists_with_sane_bounds(self):
setting = registry.get_setting_object("web.threadpool_max_threads")
assert setting is not None, (
"web.threadpool_max_threads is not registered; the operator has "
"no supported way to raise the sync-route concurrency ceiling"
)
assert setting.default is None, (
"default must stay None so AnyIO's own default applies and this "
"knob changes nothing unless an operator opts in"
)
assert setting.min_value == 1
assert setting.max_value is not None and setting.max_value <= 1000, (
"an unbounded max invites a thread count that costs more in "
"context switching than it recovers"
)
def test_description_explains_the_tradeoff(self):
setting = registry.get_setting_object("web.threadpool_max_threads")
text = (setting.description or "").lower()
# An operator reading only this string should learn why raising it is
# not free, otherwise the knob is an invitation to cargo-cult it.
assert "shared" in text or "async" in text, (
"description should say the pool is shared with async work"
)
assert "restart" in text, "description should say it needs a restart"
class TestLimiterIsAdjustable:
def test_total_tokens_can_be_raised_and_restored(self):
"""Guards the mechanism the lifespan relies on.
If a future AnyIO version makes ``total_tokens`` read-only, the
lifespan's resize would raise — it is defensively wrapped, so the
server would still boot and the knob would silently do nothing. This
test fails loudly instead.
"""
async def _check():
limiter = anyio.to_thread.current_default_thread_limiter()
original = limiter.total_tokens
try:
limiter.total_tokens = original + 5
assert limiter.total_tokens == original + 5
finally:
limiter.total_tokens = original
return original, limiter.total_tokens
original, restored = asyncio.run(_check())
assert restored == original
@pytest.mark.parametrize("bad", [-1, 2.5])
def test_limiter_rejects_invalid_values(self, bad):
"""AnyIO validates the assignment itself for these."""
async def _check():
limiter = anyio.to_thread.current_default_thread_limiter()
original = limiter.total_tokens
with pytest.raises((ValueError, TypeError)):
limiter.total_tokens = bad
assert limiter.total_tokens == original, (
"a rejected assignment must not have partially applied"
)
asyncio.run(_check())
def test_zero_is_accepted_by_anyio_so_our_min_bound_is_load_bearing(self):
"""AnyIO accepts ``0``, which would deadlock every sync route.
Verified rather than assumed: ``-1`` raises ValueError and ``2.5``
raises TypeError, but ``0`` is accepted silently. Nothing downstream
would catch it, so the ``min_value=1`` on the registered setting is
the only thing standing between an operator typo and a server that
accepts connections and serves no sync route at all.
"""
async def _check():
limiter = anyio.to_thread.current_default_thread_limiter()
original = limiter.total_tokens
try:
limiter.total_tokens = 0
return limiter.total_tokens
finally:
limiter.total_tokens = original
assert asyncio.run(_check()) == 0, (
"AnyIO started rejecting 0 — good, but update this test and note "
"that min_value=1 is now belt-and-braces rather than the sole guard"
)
setting = registry.get_setting_object("web.threadpool_max_threads")
assert setting.min_value >= 1, (
"min_value must exclude 0; AnyIO accepts it and it deadlocks "
"every synchronous route"
)
class TestLifespanAppliesTheSetting:
"""End-to-end: the knob actually reaches the running server.
Run in a subprocess because the limiter is process-global and must be
configured BEFORE lifespan startup — and because entering lifespan twice
in one process is unsafe here (see tests/web/test_lifespan_boot.py).
"""
@staticmethod
def _observed_limit(env_value: str | None) -> int:
import os
import subprocess
import sys
import tempfile
probe = (
"from fastapi.testclient import TestClient\n"
"from local_deep_research.web.fastapi_app import app\n"
"import anyio.to_thread\n"
"@app.get('/__tp_probe__')\n"
"async def _p():\n"
" lim = anyio.to_thread.current_default_thread_limiter()\n"
" return {'limit': lim.total_tokens}\n"
"with TestClient(app) as c:\n"
" print('LIMIT=' + str(c.get('/__tp_probe__').json()['limit']))\n"
)
env = dict(os.environ)
env["LDR_DATA_DIR"] = tempfile.mkdtemp()
env.pop("LDR_WEB_THREADPOOL_MAX_THREADS", None)
if env_value is not None:
env["LDR_WEB_THREADPOOL_MAX_THREADS"] = env_value
out = subprocess.run(
[sys.executable, "-c", probe],
capture_output=True,
text=True,
timeout=300,
env=env,
)
for line in out.stdout.splitlines():
if line.startswith("LIMIT="):
return int(line.split("=", 1)[1])
raise AssertionError(
f"probe produced no LIMIT line.\nstdout={out.stdout[-800:]}\n"
f"stderr={out.stderr[-800:]}"
)
@pytest.mark.slow
def test_unset_leaves_the_anyio_default(self):
assert self._observed_limit(None) == 40, (
"the default changed; this knob is supposed to be opt-in and "
"change nothing unless an operator sets it"
)
@pytest.mark.slow
def test_set_value_is_applied_to_the_running_server(self):
assert self._observed_limit("96") == 96, (
"LDR_WEB_THREADPOOL_MAX_THREADS did not reach the live limiter; "
"the lifespan wiring is broken and the knob is a no-op"
)