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.

12.5 KB
Raw Blame
"""``close_all_databases`` racing concurrent opens.

Incident context (PR #5596): the trust model of the connection cache is
"an entry exists only together with its verifier, and only while a
credential-verified open put it there". ``close_all_databases`` is the
shutdown-scale eviction: it clears ``connections`` and
``_password_verifiers`` wholesale, all under ``_connections_lock``
(``_init_locks`` is deliberately retained, like
``_rekey_locks``, so an in-flight open keeps its lock). Racing
it against open/close churn must never tear that model:

* O1 single user: a churn thread cycling open(correct)+close while a
  teardown thread repeatedly calls close_all_databases. At every sampled
  instant (under ``_connections_lock``) the engine/verifier PAIRING
  invariant holds; after both threads join and a final close_all, the
  cache is empty; the next open yields a fresh working engine that reads
  the canary (no engine handle reachable from cached state survived).
* O2 multi-user: three users churned round-robin against the same
  teardown; after stabilization every user reopens and reads its OWN
  canary (no cross-user bleed), and the pairing invariant held throughout.

Note on engine handles: per round 3's session-lifetime finding, an Engine
OBJECT handed out before a close_all can keep reconnecting via its
creator closure (by design; secured by the verifier on the next
credential-taking open). What close_all guarantees -- and what is pinned
here -- is the CACHE-level property: nothing remains reachable from
``connections``/``_password_verifiers``, and the next open publishes a
fresh, working engine.
"""

import threading
import time
import uuid

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager

# Module-level gate. Without a working SQLCipher build every test below
# is a no-op, and the dozens of in-body ``pytest.skip`` calls let a CI
# lane report this whole battery green having executed nothing. Skipping
# at import time makes the missing dependency one visible, uniform signal
# per module instead.
pytest.importorskip("sqlcipher3", reason="requires SQLCipher (encrypted mode)")

CHURN_ITERATIONS = 12
TEARDOWN_ITERATIONS = 6
# Cap on the extra teardown passes taken while the churn thread is still
# running. The fixed TEARDOWN_ITERATIONS at 0.02s is only ~0.12s against
# 12 real SQLCipher cold opens, so without this the teardown thread
# routinely finishes first and the race is never actually raced.
TEARDOWN_MAX_ITERATIONS = 200
JOIN_TIMEOUT_S = 60
BARRIER_TIMEOUT_S = 30
# Backoff before the single retry of a legitimate open. A transient
# SQLITE_BUSY under close_all contention is not the property under test,
# and reddening on it turns a loaded runner into a security-gate failure.
RETRY_BACKOFF_S = 0.1
# Sanity floor, not the race witness (that's ``overlaps >= 1`` in
# ``_run_and_assert_raced``): the teardown thread must have completed at
# least this many close_all passes, otherwise it never ran at all.
MIN_TEARDOWN_PASSES = 1


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory.

    Same self-contained pattern as the round-1/2 files, with the sanctioned
    test-mode KDF knob (each churn iteration cold-opens through PBKDF2).
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    yield mgr
    mgr.close_all_databases()


def _sample_pairing(manager, violations, label):
    with manager._connections_lock:
        engines = set(manager.connections)
        verifiers = set(manager._password_verifiers)
    if engines != verifiers:
        violations.append(f"{label}: engines={engines} verifiers={verifiers}")


def _run_churn_vs_teardown(manager, usernames, password_for):
    """Shared driver: churn thread opens/closes users round-robin; teardown
    thread hammers close_all_databases until the churn finishes.

    Returns ``(errors, pairing_violations, open_failures, overlaps,
    teardown_passes)``. ``overlaps`` counts the teardown passes that ran
    while the churn thread was demonstrably still working -- without it the
    two threads can run end to end without ever meeting and the test passes
    having raced nothing. ``teardown_passes`` is the total, used as the
    minimum-progress witness.
    """
    barrier = threading.Barrier(2)
    errors = {}
    pairing_violations = []
    open_failures = []
    churn_done = threading.Event()
    overlaps = 0
    teardown_passes = 0

    def churn():
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            for i in range(CHURN_ITERATIONS):
                username = usernames[i % len(usernames)]
                engine = manager.open_user_database(
                    username, password_for[username]
                )
                if engine is None:
                    # Retry once (see RETRY_BACKOFF_S): absorb a transient
                    # busy-database under contention, keep a repeatable
                    # refusal red.
                    time.sleep(RETRY_BACKOFF_S)
                    engine = manager.open_user_database(
                        username, password_for[username]
                    )
                if engine is None:
                    open_failures.append(f"iteration {i}: {username} (twice)")
                    continue
                manager.close_user_database(username)
                _sample_pairing(manager, pairing_violations, f"churn#{i}")
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors["churn"] = exc
        finally:
            churn_done.set()

    def teardown():
        nonlocal overlaps, teardown_passes
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            for i in range(TEARDOWN_MAX_ITERATIONS):
                still_churning = not churn_done.is_set()
                manager.close_all_databases()
                teardown_passes += 1
                _sample_pairing(manager, pairing_violations, f"teardown#{i}")
                if still_churning:
                    overlaps += 1
                if i + 1 >= TEARDOWN_ITERATIONS and churn_done.is_set():
                    break
                time.sleep(0.02)
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors["teardown"] = exc

    threads = [
        threading.Thread(target=churn, name="gap-o-churn"),
        threading.Thread(target=teardown, name="gap-o-teardown"),
    ]
    for t in threads:
        t.start()
    for t in threads:
        t.join(timeout=JOIN_TIMEOUT_S)
    assert not any(t.is_alive() for t in threads), "churn/teardown deadlocked"
    return (
        errors,
        pairing_violations,
        open_failures,
        overlaps,
        teardown_passes,
    )


