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.1 KB
Raw Blame
"""Logout clears ``ThreadLocalSessionManager`` credential-registry entries.

``ThreadLocalSessionManager`` caches ``(username, password)`` per thread so a
pooled worker can rebuild its session without a round-trip to the password
store. The password is the user's real SQLCipher master key, in plaintext, in
a process-wide registry keyed by thread and username.

The registry supports username-wide removal independently of which thread
recorded an entry. Separate owner-thread-local storage is outside this class;
normal owned worker boundaries call ``cleanup_current_thread()``, and direct
integrations must do likewise.

These tests pin only the logout-time session-manager registry clear. They
drive the manager directly so the registry contents and username scoping are
explicit.
"""

import threading

from local_deep_research.database.thread_local_session import (
    clear_user_credentials,
    thread_session_manager,
)


def _seed_credentials_on_worker(username: str, password: str) -> int:
    """Register a credential entry from a *different* thread, as a pooled
    AnyIO worker would, and return that thread's id."""
    thread_id = {}
    ready = threading.Event()
    release = threading.Event()

    def worker():
        thread_id["id"] = threading.get_ident()
        with thread_session_manager._lock:
            thread_session_manager._thread_credentials[thread_id["id"]] = (
                username,
                password,
            )
        ready.set()
        # Stay alive so cleanup_dead_threads() cannot mask the behaviour
        # under test by reaping the thread.
        release.wait(timeout=10)

    t = threading.Thread(target=worker, daemon=True)
    t.start()
    assert ready.wait(timeout=5), "worker did not seed credentials"
    _seed_credentials_on_worker.release = release
    return thread_id["id"]


def _entries_for(username: str):
    with thread_session_manager._lock:
        return [
            tid
            for tid, (user, _pw) in (
                thread_session_manager._thread_credentials.items()
            )
            if user == username
        ]


class TestLogoutClearsThreadCredentials:
    def test_credentials_on_a_live_worker_thread_are_cleared(self):
        username = "logout_creds_user"
        tid = _seed_credentials_on_worker(username, "s3cret-master-key")
        try:
            assert tid in _entries_for(username), "seed failed"

            cleared = clear_user_credentials(username)

            assert cleared == 1
            assert _entries_for(username) == [], (
                "plaintext password survived logout on a live worker thread"
            )
        finally:
            _seed_credentials_on_worker.release.set()

    def test_other_users_credentials_are_untouched(self):
        """A logout must not evict a concurrently-logged-in user."""
        victim = "logout_creds_bystander"
        leaver = "logout_creds_leaver"
        v_tid = _seed_credentials_on_worker(victim, "bystander-key")
        v_release = _seed_credentials_on_worker.release
        _seed_credentials_on_worker(leaver, "leaver-key")
        l_release = _seed_credentials_on_worker.release
        try:
            clear_user_credentials(leaver)

            assert _entries_for(leaver) == []
            assert v_tid in _entries_for(victim), (
                "clearing one user's credentials evicted another's"
            )
        finally:
            v_release.set()
            l_release.set()

    def test_clearing_an_unknown_user_is_a_no_op(self):
        assert clear_user_credentials("nobody_here_at_all") == 0

    def test_returns_count_across_multiple_threads(self):
        """One user can hold entries on several pooled workers at once."""
        username = "logout_creds_multi"
        releases = []
        for _ in range(3):
            _seed_credentials_on_worker(username, "multi-key")
            releases.append(_seed_credentials_on_worker.release)
        try:
            assert len(_entries_for(username)) == 3
            assert clear_user_credentials(username) == 3
            assert _entries_for(username) == []
        finally:
            for r in releases:
                r.set()