Skip to content

~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.

7.6 KB
Raw Blame
# allow: no-sut-import — static AST lint over the web-layer source files;
# importing the app would add nothing (the scan reads the files directly).
"""Static guard: no blocking calls directly inside ``async def`` handlers.

The FastAPI app runs a single uvicorn event loop. Sync (``def``) route
handlers are automatically threadpooled, but ``async def`` handlers run ON
the loop — any synchronous DB / network / disk / sleep call inside one
stalls every in-flight request and WebSocket until it returns. The
post-migration review found 12 such regressions (SQLCipher session opens,
settings reads, PDF extraction, a loopback HTTP POST) that no runtime test
caught, because latency assertions are flaky in CI while the *pattern* is
statically detectable.

This test walks every ``async def`` in the web layer and fails when a
known-blocking callable is invoked directly in its body. Nested ``def``
thunks are exempt — that is exactly the sanctioned pattern::

    async def handler(...):
        data = await request.json()          # legitimate await

        def _impl():                          # sync body ...
            with get_user_db_session(...):    # ... may block freely
                ...

        return await run_db_sync(_impl)       # ... runs on the threadpool

A deliberate exception can be waived with ``# allow: sync-in-async`` on the
offending line plus a justifying comment.
"""

import ast
from pathlib import Path

SRC = Path(__file__).resolve().parents[3] / "src" / "local_deep_research"
SRC_WEB = SRC / "web"

# Modules whose async defs run on the event loop.
SCANNED_FILES = sorted(
    [
        *(SRC_WEB / "routers").glob("*.py"),
        SRC_WEB / "fastapi_app.py",
        SRC_WEB / "services" / "socketio_asgi.py",
        *(SRC_WEB / "dependencies").glob("*.py"),
        # Service-layer modules that are awaited DIRECTLY from an async
        # route body rather than dispatched to the threadpool, so their
        # own ``async def``s run on the loop exactly like a handler's.
        # NoteAIService (#5854) is the notes AI surface behind nine async
        # routes in routers/notes.py; its DB work must go through
        # ``_offload_db``, never straight into ``get_user_db_session``.
        SRC / "research_library" / "notes" / "services" / "note_ai_service.py",
    ]
)

# Callables (by simple name) that block: DB session opens (SQLCipher +
# possible PBKDF2 key derivation), settings reads, sync HTTP, verified disk
# writes. Referenced by name only — passing them as an argument to
# run_db_sync / asyncio.to_thread does not trip the guard (that's a
# reference, not a call).
BLOCKING_NAMES = {
    "get_user_db_session",
    "get_settings_manager",
    "get_default_library_id",
    "ensure_user_database",
    "get_metrics_session",
    "safe_post",
    "safe_get",
    "write_file_verified",
    "SettingsManager",
}

# Method names that mean "synchronous settings/DB read" or heavy CPU work
# regardless of the object they're called on.
BLOCKING_ATTRS = {
    "get_setting",
    "get_all_settings",
    "get_settings_snapshot",
    # PDF parsing / upload validation are CPU-bound (up to 50MB per file).
    "extract_text_and_metadata",
    "validate_upload",
}

# module.attr call chains that block.
BLOCKING_CHAINS = {
    ("time", "sleep"),
    ("requests", "get"),
    ("requests", "post"),
    ("requests", "put"),
    ("requests", "delete"),
    ("shutil", "rmtree"),
    ("subprocess", "run"),
    ("subprocess", "check_output"),
}

# Modules whose EVERY function opens the user's DB session synchronously —
# any `<module>.<anything>()` call counts as blocking. `api` is the news
# helper module (`from ...news import api` in news_flask_api.py); its CRUD
# helpers all open get_user_db_session internally, which a one-level static
# scan cannot see. (This guard is one-level by design: it inspects handler
# bodies, not transitive callees.)
BLOCKING_MODULE_PREFIXES = {"api"}


def _call_target(node: ast.Call):
    """Return (kind, key) describing what a Call node invokes."""
    func = node.func
    if isinstance(func, ast.Name):
        return "name", func.id
    if isinstance(func, ast.Attribute):
        if isinstance(func.value, ast.Name):
            return "chain", (func.value.id, func.attr)
        return "attr", func.attr
    return "other", None


class _AsyncBodyScanner(ast.NodeVisitor):
    """Collect blocking calls in an async function body, skipping nested
    function definitions (their bodies run wherever they're dispatched —
    typically the threadpool via run_db_sync / to_thread)."""

    def __init__(self, source_lines):
        self.source_lines = source_lines
        self.violations = []

    def visit_FunctionDef(self, node):
        # Nested sync def: sanctioned thunk — do not descend.
        return

    def visit_AsyncFunctionDef(self, node):
        # Nested async def (rare; e.g. SSE generators): checked separately
        # by the outer walk — do not double-descend.
        return

    def visit_Lambda(self, node):
        # Lambdas here are thunks handed to run_db_sync/to_thread — their
        # bodies execute on the threadpool, like nested defs.
        return

    def visit_Call(self, node):
        kind, key = _call_target(node)
        hit = None
        if kind == "name" and key in BLOCKING_NAMES:
            hit = key
        elif kind == "chain":
            if key in BLOCKING_CHAINS:
                hit = ".".join(key)
            elif key[0] in BLOCKING_MODULE_PREFIXES:
                hit = f"{key[0]}.{key[1]}"
            elif key[1] in BLOCKING_ATTRS:
                hit = f"*.{key[1]}"
            elif key[0] not in ("asyncio", "anyio") and key[1] in (
                BLOCKING_NAMES
            ):
                hit = f"{key[0]}.{key[1]}"
        elif kind == "attr" and key in BLOCKING_ATTRS:
            hit = f"*.{key}"

        if hit is not None:
            line = self.source_lines[node.lineno - 1]
            if "# allow: sync-in-async" not in line:
                self.violations.append((node.lineno, hit))
        self.generic_visit(node)


def _scan_file(path: Path):
    source = path.read_text(encoding="utf-8")
    source_lines = source.split("\n")
    tree = ast.parse(source, filename=str(path))
    violations = []
    for node in ast.walk(tree):
        if not isinstance(node, ast.AsyncFunctionDef):
            continue
        scanner = _AsyncBodyScanner(source_lines)
        for stmt in node.body:
            scanner.visit(stmt)
        for lineno, what in scanner.violations:
            violations.append((path, node.name, lineno, what))
    return violations


def test_no_blocking_calls_in_async_web_handlers():
    all_violations = []
    scanned = 0
    for path in SCANNED_FILES:
        if not path.exists():
            continue
        scanned += 1
        all_violations.extend(_scan_file(path))

    assert scanned >= 10, (
        f"Only {scanned} files scanned — the web-layer layout moved; "
        "update SCANNED_FILES so this guard keeps covering the routers."
    )

    if all_violations:
        details = "\n".join(
            f"  {p.relative_to(SRC.parent)}:{ln} "
            f"async {fn}() calls blocking {what}"
            for p, fn, ln, what in all_violations
        )
        raise AssertionError(
            "Blocking call(s) directly inside async handlers — these run "
            "ON the uvicorn event loop and stall every request/WebSocket "
            "until they return. Move the sync work into a nested def and "
            "dispatch it via `await run_db_sync(_impl)` (DB work) or "
            "`await asyncio.to_thread(...)` (pure CPU/IO), or waive a "
            "deliberate case with `# allow: sync-in-async` + a comment.\n"
            f"{details}"
        )