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.

6.7 KB
Raw Blame
"""
Regression test: get_user_db_session must release its pooled connection when
the outermost block ends, not hold it for the thread's lifetime.

Before the fix, the thread-local session (and its checked-out QueuePool
connection) lived until cleanup_current_thread(). M long-lived worker threads
therefore pinned M connections; once M > pool_size + max_overflow, every new
block stalled on pool_timeout (the "long periods of inactivity" / QueuePool
TimeoutError). After the fix, the connection is released at scope end, so the
checked-out count tracks in-flight blocks, not live threads.

This test FAILS on the buggy code (connections pinned: checkedout == n) and
PASSES on the fix (connections released: checkedout ~ 0).

Self-contained: imports only session_context + thread_local_session and points
their db_manager at a real QueuePool SQLite engine (no fastapi/langchain chain).
"""

import threading

import pytest
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import QueuePool

from local_deep_research.database import session_context, thread_local_session


class _FakeDBManager:
    """Stand-in for encrypted_db.db_manager backed by a real QueuePool engine."""

    def __init__(self, engine):
        self._engine = engine
        self._sessionmaker = sessionmaker(bind=engine)
        self.has_encryption = False

    def open_user_database(self, username, password):
        return self._engine

    def create_thread_safe_session_for_metrics(self, username, password):
        return self._sessionmaker()


@pytest.fixture
def pooled_engine(monkeypatch):
    engine = create_engine(
        "sqlite://",
        poolclass=QueuePool,
        pool_size=20,
        max_overflow=40,
        pool_timeout=5,
        # The per-user QueuePool shares connections across worker threads;
        # default SQLite (check_same_thread=True) would reject that.
        connect_args={"check_same_thread": False},
    )
    with engine.connect() as conn:
        conn.execute(text("CREATE TABLE IF NOT EXISTS t (id INTEGER)"))
        conn.commit()
    fake = _FakeDBManager(engine)
    manager = thread_local_session.ThreadLocalSessionManager()
    monkeypatch.setattr(thread_local_session, "thread_session_manager", manager)
    monkeypatch.setattr(session_context, "thread_session_manager", manager)
    monkeypatch.setattr(thread_local_session, "db_manager", fake)
    monkeypatch.setattr(session_context, "db_manager", fake)
    try:
        yield engine
    finally:
        manager.cleanup_thread()
        engine.dispose()


def test_connection_released_when_block_ends(pooled_engine):
    engine = pooled_engine
    username = "alice"
    n = 15  # worker threads that stay alive after their block

    errors = []
    barrier = threading.Barrier(n + 1)  # n workers + main
    release = threading.Event()

    def worker():
        try:
            with session_context.get_user_db_session(username) as s:
                s.execute(text("SELECT 1"))
        except Exception as exc:  # noqa: BLE001
            errors.append(exc)
        barrier.wait(timeout=20)  # every worker finished its `with` block
        release.wait(timeout=20)  # stay alive (pins the session pre-fix)

    threads = [threading.Thread(target=worker, daemon=True) for _ in range(n)]
    for t in threads:
        t.start()
    barrier.wait(
        timeout=30
    )  # main is the (n+1)th party; returns when all n done
    # All n workers have completed their block but are still alive.
    checked = engine.pool.checkedout()
    release.set()
    for t in threads:
        t.join(timeout=20)

    assert not errors, f"worker errors: {errors}"
    # Pre-fix: each live thread pins its session -> checkedout == n (15).
    # Post-fix: connection released at scope end -> checkedout ~ 0.
    assert checked <= 1, (
        f"connections still pinned after all blocks ended: checkedout={checked} "
        f"(expected <= 1). The thread-local session is holding its QueuePool "
        f"connection for the thread's lifetime instead of releasing it at "
        f"scope end."
    )


def test_nested_blocks_do_not_close_early(pooled_engine):
    """A nested get_user_db_session must not release the shared connection
    while the enclosing block is still active (depth > 0).

    An uncommitted write on the outer session must survive the inner block's
    exit: if the inner exit had closed the shared connection, the outer
    session would be detached (or re-acquire a new connection that can't see
    the uncommitted write), so the SELECT below would fail. A bare
    ``SELECT 1`` can't establish this — after an accidental ``close()`` it
    silently acquires another connection and still passes.
    """
    engine = pooled_engine
    username = "alice"

    with session_context.get_user_db_session(username) as outer:
        # Uncommitted write on the shared session (deliberately not committed).
        outer.execute(text("INSERT INTO t (id) VALUES (42)"))
        # Nested block on the same thread reuses the shared session.
        with session_context.get_user_db_session(username) as inner:
            inner.execute(text("SELECT 1"))
        # The outer block is still active: its connection must NOT have been
        # released by the inner block's exit. The uncommitted write must still
        # be visible on the SAME connection/transaction (a re-acquired
        # connection couldn't see it).
        row = outer.execute(text("SELECT id FROM t WHERE id = 42")).fetchone()
        assert row is not None, (
            "uncommitted outer write not visible after inner block exit — the "
            "shared connection was closed by the nested block's exit, "
            "detaching the outer session."
        )

    # After the outermost block ends, the connection is released.
    assert engine.pool.checkedout() <= 1


def test_committed_resource_remains_readable_after_scope_exit(pooled_engine):
    """Connection release must preserve add_resource's ORM return contract."""
    from local_deep_research.database.models import Base
    from local_deep_research.web.services.resource_service import add_resource

    Base.metadata.create_all(pooled_engine)
    resource = add_resource(
        research_id="research-1",
        title="Open-access paper",
        url="https://example.com/paper.pdf",
        username="alice",
    )

    # The helper committed and its outermost scope returned the connection.
    assert pooled_engine.pool.checkedout() == 0
    # Production sessions expire on commit. Closing would also detach this
    # instance, making these real caller reads raise DetachedInstanceError.
    assert resource.id is not None
    assert resource.title == "Open-access paper"
    assert resource.url == "https://example.com/paper.pdf"