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.

15.9 KB
Raw Blame
"""Post-request cleanup: what Flask's ``teardown_appcontext`` guaranteed.

Ported from ``tests/web/test_teardown_cleanup.py`` on main (deleted by the
FastAPI migration). Main's ``cleanup_db_session`` hook — registered with
``@app.teardown_appcontext`` in ``web/app_factory.py`` — did four things
after every request, *each in its own ``try/except``*:

    session = g.pop("db_session", None)
    if session is not None:
        try: session.rollback()
        except Exception: logger.warning(...)
        try: session.close()
        except Exception: logger.warning(...)
    try: cleanup_dead_threads()
    except Exception: logger.debug(...)
    try: cleanup_current_thread()
    except Exception: logger.debug(...)

The successor is ``DatabaseMiddleware.__call__``'s ``finally`` in
``web/fastapi_app.py``. The ``g.db_session`` half has no successor and needs
none: ``g`` is gone, and the request-scoped session's lifecycle moved to
``run_db_sync``'s own ``finally``, which is pinned by
``tests/web/test_db_session_lifecycle_asgi.py``. What DID survive verbatim
is the pair of sweeper calls — but the port collapsed main's two
independent ``try`` blocks into one::

    try:
        cleanup_dead_threads()
        cleanup_current_thread()
    except Exception:
        logger.debug("Error during post-request cleanup")

That is not a stylistic tidy-up. It changes behaviour: main guaranteed
``cleanup_current_thread()`` ran *whether or not* ``cleanup_dead_threads()``
raised, and had a dedicated test for each. Here a single throw from the
first call silently skips the second for the rest of the process's life —
see ``TestIndependenceOfTheTwoSweeps`` below, which is the honest failure
this port exists to surface.

Sections:
  1. Both sweeps run after a normal request.  (main: ``test_cleanup_dead_threads_called``,
     ``test_cleanup_current_thread_called``)
  2. They still run when the handler raised.  (the point of ``teardown``/``finally``)
  3. A throwing sweep does not propagate to the client. (main:
     ``test_close_fails_no_propagation``)
  4. The two sweeps are independent.  (main: ``test_rollback_fails_close_still_called``
     applied to the surviving pair)
"""

import threading

import httpx
import pytest
from fastapi import FastAPI

from local_deep_research.database import thread_local_session as tls
from local_deep_research.web.fastapi_app import DatabaseMiddleware


class _SessionInjector:
    """Stands in for ``SessionMiddleware`` (which needs a signed cookie).

    Same helper shape as ``tests/web/test_db_session_lifecycle_asgi.py``:
    ``DatabaseMiddleware``'s whole contract with the layer above it is
    ``scope["session"]`` being a dict.
    """

    def __init__(self, app, sessions):
        self.app = app
        self.sessions = sessions

    async def __call__(self, scope, receive, send):
        if scope["type"] == "http":
            headers = dict(scope["headers"])
            user = headers.get(b"x-test-user", b"").decode()
            scope["session"] = (
                {"username": user, "session_id": self.sessions[user]}
                if user
                else {}
            )
        await self.app(scope, receive, send)


@pytest.fixture
def server_sessions():
    """Real server-side sessions so the revocation check inside
    ``DatabaseMiddleware`` stays on its real code path."""
    from local_deep_research.web.auth.session_manager import session_manager

    made = {u: session_manager.create_session(u) for u in ("alice",)}
    yield made
    for session_id in made.values():
        session_manager.destroy_session(session_id)


@pytest.fixture
def sweeps(monkeypatch):
    """Record every call to the two sweepers the middleware invokes.

    They are patched on ``database.thread_local_session`` because the
    middleware imports them from there *inside* its ``finally`` — a
    module-attribute patch is therefore what it actually resolves.
    """
    calls: list[str] = []

    def _dead():
        calls.append("dead")

    def _current():
        calls.append("current")

    monkeypatch.setattr(tls, "cleanup_dead_threads", _dead)
    monkeypatch.setattr(tls, "cleanup_current_thread", _current)
    return calls


def _stack(handler, server_sessions, path="/probe"):
    app = FastAPI()
    app.get(path)(handler)
    return _SessionInjector(DatabaseMiddleware(app), server_sessions)


async def _get(stack, path="/probe", user="alice"):
    transport = httpx.ASGITransport(app=stack, raise_app_exceptions=False)
    async with httpx.AsyncClient(
        transport=transport, base_url="http://testserver"
    ) as client:
        return await client.get(path, headers={"x-test-user": user})


@pytest.fixture(autouse=True)
def _no_real_db(monkeypatch):
    """The middleware opens the user's encrypted DB before dispatching;
    nothing here needs a real one."""
    monkeypatch.setattr(
        "local_deep_research.web.dependencies.auth.ensure_user_database",
        lambda request: None,
    )


