~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test(db): cover rekey worker crashes (#7653)
LearningCircuitc12a467d27e3
diff --git a/tests/database/test_rekey_process_crash_recovery.py b/tests/database/test_rekey_process_crash_recovery.pynew file mode 100644index 0000000000..57a8417e20--- /dev/null+++ b/tests/database/test_rekey_process_crash_recovery.py@@ -0,0 +1,238 @@+"""Recover real encrypted databases after a rekey process exits uncleanly."""++from __future__ import annotations++import multiprocessing+import os+from contextlib import closing+from multiprocessing.synchronize import Event+from pathlib import Path+from typing import Literal+from unittest.mock import patch++import pytest+from sqlalchemy import text++from local_deep_research.database import encrypted_db+from local_deep_research.database.encrypted_db import DatabaseManager+from local_deep_research.database.rekey_lock import (+ RekeyEpochState,+ RekeyLockContentionError,+ acquire_rekey_lock,+ read_rekey_epoch_state,+)++pytest.importorskip("sqlcipher3", reason="requires real SQLCipher encryption")++type _CrashPoint = Literal["pending", "verified", "committed"]++_EXIT_CODES: dict[_CrashPoint, int] = {+ "pending": 73,+ "verified": 74,+ "committed": 75,+}+_OLD_PASSWORD = "OldCrashRecoveryHorse1!" # noqa: S105+_NEW_PASSWORD = "NewCrashRecoveryHorse2!" # noqa: S105+_CANARY = "synthetic encrypted rekey crash canary"+_WAIT_SECONDS = 30+++def _crash_during_rekey(+ data_dir: str,+ username: str,+ crash_point: _CrashPoint,+ ready: Event,+ exit_now: Event,+) -> None:+ """Run the actual rekey, bypassing all finally blocks at one boundary."""+ manager = DatabaseManager()+ manager.data_dir = Path(data_dir)+ real_begin = encrypted_db.begin_rekey_epoch+ real_commit = encrypted_db.commit_rekey_epoch++ def exit_without_cleanup() -> None:+ ready.set()+ if not exit_now.wait(timeout=_WAIT_SECONDS):+ raise AssertionError("parent did not authorize the crash")+ os._exit(_EXIT_CODES[crash_point])++ def begin_then_crash(db_path: Path) -> str:+ candidate = real_begin(db_path)+ if crash_point == "pending":+ exit_without_cleanup()+ return candidate++ def commit_then_crash(db_path: Path, candidate: str) -> None:+ if crash_point == "verified":+ exit_without_cleanup()+ real_commit(db_path, candidate)+ if crash_point == "committed":+ exit_without_cleanup()++ with (+ patch.object(encrypted_db, "begin_rekey_epoch", begin_then_crash),+ patch.object(encrypted_db, "commit_rekey_epoch", commit_then_crash),+ ):+ manager.change_password(username, _OLD_PASSWORD, _NEW_PASSWORD)+ raise AssertionError("rekey returned without reaching its crash point")+++def _stop_process(process: multiprocessing.Process) -> None:+ """A broken native call must not leave a child behind after an assertion."""+ if process.is_alive():+ process.terminate()+ process.join(timeout=5)+ if process.is_alive():+ process.kill()+ process.join(timeout=5)+ assert not process.is_alive(), "rekey worker survived forced cleanup"+ process.close()++[email protected]("crash_point", ["pending", "verified", "committed"])+def test_process_crash_recovers_only_the_surviving_password(+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+ crash_point: _CrashPoint,+) -> None:+ monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))+ monkeypatch.setenv("LDR_TEST_MODE", "true")+ monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")+ username = "rekey_crash_user"+ setup_manager = DatabaseManager()+ setup_manager.data_dir = tmp_path+ if not setup_manager.has_encryption:+ pytest.skip("requires real SQLCipher encryption")+ try:+ engine = setup_manager.create_user_database(username, _OLD_PASSWORD)+ assert engine is not None+ with engine.begin() as connection:+ connection.execute(text("CREATE TABLE crash_canary (note TEXT)"))+ connection.execute(+ text("INSERT INTO crash_canary (note) VALUES (:note)"),+ {"note": _CANARY},+ )+ db_path = setup_manager._get_user_db_path(username)+ finally:+ setup_manager.close_all_databases()++ # The fixture is an encrypted, checkpointed database, not a plaintext+ # stand-in or an in-memory engine that would disappear with its creator.+ database_bytes = db_path.read_bytes()+ assert database_bytes[:16] != b"SQLite format 3\x00"+ assert _CANARY.encode() not in database_bytes+ initial_state = read_rekey_epoch_state(db_path)+ assert initial_state.committed_epoch is not None+ assert initial_state.pending_epoch is None+ lock_path = db_path.with_name(f"{db_path.name}.rekey.lock")+ original_lock_stat = lock_path.stat()++ context = multiprocessing.get_context("spawn")+ ready = context.Event()+ exit_now = context.Event()+ worker = context.Process(+ target=_crash_during_rekey,+ args=(str(tmp_path), username, crash_point, ready, exit_now),+ )+ worker.start()+ try:+ assert ready.wait(timeout=_WAIT_SECONDS), (+ "rekey never reached its boundary"+ )+ assert worker.is_alive()+ interrupted_state = read_rekey_epoch_state(db_path)+ candidate = interrupted_state.pending_epoch+ assert candidate is not None+ assert candidate != initial_state.committed_epoch+ assert interrupted_state.committed_epoch == (+ candidate+ if crash_point == "committed"+ else initial_state.committed_epoch+ )++ # Prove that this is a live OS lock, then exit without a context+ # manager unwind. Restart must use that same persistent sidecar.+ with pytest.raises(RekeyLockContentionError):+ with acquire_rekey_lock(db_path):+ pytest.fail("parent acquired the live child's rekey lock")+ exit_now.set()+ worker.join(timeout=_WAIT_SECONDS)+ assert not worker.is_alive(), "rekey worker did not exit"+ assert worker.exitcode == _EXIT_CODES[crash_point]+ finally:+ exit_now.set()+ _stop_process(worker)++ with acquire_rekey_lock(db_path):+ assert read_rekey_epoch_state(db_path) == interrupted_state+ if os.name != "nt":+ current_lock_stat = lock_path.stat()+ assert (current_lock_stat.st_dev, current_lock_stat.st_ino) == (+ original_lock_stat.st_dev,+ original_lock_stat.st_ino,+ )++ surviving_password = (+ _OLD_PASSWORD if crash_point == "pending" else _NEW_PASSWORD+ )+ rejected_password = (+ _NEW_PASSWORD if crash_point == "pending" else _OLD_PASSWORD+ )+ epoch_paths = [+ db_path.with_name(f"{db_path.name}{suffix}")+ for suffix in (".rekey.epoch", ".rekey.pending")+ ]+ before_wrong_password = {path: path.read_bytes() for path in epoch_paths}+ recovery_manager = DatabaseManager()+ recovery_manager.data_dir = tmp_path+ try:+ assert (+ recovery_manager.open_user_database(username, rejected_password)+ is None+ )+ assert {+ path: path.read_bytes() for path in epoch_paths+ } == before_wrong_password+ assert username not in recovery_manager.connections+ assert username not in recovery_manager._password_verifiers+ assert username not in recovery_manager._connection_epochs++ recovered_engine = recovery_manager.open_user_database(+ username, surviving_password+ )+ assert recovered_engine is not None+ with recovered_engine.connect() as connection:+ assert connection.execute(+ text("SELECT note FROM crash_canary")+ ).all() == [(_CANARY,)]+ assert read_rekey_epoch_state(db_path) == RekeyEpochState(+ candidate, None+ )+ finally:+ recovery_manager.close_all_databases()++ # A new raw connection must decrypt the actual canary after recovery;+ # neither a cached engine nor SELECT 1 would prove the surviving key.+ fresh_connection = DatabaseManager._make_sqlcipher_connection(+ db_path, surviving_password+ )+ try:+ assert fresh_connection.execute(+ "SELECT note FROM crash_canary"+ ).fetchall() == [(_CANARY,)]+ finally:+ fresh_connection.close()+ with pytest.raises(ValueError):+ with closing(+ DatabaseManager._make_sqlcipher_connection(+ db_path, rejected_password+ )+ ):+ pass+ assert lock_path.exists()+ if os.name != "nt":+ final_lock_stat = lock_path.stat()+ assert (final_lock_stat.st_dev, final_lock_stat.st_ino) == (+ original_lock_stat.st_dev,+ original_lock_stat.st_ino,+ )