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.

8.1 KB
Raw Blame
"""
Contextvar propagation fences for ``utilities/request_context``.

The FastAPI migration relies on three distinct propagation behaviors of
the request-user contextvars, each exercised by real production code:

1. ``run_db_sync`` (web/dependencies/threadpool.py) offloads sync DB work
   via ``asyncio.to_thread`` — which copies the caller's context — so
   ``get_current_username()`` inside the offloaded callable must resolve
   to the request's user (rag.py relies on this when spawning the
   background index worker from inside ``run_db_sync``).

2. Routers (auth.py `_ctx_post_login`, rag.py `_ctx_worker` /
   "index-collection-parallel") wrap background threads in
   ``contextvars.copy_context().run(...)`` so the worker sees the
   authenticated user. The snapshot must also survive the request ending
   (middleware resetting the vars) after the thread was handed the copy.

3. Plain ``ThreadPoolExecutor.submit`` does NOT copy the caller's
   context — worker threads start with an empty context. This is exactly
   why ``database.session_passwords.capture_request_db_password`` exists:
   the DB password must be captured on the request thread and passed to
   workers explicitly. If Python/executor semantics ever changed (or a
   custom context-propagating executor were swapped in globally), that
   design rationale would be stale — this test documents the invariant.

These are propagation fences, deliberately separate from
tests/utilities/test_request_context.py which pins the basic
set/get/reset semantics and per-thread / per-task isolation.
"""

from __future__ import annotations

import asyncio
import contextvars
import threading
from concurrent.futures import ThreadPoolExecutor
from unittest.mock import patch

from local_deep_research.utilities.request_context import (
    get_current_session_id,
    get_current_username,
    request_user,
    reset_request_user,
    set_request_user,
)
from local_deep_research.web.dependencies.threadpool import run_db_sync

# run_db_sync imports cleanup_current_thread lazily inside the worker;
# patch it out so these tests never touch the SQLCipher engine bootstrap.
_CLEANUP_TARGET = (
    "local_deep_research.database.thread_local_session.cleanup_current_thread"
)


def _snapshot() -> tuple:
    """Record both request-context values as seen by the calling thread."""
    return (get_current_username(), get_current_session_id())


class TestRunDbSyncPropagation:
    """(a) The offloaded callable must see the request's user."""

    def test_offloaded_callable_sees_request_user(self):
        async def _runner():
            with request_user("dave", "sess-dave"):
                return await run_db_sync(_snapshot)

        with patch(_CLEANUP_TARGET):
            assert asyncio.run(_runner()) == ("dave", "sess-dave")

    def test_offloaded_callable_sees_user_even_when_fn_raises(self):
        """Propagation must hold on the error path too — exception
        handlers inside the callable may log with user attribution."""
        observed = {}

        def _boom():
            observed["ctx"] = _snapshot()
            raise RuntimeError("expected")

        async def _runner():
            with request_user("erin", "sess-erin"):
                try:
                    await run_db_sync(_boom)
                except RuntimeError:
                    pass

        with patch(_CLEANUP_TARGET):
            asyncio.run(_runner())
        assert observed["ctx"] == ("erin", "sess-erin")

    def test_worker_mutation_does_not_leak_back_to_caller(self):
        """to_thread runs in a *copy* of the context — a set() inside the
        worker must not rebind the request task's vars."""

        async def _runner():
            with request_user("outer-user", "outer-sess"):
                await run_db_sync(
                    set_request_user, "worker-user", "worker-sess"
                )
                return _snapshot()

        with patch(_CLEANUP_TARGET):
            assert asyncio.run(_runner()) == ("outer-user", "outer-sess")


class TestCopyContextThreadPattern:
    """(b) The router background-thread pattern:
    ``ctx = copy_context(); Thread(target=ctx.run, args=(worker,))``."""

    def test_copy_context_thread_sees_request_user(self):
        observed = {}

        def _worker():
            observed["ctx"] = _snapshot()

        with request_user("frank", "sess-frank"):
            ctx = contextvars.copy_context()
            t = threading.Thread(target=ctx.run, args=(_worker,))
            t.start()
            t.join(timeout=5)

        assert not t.is_alive()
        assert observed["ctx"] == ("frank", "sess-frank")

    def test_snapshot_survives_request_reset(self):
        """auth.py starts the post-login daemon thread and then returns
        the redirect; middleware resets the request vars. The snapshot
        handed to the thread must still resolve the logged-in user."""
        tokens = set_request_user("grace", "sess-grace")
        ctx = contextvars.copy_context()
        reset_request_user(tokens)
        # The request is "over" — the caller no longer sees grace ...
        assert get_current_username() != "grace"

        observed = {}

        def _worker():
            observed["ctx"] = _snapshot()

        # ... but the worker started afterwards from the snapshot does.
        t = threading.Thread(target=ctx.run, args=(_worker,))
        t.start()
        t.join(timeout=5)
        assert not t.is_alive()
        assert observed["ctx"] == ("grace", "sess-grace")

    def test_worker_set_inside_copied_context_does_not_leak(self):
        with request_user("henry", "sess-henry"):
            ctx = contextvars.copy_context()
            t = threading.Thread(
                target=ctx.run,
                args=(set_request_user, "imposter", "sess-imposter"),
            )
            t.start()
            t.join(timeout=5)
            assert not t.is_alive()
            assert _snapshot() == ("henry", "sess-henry")


class TestPlainExecutorDoesNotPropagate:
    """(c) Plain ``ThreadPoolExecutor.submit`` starts workers with an
    empty context — the documented reason capture_request_db_password
    must run on the request thread."""

    def test_submit_worker_sees_no_request_user(self):
        with request_user("iris", "sess-iris"):
            with ThreadPoolExecutor(max_workers=1) as pool:
                worker_view = pool.submit(_snapshot).result(timeout=5)
        assert worker_view == (None, None), (
            "ThreadPoolExecutor.submit unexpectedly propagated the request "
            "context — capture_request_db_password's explicit-capture design "
            "rationale (database/session_passwords.py) needs revisiting"
        )

    def test_submit_with_explicit_copy_context_is_the_fix(self):
        """Contrast case: submitting ``ctx.run`` (what langchain's
        ContextThreadPoolExecutor does) restores propagation — pinning
        that the gap is in submit(), not in the contextvar itself."""
        with request_user("judy", "sess-judy"):
            ctx = contextvars.copy_context()
            with ThreadPoolExecutor(max_workers=1) as pool:
                worker_view = pool.submit(ctx.run, _snapshot).result(timeout=5)
        assert worker_view == ("judy", "sess-judy")

    def test_reused_worker_does_not_inherit_prior_task_context(self):
        """Even if a prior task set the vars *inside* a pool worker, a
        later submit on the same worker must not observe them — the
        cross-user leakage scenario run_db_sync's cleanup guards against
        at the DB-session layer must not exist at the contextvar layer."""
        with ThreadPoolExecutor(max_workers=1) as pool:
            # Task 1 pollutes the worker's context via a copied context.
            with request_user("kate", "sess-kate"):
                ctx = contextvars.copy_context()
                pool.submit(
                    ctx.run, set_request_user, "kate", "sess-kate"
                ).result(timeout=5)
            # Task 2 on the same (single) worker starts clean because
            # each submit without ctx.run executes in the worker
            # thread's own context, which was never rebound.
            later_view = pool.submit(_snapshot).result(timeout=5)
        assert later_view[0] != "kate"