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.

8.7 KB
Raw Blame
"""Racing double-rekey: two concurrent change_password calls, one DB.

Incident context (PR #5596): ``change_password`` originally held NO lock
across the rekey, because it is built from the public pieces -- close,
open(old), PRAGMA rekey, evict. Two threads calling
``change_password(user, old, new_A)`` and ``change_password(user, old,
new_B)`` at a barrier therefore interleaved freely, and CI showed both
reporting success. A per-user lock now serialises the two calls; which
one wins is still scheduler-dependent. What must NOT be
scheduler-dependent is the existential shape afterwards:

* the file ends up keyed by exactly ONE of {old, new_A, new_B} -- a file
  corrupted by interleaved rekeys would leave NO password working;
* the winning password opens, and its engine reads the canary written
  before the race (data survival);
* the losing passwords fail cleanly (None), never a broken engine;
* after a final close_all_databases the manager holds nothing, and the
  engine/verifier pairing invariant holds at every sampled instant.

Round 1's test_db_connection_change_password_race.py pinned the
old-vs-rekey race (hammer threads READ while one rekey chain runs). This
file is the two-WRITERS duel: both threads rekey. It is parametrised over
a single run id: the identifier is kept so the credentials stay unique
per run and a maintainer chasing a flake can widen the range locally, but
repeating a byte-identical duel in CI buys no schedule diversity.
"""

import threading
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)")

JOIN_TIMEOUT_S = 60
BARRIER_TIMEOUT_S = 30


@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 duel performs several derivations/rekeys).
    """
    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()


# One run, not five. The five parametrised repetitions were
# byte-identical -- same users, same passwords, same barrier -- so
# they added no coverage while costing five creates and ten full
# SQLCipher rekeys per CI run. Genuine schedule diversity would need
# different interleavings, not the same duel repeated.
@pytest.mark.parametrize("run_id", range(1))
def test_double_rekey_duel_existential_invariants(manager, run_id):
    """Two barrier-synchronized change_password races: old->new_A vs
    old->new_B. Afterwards exactly one password owns the file, the winner
    reads the pre-race canary, the losers fail cleanly, and the manager
    quiesces to an empty, pairing-consistent cache."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"duel_{uuid.uuid4().hex[:8]}"
    old = f"DuelOld-{run_id}-CorrectHorse1!"  # noqa: S105
    new_a = f"DuelNewA-{run_id}-CorrectHorse2!"  # noqa: S105
    new_b = f"DuelNewB-{run_id}-CorrectHorse3!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_m_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_m_canary (id, note) VALUES (1, :note)"),
            {"note": "gap-M"},
        )
    manager.close_user_database(username)

    barrier = threading.Barrier(2)
    errors = {}
    outcomes = {}
    contention = {}
    pairing_violations = []

    from local_deep_research.database.rekey_lock import (
        RekeyLockContentionError,
    )

    def dueler(label, new_password):
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            outcomes[label] = manager.change_password(
                username, old, new_password
            )
            with manager._connections_lock:
                engines = set(manager.connections)
                verifiers = set(manager._password_verifiers)
            if engines != verifiers:
                pairing_violations.append(f"{label}: {engines} vs {verifiers}")
        except RekeyLockContentionError as exc:
            # The serialized-rekey contract: the loser is told loudly that
            # another rekey lifecycle owns the lock (the web layer maps
            # this to 409). It is a classified outcome, not an error --
            # the pre-serialization duel could only distinguish loser by
            # open(old) failing after the fact.
            contention[label] = exc
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors[label] = exc

    threads = [
        threading.Thread(target=dueler, args=("A", new_a), name="gap-m-a"),
        threading.Thread(target=dueler, args=("B", new_b), name="gap-m-b"),
    ]
    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), "rekey duel deadlocked"
    assert not errors, f"a dueling thread raised: {errors}"
    assert not pairing_violations, (
        f"engine/verifier pairing broke mid-duel: {pairing_violations}"
    )
    # Race witness: both duelers must have recorded a classified outcome
    # (a boolean from change_password, or the typed contention error the
    # serialized-rekey contract prescribes for the loser).
    assert set(outcomes) | set(contention) == {"A", "B"}, (
        f"a dueling thread recorded no outcome: outcomes={outcomes} "
        f"contention={contention} -- the duel did not actually run"
    )
    reported_true = {label for label, changed in outcomes.items() if changed}
    assert len(reported_true) <= 1, (
        f"both change_password calls reported success ({outcomes}) -- at "
        "most one rekey can own the file"
    )

    # Quiesce: no cached state survives a close_all_databases.
    manager.close_all_databases()
    with manager._connections_lock:
        assert manager.connections == {}
        assert manager._password_verifiers == {}

    # Existential probe: exactly one of the three passwords opens the file.
    winners = []
    for label, pw in (("old", old), ("new_a", new_a), ("new_b", new_b)):
        if manager.open_user_database(username, pw) is not None:
            winners.append((label, pw))
    assert winners, (
        "no password can open the database after the duel -- the racing "
        "rekeys corrupted the file"
    )
    assert len(winners) == 1, (
        f"multiple passwords open the file after the duel: "
        f"{[label for label, _ in winners]} -- the file must end up keyed "
        "by exactly one of them"
    )

    # Outcome distribution, cross-checked against who actually owns the
    # file: a dueler that reported success MUST be the owner. (The reverse
    # is not asserted -- a rekey can land and still return False if the
    # eviction that follows it raises -- so this stays one-directional.)
    owner_label = {"new_a": "A", "new_b": "B"}.get(winners[0][0])
    expected_true = {owner_label} if owner_label else set()
    assert reported_true <= expected_true, (
        f"change_password returned True for {reported_true} but the file is "
        f"keyed by {winners[0][0]!r} -- a reported success must own the file"
    )

    # The winner reads the canary written before the race.
    winning_label, winning_pw = winners[0]
    engine = manager.open_user_database(username, winning_pw)
    assert engine is not None
    with engine.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_m_canary WHERE id = 1")
            ).scalar()
            == "gap-M"
        ), "the winning password's engine cannot read pre-race data"
    assert manager._password_matches_cached(username, winning_pw) is True

    # The losers fail cleanly -- None, never a broken engine -- and leave
    # the winner's cache entry undisturbed.
    for label, pw in (("old", old), ("new_a", new_a), ("new_b", new_b)):
        if label == winning_label:
            continue
        assert manager.open_user_database(username, pw) is None, (
            f"losing password {label!r} still opens the file"
        )
        assert manager.connections[username] is engine, (
            f"a losing attempt ({label!r}) disturbed the winner's engine"
        )