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.

5.4 KB
Raw Blame
"""``database_sink`` must never write to the database on the event loop.

The sink chooses between queueing a log entry to a background writer and
writing it synchronously. A synchronous write is
``_write_log_to_database -> get_user_db_session -> possibly
open_user_database`` — SQLCipher PBKDF2 key derivation, engine construction
and a commit. On the event loop, with ``workers=1``, that stalls the entire
process: HTTP, Socket.IO, SSE and the health endpoint.

The dispatch used to be ``threading.current_thread().name != "MainThread"``.
Flask's version had a second clause (``not has_app_context() or ...``) and,
under Werkzeug, request and Socket.IO handlers ran on ``Thread-N``, so the
synchronous branch was effectively startup-only. The port dropped the Flask
clause — correctly, there is no Flask — but kept a thread-name test whose
meaning inverted: under uvicorn the event loop IS ``MainThread``, because
``web/app.py`` calls ``uvicorn.run()`` from ``main()``.

Reproduced against a real server: the loop thread is literally ``MainThread``,
so any ``logger.*`` call on the loop took the synchronous branch. Socket.IO
coroutines scheduled from research threads log there routinely.

No pre-existing test could catch this. ``TestClient`` drives the app from an
``asyncio-portal-*`` thread, so the thread name never matches and the
synchronous branch is unreachable under pytest's usual harness — which is why
these tests assert on the dispatch decision from inside a REAL running loop
rather than through a client.
"""

import asyncio
import threading

import pytest


def _dispatch_taken():
    """Return "queue" or "sync" for the current thread, by driving the real
    sink and observing which path it takes."""
    from local_deep_research.utilities import log_utils

    calls = {"queued": 0, "sync": 0}

    real_put = log_utils._log_queue.put_nowait

    def fake_put(entry):
        calls["queued"] += 1
        return real_put(entry)

    def fake_write(entry):
        calls["sync"] += 1

    orig_write = log_utils._write_log_to_database
    log_utils._log_queue.put_nowait = fake_put
    log_utils._write_log_to_database = fake_write
    try:
        record = {
            "message": "probe",
            "name": "probe.module",
            "function": "probe_fn",
            "line": 1,
            "level": type("L", (), {"name": "INFO"})(),
            "time": __import__("datetime").datetime.now(),
            "extra": {"research_id": "r-probe"},
            "exception": None,
        }
        log_utils.database_sink(type("M", (), {"record": record})())
    finally:
        log_utils._log_queue.put_nowait = real_put
        log_utils._write_log_to_database = orig_write

    if calls["sync"]:
        return "sync"
    if calls["queued"]:
        return "queue"
    return "neither"


class TestSinkDispatch:
    def test_on_a_running_event_loop_the_sink_queues(self):
        """The regression pin.

        Runs on the MAIN thread with a real loop running on it — exactly the
        shipped ``uvicorn.run()`` shape — so a thread-name-only dispatch takes
        the synchronous branch and fails here.
        """
        assert threading.current_thread().name == "MainThread", (
            "this test must run on the main thread to reproduce the shipped "
            "uvicorn shape; got "
            f"{threading.current_thread().name!r}"
        )

        async def _inner():
            return _dispatch_taken()

        taken = asyncio.run(_inner())

        assert taken == "queue", (
            "database_sink performed a SYNCHRONOUS database write while an "
            "event loop was running on this thread. That write is a "
            "SQLCipher open + commit on the event loop, and with workers=1 it "
            "stalls the whole process."
        )

    def test_main_thread_without_a_loop_still_writes_directly(self):
        """Startup / CLI behaviour is preserved — this is not 'always queue'."""
        assert threading.current_thread().name == "MainThread"
        assert _dispatch_taken() == "sync"

    def test_background_thread_queues(self):
        """Unchanged: worker and research threads always queue."""
        result = {}

        def worker():
            result["taken"] = _dispatch_taken()

        t = threading.Thread(target=worker, name="Thread-probe")
        t.start()
        t.join(timeout=10)

        assert result.get("taken") == "queue"

    def test_background_thread_running_a_loop_queues(self):
        """A loop on a non-main thread queues for both reasons at once."""
        result = {}

        def worker():
            async def _inner():
                return _dispatch_taken()

            result["taken"] = asyncio.run(_inner())

        t = threading.Thread(target=worker, name="Thread-probe-loop")
        t.start()
        t.join(timeout=10)

        assert result.get("taken") == "queue"


@pytest.mark.parametrize("thread_name", ["MainThread", "Thread-7"])
def test_dispatch_never_sync_while_a_loop_runs(thread_name, monkeypatch):
    """Whatever the thread is called, a running loop means queue.

    Thread name is the wrong question; this pins the right one so a future
    refactor cannot quietly reintroduce a name-based proxy.
    """
    fake = type("T", (), {"name": thread_name})()
    monkeypatch.setattr(threading, "current_thread", lambda: fake)

    async def _inner():
        return _dispatch_taken()

    assert asyncio.run(_inner()) == "queue"