# ---------------------------------------------------------------------------
# 1. Both sweeps run after a normal request
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_both_sweeps_run_after_a_successful_request(
    server_sessions, sweeps
):
    """main ran ``cleanup_dead_threads`` and ``cleanup_current_thread``
    after every request. Both are credential sweepers: what they reclaim
    is a thread-local DB session and the password that opened it, so a
    dropped sweep leaves a decrypted handle alive on a pooled worker.

    PREMISE UPDATE (PR #6207, follow-up to #6095): this stack's request is
    authenticated (``server_sessions`` + the default ``user="alice"`` in
    ``_get``), so ``DatabaseMiddleware`` now also runs
    ``ensure_user_database`` through ``run_db_sync`` instead of a bare
    ``asyncio.to_thread``. ``run_db_sync``'s own ``finally`` unconditionally
    calls ``cleanup_current_thread()`` on the executor thread it ran on,
    before the request is even dispatched to the inner app — that boundary
    is the whole point of the change (closing a leak where a cold rekey's
    ``get_user_db_session()`` pinned a session on a pooled default-executor
    thread with no cleanup at all). It runs regardless of whether
    ``ensure_user_database`` did anything, which is why it still fires here
    even though this file's ``_no_real_db`` fixture stubs it to a no-op —
    the cleanup is a cheap unconditional safety net by design, not
    conditioned on the wrapped call's effect. That is a genuine third sweep
    on a genuinely different thread, not a duplicate of the loop-thread
    pair below it.
    """

    def probe():
        return {"ok": True}

    response = await _get(_stack(probe, server_sessions))

    assert response.status_code == 200, response.text
    assert sweeps == ["current", "dead", "current"], (
        f"post-request cleanup ran {sweeps}; expected the middleware's "
        "run_db_sync offload of ensure_user_database to sweep once up "
        "front, then main's guaranteed pair (dead-thread first) after "
        "the request"
    )


@pytest.mark.asyncio
async def test_the_sweeps_run_on_the_loop_thread_exactly_once(
    server_sessions, monkeypatch
):
    """Guard against a double-registration (two middleware layers) and
    pin where the sweep lands, since that is the structural difference
    from Flask's request-thread teardown."""
    threads: list[int] = []
    monkeypatch.setattr(
        tls,
        "cleanup_dead_threads",
        lambda: threads.append(threading.get_ident()),
    )
    monkeypatch.setattr(tls, "cleanup_current_thread", lambda: None)

    def probe():
        return {"ok": True}

    await _get(_stack(probe, server_sessions))

    assert len(threads) == 1, f"dead-thread sweep ran {len(threads)} times"
    assert threads[0] == threading.get_ident(), (
        "the sweep must run on the event-loop thread — this middleware's "
        "cleanup is async, unlike Flask's teardown_appcontext"
    )


# ---------------------------------------------------------------------------
# 2. They still run when the handler raised
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_both_sweeps_run_when_the_handler_raises(server_sessions, sweeps):
    """``teardown_appcontext`` ran on the error path too; a ``finally``
    must as well. If it did not, the one request shape most likely to
    have left a half-open session behind is the one that skips cleanup.

    PREMISE UPDATE (PR #6207): same reasoning as
    ``test_both_sweeps_run_after_a_successful_request`` above — the
    authenticated request now also produces a leading ``run_db_sync``
    sweep of ``ensure_user_database`` before the (raising) handler ever
    runs, so the expected sequence gains that same third, earlier entry.
    """

    def boom():
        raise RuntimeError("handler exploded")

    await _get(_stack(boom, server_sessions))

    assert sweeps == ["current", "dead", "current"], (
        f"a failing handler left cleanup at {sweeps}; expected the "
        "run_db_sync offload sweep up front plus the finally block's "
        "guaranteed pair (dead-thread first) on the error path"
    )


# ---------------------------------------------------------------------------
# 3. A throwing sweep does not reach the client
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_a_throwing_sweep_does_not_escape_the_middleware(
    server_sessions, monkeypatch
):
    """main swallowed cleanup failures (``logger.debug``) rather than
    letting them out of the teardown hook.

    Driven against the raw ASGI callable rather than through an HTTP
    client on purpose. The response is already on the wire by the time the
    ``finally`` runs, so *any* client — httpx's ASGITransport included —
    reports 200 whether or not the sweep's exception escaped; asserting on
    the status code would pass with the ``except`` deleted. Awaiting
    ``__call__`` directly is the only place the escape is observable.
    """
    monkeypatch.setattr(
        tls,
        "cleanup_dead_threads",
        lambda: (_ for _ in ()).throw(RuntimeError("sweep failed")),
    )
    monkeypatch.setattr(tls, "cleanup_current_thread", lambda: None)

    def probe():
        return {"ok": True}

    stack = _stack(probe, server_sessions)
    sent: list[dict] = []

    scope = {
        "type": "http",
        "asgi": {"version": "3.0", "spec_version": "2.3"},
        "http_version": "1.1",
        "method": "GET",
        "scheme": "http",
        "path": "/probe",
        "raw_path": b"/probe",
        "query_string": b"",
        "root_path": "",
        "headers": [(b"host", b"testserver"), (b"x-test-user", b"alice")],
        "client": ("127.0.0.1", 12345),
        "server": ("testserver", 80),
    }

    async def receive():
        return {"type": "http.request", "body": b"", "more_body": False}

    async def send(message):
        sent.append(message)

    # No pytest.raises wrapper: an escape fails the test right here, with
    # the original traceback rather than a re-worded assertion.
    await stack(scope, receive, send)

    assert sent and sent[0]["type"] == "http.response.start", sent
    assert sent[0]["status"] == 200, (
        "positive control: the request must have been served, or the "
        "no-escape claim above is about a request that never happened"
    )