def _run_and_assert_raced(manager, usernames, password_for, max_attempts=5):
    """Run the churn/teardown race, retrying the racing phase (never the
    correctness assertions) until an overlap is actually witnessed, then
    binds ``overlaps >= 1``.

    A pass with zero interleaving proves nothing about the race -- so
    unlike a plain single-shot assertion, "we didn't overlap" gets a
    bounded number of fresh attempts (each a full barrier-synchronized
    churn/teardown run) before this fails. This used to be a
    ``pytest.skip``, which was worse than useless here: in the CI summary
    a skip from a loaded runner is indistinguishable from "no SQLCipher
    build", so the lane reported green having raced nothing and nobody
    could tell. Now a run that never overlaps is a hard failure, not a
    warning that survives unexamined.

    Every attempt's correctness invariants (errors, pairing, opens) are
    checked immediately and unconditionally: a real invariant violation
    fails the test on the attempt where it happened -- it is never
    absorbed by a later retry, because only the "did we overlap" outcome
    is retried.
    """
    for attempt in range(1, max_attempts + 1):
        (
            errors,
            pairing_violations,
            open_failures,
            overlaps,
            teardown_passes,
        ) = _run_churn_vs_teardown(manager, usernames, password_for)
        assert not errors, (
            f"a racing thread raised (attempt {attempt}): {errors}"
        )
        assert not open_failures, (
            f"legitimate opens failed during close_all churn "
            f"(attempt {attempt}): {open_failures}"
        )
        assert not pairing_violations, (
            f"engine/verifier pairing broke (attempt {attempt}): "
            f"{pairing_violations}"
        )
        # Sanity check only, not the race witness: a wedged runner that
        # never even started the teardown thread would fail here first.
        assert teardown_passes >= MIN_TEARDOWN_PASSES, (
            f"the teardown thread completed {teardown_passes} close_all "
            f"passes (minimum {MIN_TEARDOWN_PASSES}, attempt {attempt}) -- "
            "the race driver did not run"
        )
        if overlaps >= 1:
            return
    pytest.fail(
        f"close_all race not witnessed after {max_attempts} attempts: the "
        "teardown thread never ran while the churn thread was still "
        "active on any attempt, so the pairing invariant above was "
        "checked but nothing was actually raced"
    )


def test_close_all_racing_single_user_open_close_churn(manager):
    """O1: one user's open/close churn vs repeated close_all_databases."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"churnone_{uuid.uuid4().hex[:8]}"
    password = "ChurnOnePassword1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_o_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_o_canary (id, note) VALUES (1, :note)"),
            {"note": "gap-O1"},
        )
    manager.close_user_database(username)

    _run_and_assert_raced(manager, [username], {username: password})

    # Stabilize: final close_all empties every map.
    manager.close_all_databases()
    with manager._connections_lock:
        assert manager.connections == {}
        assert manager._password_verifiers == {}

    # The next open publishes a fresh working engine with the canary.
    fresh = manager.open_user_database(username, password)
    assert fresh is not None
    with fresh.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_o_canary WHERE id = 1")
            ).scalar()
            == "gap-O1"
        )
    assert manager.connections[username] is fresh


def test_close_all_racing_multi_user_churn(manager):
    """O2: three users churned round-robin vs the same teardown; every user
    reopens afterwards and reads its OWN canary (no cross-user bleed)."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    users = []
    for name, password, note in [
        ("churnu1", "ChurnUserPw1!", "gap-O2-one"),
        ("churnu2", "ChurnUserPw2!", "gap-O2-two"),
        ("churnu3", "ChurnUserPw3!", "gap-O2-three"),
    ]:
        username = f"{name}_{uuid.uuid4().hex[:8]}"
        engine = manager.create_user_database(username, password)
        with engine.begin() as conn:
            conn.execute(
                text(
                    "CREATE TABLE gap_o_canary "
                    "(id INTEGER PRIMARY KEY, note TEXT)"
                )
            )
            conn.execute(
                text("INSERT INTO gap_o_canary (id, note) VALUES (1, :note)"),
                {"note": note},
            )
        users.append((username, password, note))
    manager.close_all_databases()

    usernames = [u for u, _, _ in users]
    password_for = {u: p for u, p, _ in users}
    _run_and_assert_raced(manager, usernames, password_for)

    manager.close_all_databases()
    with manager._connections_lock:
        assert manager.connections == {}
        assert manager._password_verifiers == {}

    for username, password, note in users:
        fresh = manager.open_user_database(username, password)
        assert fresh is not None, f"{username} failed to reopen after churn"
        with fresh.connect() as conn:
            assert (
                conn.execute(
                    text("SELECT note FROM gap_o_canary WHERE id = 1")
                ).scalar()
                == note
            ), f"{username} read the wrong user's canary"