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.

4.7 KB
Raw Blame
"""Re-entrant get_user_db_session must not roll back the caller's writes.

The thread-local session cache re-validates on every get_session call with
``SELECT 1`` followed by a rollback (added to release the SQLite SHARED
lock the validation SELECT takes under DEFERRED isolation). But a NESTED
get_user_db_session call — a helper opening its own session inside a
caller's with-block, e.g. library_rag_service.index_document →
embedding_manager._store_chunks_to_db — got the same cached session, and
that rollback silently destroyed the caller's uncommitted writes.

The fix tracks a per-thread scope depth: the stale-lock rollback only
fires when NO enclosing get_user_db_session block is active.
"""

import threading
from contextlib import contextmanager
from unittest.mock import Mock, patch

from local_deep_research.database.thread_local_session import (
    ThreadLocalSessionManager,
)

CTX = "local_deep_research.database.session_context"


class TestScopeTracking:
    def test_depth_nesting(self):
        m = ThreadLocalSessionManager()
        assert m.in_scope() is False
        m.enter_scope()
        assert m.in_scope() is True
        m.enter_scope()
        m.exit_scope()
        assert m.in_scope() is True
        m.exit_scope()
        assert m.in_scope() is False

    def test_exit_never_goes_negative(self):
        m = ThreadLocalSessionManager()
        m.exit_scope()
        m.enter_scope()
        assert m.in_scope() is True

    def test_scope_is_per_thread(self):
        m = ThreadLocalSessionManager()
        m.enter_scope()
        seen = {}

        def worker():
            seen["other_thread_in_scope"] = m.in_scope()

        t = threading.Thread(target=worker)
        t.start()
        t.join()

        assert seen["other_thread_in_scope"] is False
        assert m.in_scope() is True


class TestCachedSessionRevalidation:
    def _manager_with_cached_session(self):
        m = ThreadLocalSessionManager()
        session = Mock()
        m._local.session = session
        m._local.username = "alice"
        return m, session

    def test_top_level_call_rolls_back_stale_lock(self):
        """No enclosing block: the validation SELECT's SHARED lock must be
        released (the original behavior, still required)."""
        m, session = self._manager_with_cached_session()

        result = m.get_session("alice", "pw")

        assert result is session
        session.execute.assert_called_once()
        session.rollback.assert_called_once()

    def test_reentrant_call_preserves_callers_transaction(self):
        """Enclosing block active: rolling back would destroy the caller's
        uncommitted writes — must NOT happen."""
        m, session = self._manager_with_cached_session()
        m.enter_scope()

        result = m.get_session("alice", "pw")

        assert result is session
        session.execute.assert_called_once()
        session.rollback.assert_not_called()


class TestGetUserDbSessionScope:
    @contextmanager
    def _patched(self, session=None):
        session = session or Mock()
        with (
            patch(
                "local_deep_research.database.thread_local_session"
                ".get_metrics_session",
                return_value=session,
            ),
            patch(
                "local_deep_research.database.session_passwords"
                ".session_password_store"
            ) as store,
        ):
            store.get_any_session_password.return_value = "pw"
            yield session

    def test_nested_blocks_track_depth(self):
        from local_deep_research.database.session_context import (
            get_user_db_session,
        )
        from local_deep_research.database.thread_local_session import (
            thread_session_manager,
        )

        assert thread_session_manager.in_scope() is False
        with self._patched():
            with get_user_db_session("alice"):
                assert thread_session_manager.in_scope() is True
                with get_user_db_session("alice"):
                    assert thread_session_manager.in_scope() is True
                assert thread_session_manager.in_scope() is True
        assert thread_session_manager.in_scope() is False

    def test_scope_exits_on_exception(self):
        import pytest

        from local_deep_research.database.session_context import (
            get_user_db_session,
        )
        from local_deep_research.database.thread_local_session import (
            thread_session_manager,
        )

        with self._patched():
            with pytest.raises(RuntimeError):
                with get_user_db_session("alice"):
                    raise RuntimeError("boom")
        assert thread_session_manager.in_scope() is False