# ---------------------------------------------------------------------------
# 4. The two sweeps are independent
# ---------------------------------------------------------------------------


class TestIndependenceOfTheTwoSweeps:
    """main wrapped each cleanup step in its OWN ``try``; this branch
    shares one. See the module docstring.

    main's ``test_rollback_fails_close_still_called`` pinned exactly this
    shape on the rollback/close pair; the same reasoning applies to the
    pair that survived the port.
    """

    @pytest.mark.asyncio
    async def test_positive_control_a_healthy_first_sweep_reaches_the_second(
        self, server_sessions, sweeps
    ):
        """Without this, the failing test below would be satisfied by a
        second sweep that never runs at all.

        PREMISE UPDATE (PR #6207): this invocation is byte-identical to
        ``test_both_sweeps_run_after_a_successful_request``, whose expected
        sequence this PR changed to ``["current", "dead", "current"]`` --
        the leading entry being ``run_db_sync``'s unconditional sweep of the
        executor thread that ran ``ensure_user_database``, fired BEFORE the
        request is dispatched. A bare ``"current" in sweeps`` is therefore
        now satisfied by that leading entry alone: it would still pass with
        the entire post-request pair gone, which is exactly the vacuity this
        control exists to rule out. Count instead -- two ``"current"``
        sweeps means run_db_sync's AND the post-request one both ran, so a
        healthy first sweep really did reach the second.
        """

        def probe():
            return {"ok": True}

        await _get(_stack(probe, server_sessions))
        assert sweeps.count("current") == 2, (
            f"post-request cleanup ran {sweeps}; expected run_db_sync's "
            "up-front sweep plus the post-request current-thread sweep. One "
            "``current`` means the post-request pair never ran, which would "
            "make the sibling failure test below vacuous"
        )

    @pytest.mark.asyncio
    async def test_current_thread_sweep_still_runs_when_dead_sweep_throws(
        self, server_sessions, monkeypatch
    ):
        """LIVE LOSS (as of 76eed009b).

        ``cleanup_current_thread`` reclaims the CALLING thread's session and
        the cached password that opened it. ``cleanup_dead_threads`` sweeps
        entries for threads that have already exited — it walks a shared
        registry and is the more likely of the two to throw (a mutated dict,
        a closed engine). Under this branch's single ``try`` that throw takes
        the current-thread sweep with it, so the loop thread's own entry is
        never reclaimed. main's two separate blocks made that impossible.

        PREMISE UPDATE (PR #6207): the leading ``"current"`` entry below is
        NOT part of the dead/current pair this test is about — it is
        ``run_db_sync``'s own unconditional cleanup of the executor thread
        that ran ``ensure_user_database`` (see
        ``test_both_sweeps_run_after_a_successful_request`` for the full
        explanation), and it happens before the handler runs, so it happens
        before ``cleanup_dead_threads`` even has a chance to throw. The
        property this test actually pins — that a throwing dead-thread
        sweep does not take the current-thread sweep down with it — is
        still exactly the last two entries, ``["dead", "current"]``.
        """
        calls: list[str] = []

        def _dead():
            calls.append("dead")
            raise RuntimeError("registry mutated during sweep")

        monkeypatch.setattr(tls, "cleanup_dead_threads", _dead)
        monkeypatch.setattr(
            tls, "cleanup_current_thread", lambda: calls.append("current")
        )

        def probe():
            return {"ok": True}

        response = await _get(_stack(probe, server_sessions))

        assert response.status_code == 200, response.text
        assert calls == ["current", "dead", "current"], (
            "cleanup_dead_threads raised and took cleanup_current_thread "
            f"down with it (calls={calls}). DatabaseMiddleware.__call__'s "
            "finally in web/fastapi_app.py runs cleanup_dead_threads and "
            "cleanup_current_thread in two independent try/except blocks "
            "precisely so one failing sweeper cannot disable the other; "
            "this assertion pins that isolation holding. (leading entry is "
            "run_db_sync's own pre-dispatch sweep of ensure_user_database, "
            "unrelated to the dead/current pair)"
        )