~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
from __future__ import annotations
import ast
import errno
import multiprocessing
import os
import stat
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Final
from unittest.mock import MagicMock, call, patch
import pytest
from local_deep_research.database import encrypted_db
from local_deep_research.database import rekey_lock as rekey_lock_module
from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.rekey_lock import (
RekeyEpochState,
RekeyEpochUnreadableError,
RekeyIndeterminateError,
RekeyLockContentionError,
RekeyRecoveryRequiredError,
RekeySidecarIOError,
RekeySidecarOperation,
RekeyStage,
RekeyUnavailableError,
acquire_rekey_lock,
begin_rekey_epoch,
commit_rekey_epoch,
read_rekey_epoch_state,
)
def _new_manager(data_dir: Path) -> DatabaseManager:
with patch.object(
DatabaseManager, "_check_encryption_available", return_value=True
):
manager = DatabaseManager()
manager.data_dir = data_dir
return manager
@pytest.fixture
def manager(tmp_path: Path) -> DatabaseManager:
return _new_manager(tmp_path)
def _existing_db(manager: DatabaseManager, username: str) -> Path:
db_path = manager._get_user_db_path(username)
db_path.touch()
return db_path
def _initialize_epoch(db_path: Path) -> str:
with acquire_rekey_lock(db_path):
return rekey_lock_module.read_or_initialize_rekey_epoch(db_path)
type _QueryRow = tuple[int, ...] | tuple[str, ...]
@dataclass(frozen=True, slots=True)
class _RawVerification:
synchronous: int = 2
quick_rows: tuple[_QueryRow, ...] = (("ok",),)
cipher_rows: tuple[_QueryRow, ...] = ()
checkpoint: tuple[int, int, int] = (0, 0, 0)
schema_readable: bool = True
_SUCCESSFUL_RAW: Final = _RawVerification()
def _query_result(
*,
row: _QueryRow | None = None,
rows: tuple[_QueryRow, ...] = (),
) -> MagicMock:
result = MagicMock()
result.fetchone.return_value = row
result.__iter__.return_value = iter(rows)
return result
def _raw_connection(
verification: _RawVerification = _SUCCESSFUL_RAW,
*,
events: list[str] | None = None,
label: str = "raw",
) -> MagicMock:
connection = MagicMock()
def execute(statement: str) -> MagicMock:
if events is not None:
events.append(f"{label}:{statement}")
if statement == "PRAGMA synchronous = FULL":
return _query_result()
if statement == "PRAGMA synchronous":
return _query_result(row=(verification.synchronous,))
if statement == "SELECT count(*) FROM sqlite_master":
if not verification.schema_readable:
raise RuntimeError("file is not a database")
return _query_result(row=(1,))
if statement == "PRAGMA quick_check":
return _query_result(rows=verification.quick_rows)
if statement == "PRAGMA cipher_integrity_check":
return _query_result(rows=verification.cipher_rows)
if statement == "PRAGMA wal_checkpoint(TRUNCATE)":
return _query_result(row=verification.checkpoint)
if statement == "PRAGMA wal_checkpoint(PASSIVE)":
return _query_result(row=verification.checkpoint)
if statement.startswith("PRAGMA busy_timeout = "):
return _query_result()
raise AssertionError(f"unexpected raw SQL: {statement}")
connection.execute.side_effect = execute
return connection
def test_same_user_contention_raises_before_database_work(
manager: DatabaseManager,
) -> None:
# Given one same-user rekey paused on its raw verified connection
db_path = _existing_db(manager, "alice")
_initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(label="new")
rekey_started = threading.Event()
release_rekey = threading.Event()
def controlled_rekey(connection, _password, *, db_path) -> None:
assert connection is old_connection
rekey_started.set()
assert release_rekey.wait(timeout=5)
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
) as make_raw,
patch.object(manager, "open_user_database") as cached_open,
patch(
"local_deep_research.database.encrypted_db.set_sqlcipher_rekey",
side_effect=controlled_rekey,
) as rekey,
ThreadPoolExecutor(max_workers=1) as executor,
):
winner = executor.submit(
manager.change_password, "alice", "old-password", "new-password"
)
assert rekey_started.wait(timeout=5)
try:
# When a second request for Alice arrives during the rekey
with pytest.raises(RekeyLockContentionError):
manager.change_password(
"alice", "other-old-password", "other-new-password"
)
finally:
release_rekey.set()
# Then only the winner reaches raw verification and rekey
assert winner.result(timeout=5) is True
assert make_raw.call_args_list == [
call(db_path, "old-password"),
call(db_path, "new-password"),
]
cached_open.assert_not_called()
rekey.assert_called_once_with(
old_connection, "new-password", db_path=db_path
)
old_connection.close.assert_called_once_with()
new_connection.close.assert_called_once_with()
def test_in_process_sidecar_holder_contention_raises_before_database_work(
manager: DatabaseManager,
) -> None:
# Given this thread already holds Alice's sidecar lock, so the
# change_password below loses on the in-process gate after its bounded
# local wait (the cross-process flock path is pinned separately by
# test_cross_process_flock_holder_is_typed_contention_not_a_hang)
db_path = _existing_db(manager, "alice")
_initialize_epoch(db_path)
with (
patch.object(manager, "_make_sqlcipher_connection") as make_raw,
acquire_rekey_lock(db_path),
):
# When this manager attempts Alice's password change
with pytest.raises(RekeyLockContentionError):
manager.change_password("alice", "old-password", "new-password")
# Then it never touches the shared database lifecycle
make_raw.assert_not_called()
def test_rekey_locks_remain_held_through_raw_connection_close(
manager: DatabaseManager,
) -> None:
# Given a successful rekey paused while closing its raw connection
db_path = _existing_db(manager, "alice")
_initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(label="new")
close_started = threading.Event()
release_close = threading.Event()
def controlled_close() -> None:
close_started.set()
assert release_close.wait(timeout=5)
old_connection.close.side_effect = controlled_close
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch("local_deep_research.database.encrypted_db.set_sqlcipher_rekey"),
ThreadPoolExecutor(max_workers=1) as executor,
):
winner = executor.submit(
manager.change_password, "alice", "old-password", "new-password"
)
assert close_started.wait(timeout=5)
try:
# When another request arrives during final raw cleanup
with pytest.raises(RekeyLockContentionError):
manager.change_password(
"alice", "other-old-password", "other-new-password"
)
finally:
release_close.set()
# Then cleanup completes before either serialization lock is released
assert winner.result(timeout=5) is True
def test_different_users_rekey_independently(manager: DatabaseManager) -> None:
# Given Alice's raw rekey is paused while Bob has a separate database
alice_path = _existing_db(manager, "alice")
bob_path = _existing_db(manager, "bob")
_initialize_epoch(alice_path)
_initialize_epoch(bob_path)
alice_old_connection = _raw_connection(label="alice-old")
alice_new_connection = _raw_connection(label="alice-new")
bob_old_connection = _raw_connection(label="bob-old")
bob_new_connection = _raw_connection(label="bob-new")
alice_started = threading.Event()
release_alice = threading.Event()
bob_rekeyed = threading.Event()
def make_raw(_db_path: Path, password: str) -> MagicMock:
return {
"alice-old": alice_old_connection,
"alice-new": alice_new_connection,
"bob-old": bob_old_connection,
"bob-new": bob_new_connection,
}[password]
def controlled_rekey(connection, _password, *, db_path) -> None:
if connection is alice_old_connection:
alice_started.set()
assert release_alice.wait(timeout=5)
else:
assert connection is bob_old_connection
bob_rekeyed.set()
with (
patch.object(
manager, "_make_sqlcipher_connection", side_effect=make_raw
),
patch(
"local_deep_research.database.encrypted_db.set_sqlcipher_rekey",
side_effect=controlled_rekey,
),
ThreadPoolExecutor(max_workers=2) as executor,
):
alice = executor.submit(
manager.change_password, "alice", "alice-old", "alice-new"
)
assert alice_started.wait(timeout=5)
try:
# When Bob changes his password while Alice owns Alice's locks
bob = executor.submit(
manager.change_password, "bob", "bob-old", "bob-new"
)
# Then Bob completes without waiting for Alice
assert bob.result(timeout=5) is True
assert bob_rekeyed.is_set()
assert not alice.done()
finally:
release_alice.set()
assert alice.result(timeout=5) is True
def test_wrong_old_password_does_not_advance_epoch(
manager: DatabaseManager,
) -> None:
# Given raw SQLCipher verification rejects the supplied old password
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=ValueError("invalid database key"),
) as make_raw,
patch.object(manager, "open_user_database") as cached_open,
patch(
"local_deep_research.database.encrypted_db.set_sqlcipher_rekey"
) as rekey,
):
# When Alice attempts a password change
result = manager.change_password(
"alice", "incorrect-password", "new-password"
)
# Then verification stays uncached and the durable epoch remains unchanged
assert result is False
make_raw.assert_called_once_with(db_path, "incorrect-password")
cached_open.assert_not_called()
rekey.assert_not_called()
assert rekey_lock_module.read_rekey_epoch(db_path) == original_epoch
def test_encryption_unavailable_returns_false(
manager: DatabaseManager,
) -> None:
# Given this process runs in the documented unencrypted fallback mode
manager.has_encryption = False
# When Alice requests a password change
changed = manager.change_password("alice", "old-password", "new-password")
# Then the documented boolean contract holds: unencrypted mode is a
# static deployment mode (nothing to rekey), not an operational
# failure, so it answers False with a warning rather than a typed 503
# (tests/security/test_db_connection_unencrypted_mode.py pins the same).
assert changed is False
def test_missing_database_is_typed_unavailable(
manager: DatabaseManager,
) -> None:
# Given Alice has no database file
db_path = manager._get_user_db_path("alice")
# When Alice requests a password change
with pytest.raises(RekeyUnavailableError) as raised:
manager.change_password("alice", "old-password", "new-password")
# Then absence is operationally distinct from old-password rejection
assert raised.value.db_path == db_path
assert raised.value.stage is RekeyStage.PREPARATION
def test_pre_pending_checkpoint_failure_is_typed_unavailable(
manager: DatabaseManager,
) -> None:
# Given the old key opens but its pre-rekey checkpoint fails
db_path = _existing_db(manager, "alice")
_initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
nested_message = "old-password checkpoint detail"
with (
patch.object(
manager,
"_make_sqlcipher_connection",
return_value=old_connection,
),
patch.object(
manager,
"_pre_rekey_checkpoint",
side_effect=OSError(nested_message),
),
):
# When preparation cannot establish a durable checkpoint
with pytest.raises(RekeyUnavailableError) as raised:
manager.change_password("alice", "old-password", "new-password")
# Then the typed error contains no nested SQLCipher or password text
assert raised.value.db_path == db_path
assert raised.value.stage is RekeyStage.PREPARATION
assert nested_message not in str(raised.value)
old_connection.close.assert_called_once_with()
def test_cached_engine_disposal_failure_is_typed_unavailable(
manager: DatabaseManager,
) -> None:
# Given preparation detaches a cached engine that cannot be disposed
db_path = _existing_db(manager, "alice")
epoch = _initialize_epoch(db_path)
engine = MagicMock()
engine.dispose.side_effect = OSError("old-password disposal detail")
manager._cache_connection(
"alice",
manager._make_connection_cache_entry(engine, "old-password", epoch),
)
# When Alice requests a password change
with pytest.raises(RekeyUnavailableError) as raised:
manager.change_password("alice", "old-password", "new-password")
# Then disposal is an operational preparation failure, not bad credentials
assert raised.value.db_path == db_path
assert raised.value.stage is RekeyStage.PREPARATION
assert "old-password" not in str(raised.value)
engine.dispose.assert_called_once_with()
def test_sidecar_io_failure_is_typed_unavailable(
manager: DatabaseManager,
) -> None:
# Given the database exists but its process sidecar cannot be opened
db_path = _existing_db(manager, "alice")
with patch.object(
encrypted_db,
"acquire_rekey_lock",
side_effect=OSError("old-password sidecar detail"),
):
# When Alice requests a password change
with pytest.raises(RekeyUnavailableError) as raised:
manager.change_password("alice", "old-password", "new-password")
# Then sidecar I/O is unavailable without carrying nested secret text
assert raised.value.db_path == db_path
assert raised.value.stage is RekeyStage.PREPARATION
assert "old-password" not in str(raised.value)
def test_pending_publication_failure_without_pending_is_unavailable(
manager: DatabaseManager,
) -> None:
# Given pending publication will fail before making its marker visible
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
with (
patch.object(
manager,
"_make_sqlcipher_connection",
return_value=old_connection,
),
patch.object(
encrypted_db,
"begin_rekey_epoch",
side_effect=OSError("pending publication detail"),
),
):
# When the failed operation is reconciled against stable E1
with pytest.raises(RekeyUnavailableError) as raised:
manager.change_password("alice", "old-password", "new-password")
# Then the operation is unavailable because no candidate became durable
assert raised.value.stage is RekeyStage.PENDING_PUBLICATION
assert read_rekey_epoch_state(db_path) == RekeyEpochState(
committed_epoch=original_epoch,
pending_epoch=None,
)
def test_pending_publication_failure_with_visible_pending_is_indeterminate(
manager: DatabaseManager,
) -> None:
# Given pending os.replace succeeds before its directory fsync fails
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
pending_path = db_path.with_name(f"{db_path.name}.rekey.pending")
old_connection = _raw_connection(label="old")
real_sync_parent = rekey_lock_module._sync_parent_directory
def fail_pending_directory_sync(path: Path) -> None:
if path == pending_path:
raise OSError("pending directory fsync detail")
real_sync_parent(path)
with (
patch.object(
manager,
"_make_sqlcipher_connection",
return_value=old_connection,
),
patch.object(
rekey_lock_module,
"_sync_parent_directory",
side_effect=fail_pending_directory_sync,
),
):
# When the pending publication wrapper reports failure
with pytest.raises(RekeyIndeterminateError) as raised:
manager.change_password("alice", "old-password", "new-password")
# Then its visible E1+pending-E2 state remains explicit for recovery
state = read_rekey_epoch_state(db_path)
assert state.committed_epoch == original_epoch
assert state.pending_epoch is not None
assert raised.value.stage is RekeyStage.PENDING_PUBLICATION
assert raised.value.state == state
def test_rekey_publishes_after_fresh_candidate_verification(
manager: DatabaseManager,
) -> None:
# Given distinct old-key and new-key raw connections
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
events: list[str] = []
old_connection = _raw_connection(events=events, label="old")
new_connection = _raw_connection(events=events, label="new")
real_commit = commit_rekey_epoch
def make_raw(_db_path: Path, password: str) -> MagicMock:
events.append(f"open:{password}")
return old_connection if password == "old-password" else new_connection
def spy_rekey(connection, _password, *, db_path) -> None:
state = read_rekey_epoch_state(db_path)
assert state.committed_epoch == original_epoch
assert state.pending_epoch is not None
events.append("rekey")
def spy_commit(path: Path, candidate_epoch: str) -> None:
assert (
connection_events := [
event for event in events if event.startswith("new:")
]
)
assert connection_events == [
"new:PRAGMA synchronous = FULL",
"new:PRAGMA synchronous",
"new:SELECT count(*) FROM sqlite_master",
"new:PRAGMA quick_check",
"new:PRAGMA busy_timeout = "
f"{encrypted_db._gated_checkpoint_busy_timeout_ms()}",
"new:PRAGMA wal_checkpoint(TRUNCATE)",
"new:PRAGMA cipher_integrity_check",
]
assert events[-1] == "new:PRAGMA cipher_integrity_check"
events.append("commit")
real_commit(path, candidate_epoch)
with (
patch.object(
manager, "_make_sqlcipher_connection", side_effect=make_raw
) as make_raw,
patch.object(manager, "open_user_database") as cached_open,
patch(
"local_deep_research.database.encrypted_db.set_sqlcipher_rekey",
side_effect=spy_rekey,
) as rekey,
patch.object(
encrypted_db, "commit_rekey_epoch", side_effect=spy_commit
),
):
# When the password changes
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then only the verified candidate is published and pending metadata is cleared
assert result is True
assert make_raw.call_args_list == [
call(db_path, "old-password"),
call(db_path, "new-password"),
]
rekey.assert_called_once_with(
old_connection, "new-password", db_path=db_path
)
old_connection.close.assert_called_once_with()
new_connection.close.assert_called_once_with()
cached_open.assert_not_called()
state = read_rekey_epoch_state(db_path)
assert state.pending_epoch is None
assert state.committed_epoch != original_epoch
assert events.index("new:PRAGMA cipher_integrity_check") < events.index(
"commit"
)
def test_rekey_failure_preserves_committed_epoch_and_pending_candidate(
manager: DatabaseManager,
) -> None:
# Given a cached old-key engine and a rekey operation that will fail
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_engine = MagicMock()
entry = manager._make_connection_cache_entry(
old_engine, "old-password", original_epoch
)
manager._cache_connection("alice", entry)
old_connection = _raw_connection(label="old")
with (
patch.object(manager, "_checkpoint_wal") as checkpoint,
patch.object(
manager, "_make_sqlcipher_connection", return_value=old_connection
),
patch(
"local_deep_research.database.encrypted_db.set_sqlcipher_rekey",
side_effect=RuntimeError("simulated rekey failure"),
),
):
# When rekey fails after pending metadata is durable
with pytest.raises(RekeyIndeterminateError) as raised:
manager.change_password("alice", "old-password", "new-password")
# Then E1 remains committed, E2 remains pending, and no old cache survives
state = read_rekey_epoch_state(db_path)
assert raised.value.stage is RekeyStage.PHYSICAL_REKEY
assert raised.value.state == state
assert state.committed_epoch == original_epoch
assert state.pending_epoch is not None
assert state.pending_epoch != original_epoch
assert rekey_lock_module.read_rekey_epoch(db_path) is None
assert "alice" not in manager.connections
assert "alice" not in manager._password_verifiers
assert "alice" not in manager._connection_epochs
checkpoint.assert_not_called()
old_engine.dispose.assert_called_once_with()
old_connection.close.assert_called_once_with()
@pytest.mark.parametrize(
"verification",
[
_RawVerification(quick_rows=(("corrupt",),)),
_RawVerification(cipher_rows=(("hmac failure",),)),
],
ids=["quick-check", "cipher-integrity"],
)
def test_integrity_scan_findings_after_proven_key_publish(
manager: DatabaseManager,
verification: _RawVerification,
) -> None:
# Given a candidate whose key decrypts the schema but whose integrity
# scans report damage (a database that was already damaged fails them
# under whichever key is active)
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(verification, label="new")
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
patch.object(encrypted_db.logger, "warning") as warning,
):
# When the password change verifies the candidate
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then the proven key is published and the finding is logged loudly
assert result is True
state = read_rekey_epoch_state(db_path)
assert state.pending_epoch is None
assert state.committed_epoch != original_epoch
assert any(
"after the active key was proven" in str(call.args[0])
for call in warning.call_args_list
)
@pytest.mark.parametrize(
"verification",
[_RawVerification(synchronous=1)],
ids=["synchronous"],
)
def test_candidate_verification_failure_keeps_pending_state(
manager: DatabaseManager,
verification: _RawVerification,
) -> None:
# Given a rekey whose fresh candidate connection fails a strict durability check
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(
verification,
label="new",
)
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
):
# When the password change verifies the candidate
with pytest.raises(RekeyIndeterminateError) as raised:
manager.change_password("alice", "old-password", "new-password")
# Then publication is withheld and the pending generation fences all callers
state = read_rekey_epoch_state(db_path)
assert raised.value.stage is RekeyStage.CANDIDATE_VERIFICATION
assert raised.value.state == state
assert state.committed_epoch == original_epoch
assert state.pending_epoch is not None
assert rekey_lock_module.read_rekey_epoch(db_path) is None
def test_busy_checkpoint_after_proven_candidate_publishes(
manager: DatabaseManager,
) -> None:
# Given a candidate that passes the key proof (schema read)
# but whose post-rekey checkpoint is kept busy by a concurrent reader
# (moved out of the failure matrix above: a busy reader is not ambiguity)
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
events: list[str] = []
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(
_RawVerification(checkpoint=(1, 1, 0)), events=events, label="new"
)
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
):
# When the password change verifies the candidate
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then the verified generation is published and pending state cleared,
# without the cipher scan that stale old-key WAL frames would fail
assert result is True
state = read_rekey_epoch_state(db_path)
assert state.pending_epoch is None
assert state.committed_epoch != original_epoch
assert "new:PRAGMA cipher_integrity_check" not in events
def test_commit_failure_after_proven_key_is_success_with_pending_left(
manager: DatabaseManager,
) -> None:
# Given a verified candidate whose committed publication fails before E2
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(label="new")
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
patch.object(
encrypted_db,
"commit_rekey_epoch",
side_effect=OSError("commit publication detail"),
),
):
# When committed publication reports failure
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then the proven new key is a completed change, and E1+pending-E2
# stays on disk so caches stay fenced until the next open commits it
assert result is True
state = read_rekey_epoch_state(db_path)
assert state.committed_epoch == original_epoch
assert state.pending_epoch is not None
def test_commit_wrapper_failure_after_e2_publication_returns_success(
manager: DatabaseManager,
) -> None:
# Given the commit wrapper publishes E2 before reporting an I/O failure
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(label="new")
def publish_then_raise(path: Path, candidate_epoch: str) -> None:
commit_rekey_epoch(path, candidate_epoch)
raise OSError("post-replace directory fsync detail")
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
patch.object(
encrypted_db,
"commit_rekey_epoch",
side_effect=publish_then_raise,
),
):
# When change_password reconciles publication while holding the sidecar
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then verified E2 is success and its pending marker is cleaned up
state = read_rekey_epoch_state(db_path)
assert result is True
assert state.committed_epoch != original_epoch
assert state.pending_epoch is None
def test_commit_reconciliation_cleanup_failure_preserves_explicit_state(
manager: DatabaseManager,
) -> None:
# Given E2 publishes before the wrapper and recovery cleanup both fail
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(label="new")
def publish_then_raise(path: Path, candidate_epoch: str) -> None:
commit_rekey_epoch(path, candidate_epoch)
raise OSError("post-replace directory fsync detail")
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
patch.object(
encrypted_db,
"commit_rekey_epoch",
side_effect=publish_then_raise,
),
patch.object(
encrypted_db,
"clear_pending_rekey_epoch",
side_effect=OSError("cleanup detail"),
),
):
# When change_password reconciles committed publication
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then success is final while E2+pending-E2 remains recoverable
state = read_rekey_epoch_state(db_path)
assert result is True
assert state.committed_epoch != original_epoch
assert state.pending_epoch == state.committed_epoch
def test_unreadable_commit_state_reread_after_proven_key_is_success(
manager: DatabaseManager,
) -> None:
# Given a verified candidate whose commit failure cannot be reconciled
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(label="new")
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(
encrypted_db,
"read_rekey_epoch_state",
side_effect=[
RekeyEpochState(original_epoch, None),
RekeyRecoveryRequiredError(db_path),
],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
patch.object(
encrypted_db,
"commit_rekey_epoch",
side_effect=OSError("commit publication detail"),
),
):
# When commit reconciliation cannot parse durable state
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then the proven key still makes it a completed change, and the
# pending metadata remains untouched for the next open's recovery
assert result is True
state = read_rekey_epoch_state(db_path)
assert state.committed_epoch == original_epoch
assert state.pending_epoch is not None
def test_candidate_close_failure_after_commit_returns_success(
manager: DatabaseManager,
) -> None:
# Given a fully verifiable candidate whose final raw close fails
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(label="new")
new_connection.close.side_effect = OSError("simulated close failure")
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
):
# When durable verification and epoch publication complete first
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then cleanup cannot reverse the committed manager outcome
state = read_rekey_epoch_state(db_path)
assert result is True
assert state.pending_epoch is None
assert state.committed_epoch != original_epoch
def test_pending_cleanup_failure_after_commit_returns_success(
manager: DatabaseManager,
) -> None:
# Given a verified candidate whose pending cleanup fails after publication
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(label="new")
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
patch.object(
encrypted_db,
"clear_pending_rekey_epoch",
side_effect=OSError("simulated pending cleanup failure"),
),
):
# When the committed epoch is durable before cleanup runs
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then success is final and E2+pending-E2 remains explicit for recovery
state = read_rekey_epoch_state(db_path)
assert result is True
assert state.committed_epoch != original_epoch
assert state.pending_epoch == state.committed_epoch
assert rekey_lock_module.read_rekey_epoch(db_path) is None
def test_sidecar_release_failure_after_commit_returns_success(
manager: DatabaseManager,
) -> None:
# Given a successful rekey whose sidecar release reports an I/O error
db_path = _existing_db(manager, "alice")
original_epoch = _initialize_epoch(db_path)
old_connection = _raw_connection(label="old")
new_connection = _raw_connection(label="new")
@contextmanager
def release_failure(
path: Path,
*,
blocking: bool = False,
local_wait: float | None = None,
contention_retry: float = 0.0,
):
with acquire_rekey_lock(
path,
blocking=blocking,
local_wait=local_wait,
contention_retry=contention_retry,
):
yield
raise OSError("simulated sidecar release failure")
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=[old_connection, new_connection],
),
patch.object(encrypted_db, "set_sqlcipher_rekey"),
patch.object(
encrypted_db,
"acquire_rekey_lock",
side_effect=release_failure,
),
):
# When release fails only after committed publication and cleanup
result = manager.change_password(
"alice", "old-password", "new-password"
)
# Then the manager preserves success and the stable committed generation
state = read_rekey_epoch_state(db_path)
assert result is True
assert state.pending_epoch is None
assert state.committed_epoch != original_epoch
@pytest.mark.parametrize("candidate_already_committed", [False, True])
def test_cold_open_recovers_verified_pending_candidate(
manager: DatabaseManager,
candidate_already_committed: bool,
) -> None:
# Given either an ambiguous E1+pending-E2 or cleanup-only E2+pending-E2 state
db_path = _existing_db(manager, "alice")
with acquire_rekey_lock(db_path):
original_epoch = rekey_lock_module.read_or_initialize_rekey_epoch(
db_path
)
pending_epoch = begin_rekey_epoch(db_path)
if candidate_already_committed:
commit_rekey_epoch(db_path, pending_epoch)
recovery_connection = _raw_connection(label="recovery")
engine = MagicMock()
with (
patch.object(
manager,
"_make_sqlcipher_connection",
return_value=recovery_connection,
) as make_raw,
patch.object(
manager, "_open_user_database_cold", return_value=engine
) as cold_open,
patch.object(manager, "_run_phase2_rekey"),
):
# When cold open validates the supplied password against the surviving key
opened = manager.open_user_database("alice", "surviving-password")
# Then E2 is finalized before migrations and cache publication
assert opened is engine
make_raw.assert_called_once_with(db_path, "surviving-password")
cold_open.assert_called_once()
state = read_rekey_epoch_state(db_path)
assert state == RekeyEpochState(
committed_epoch=pending_epoch,
pending_epoch=None,
)
if not candidate_already_committed:
assert state.committed_epoch != original_epoch
def test_cold_open_wrong_key_leaves_pending_state_untouched(
manager: DatabaseManager,
) -> None:
# Given an interrupted rekey and a password that cannot open its surviving key
db_path = _existing_db(manager, "alice")
with acquire_rekey_lock(db_path):
original_epoch = rekey_lock_module.read_or_initialize_rekey_epoch(
db_path
)
pending_epoch = begin_rekey_epoch(db_path)
secret = "wrong-recovery-password"
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=ValueError(secret),
),
patch.object(manager, "_open_user_database_cold") as cold_open,
):
# When cold-open recovery cannot open the surviving key with it
opened = manager.open_user_database("alice", secret)
# Then it is an ordinary failed login (None, which the login route
# counts toward the lockout) and the candidate is left untouched
assert opened is None
assert read_rekey_epoch_state(db_path) == RekeyEpochState(
committed_epoch=original_epoch,
pending_epoch=pending_epoch,
)
assert rekey_lock_module.read_rekey_epoch(db_path) is None
cold_open.assert_not_called()
def test_change_password_wrong_key_during_pending_returns_false(
manager: DatabaseManager,
) -> None:
# Given an interrupted rekey and a current password that opens neither key
db_path = _existing_db(manager, "alice")
with acquire_rekey_lock(db_path):
original_epoch = rekey_lock_module.read_or_initialize_rekey_epoch(
db_path
)
pending_epoch = begin_rekey_epoch(db_path)
with (
patch.object(
manager,
"_make_sqlcipher_connection",
side_effect=ValueError("wrong key"),
) as make_raw,
patch.object(encrypted_db, "set_sqlcipher_rekey") as rekey,
):
# When a password change is attempted with that password
result = manager.change_password(
"alice", "wrong-password", "new-password"
)
# Then it is the ordinary wrong-current-password outcome, nothing moved
assert result is False
make_raw.assert_called_once_with(db_path, "wrong-password")
rekey.assert_not_called()
assert read_rekey_epoch_state(db_path) == RekeyEpochState(
committed_epoch=original_epoch,
pending_epoch=pending_epoch,
)
def test_rekey_lock_registry_is_distinct_from_init_locks(
manager: DatabaseManager,
) -> None:
# Given both per-user lifecycle locks are requested for Alice
init_lock = manager._get_init_lock("alice")
# When the rekey lock is resolved
rekey_lock = manager._get_rekey_lock("alice")
# Then rekey serialization does not reuse the cold-open lock
assert rekey_lock is not init_lock
def _hold_sidecar_in_child(db_path: str, acquired, release) -> None:
with acquire_rekey_lock(Path(db_path)):
acquired.set()
if not release.wait(timeout=30):
raise RuntimeError("parent did not release the sidecar holder")
def test_cross_process_flock_holder_is_typed_contention_not_a_hang(
manager: DatabaseManager,
) -> None:
# Given a separate process holding Alice's sidecar flock (the in-process
# gate is free, so change_password reaches the flock itself)
db_path = _existing_db(manager, "alice")
_initialize_epoch(db_path)
context = multiprocessing.get_context("spawn")
acquired = context.Event()
release = context.Event()
holder = context.Process(
target=_hold_sidecar_in_child,
args=(str(db_path), acquired, release),
)
holder.start()
try:
assert acquired.wait(timeout=20), "child did not take the sidecar"
with (
patch.object(manager, "_make_sqlcipher_connection") as make_raw,
ThreadPoolExecutor(max_workers=1) as executor,
):
# When this process attempts Alice's password change
started = time.monotonic()
attempt = executor.submit(
manager.change_password,
"alice",
"old-password",
"new-password",
)
try:
# Then it fails with the typed contention error after the
# short retry window -- a blocking flock would hang here
# until the holder exits
with pytest.raises(RekeyLockContentionError):
attempt.result(timeout=8)
elapsed = time.monotonic() - started
finally:
release.set()
assert elapsed >= encrypted_db._REKEY_CONTENTION_RETRY_SECONDS * 0.9
make_raw.assert_not_called()
finally:
release.set()
holder.join(timeout=10)
if holder.is_alive():
holder.terminate()
holder.join(timeout=5)
assert holder.exitcode == 0
def test_disposal_checkpoint_busy_wait_is_bounded_and_restored(
manager: DatabaseManager,
) -> None:
# Given a pooled connection whose busy timeout is the configured one
engine = MagicMock()
connection = engine.connect.return_value.__enter__.return_value
statements: list[str] = []
def execute(statement):
statements.append(str(statement))
return MagicMock()
connection.execute.side_effect = execute
# When the gated disposal checkpoint runs
manager._checkpoint_wal(engine, "for alice")
# Then the TRUNCATE waits at most the gated bound, and the connection
# gets its configured busy timeout back before it returns to the pool
bound = encrypted_db._gated_checkpoint_busy_timeout_ms()
configured = encrypted_db.get_sqlcipher_busy_timeout_ms()
assert bound <= encrypted_db._GATED_CHECKPOINT_BUSY_TIMEOUT_MS
assert statements == [
f"PRAGMA busy_timeout = {bound}",
"PRAGMA wal_checkpoint(TRUNCATE)",
f"PRAGMA busy_timeout = {configured}",
]
def test_post_rekey_checkpoint_busy_wait_is_bounded(
manager: DatabaseManager,
) -> None:
# Given a fresh new-key connection
events: list[str] = []
connection = _raw_connection(events=events, label="new")
# When the post-rekey checkpoint runs while the sidecar gate is held
assert manager._best_effort_rekey_checkpoint(connection) is True
# Then its busy wait is bounded before the TRUNCATE is issued
assert events == [
"new:PRAGMA busy_timeout = "
f"{encrypted_db._gated_checkpoint_busy_timeout_ms()}",
"new:PRAGMA wal_checkpoint(TRUNCATE)",
]
def _busy_timeout_f_string_executions(source: str) -> list[int]:
"""Lines where an f-string ``PRAGMA busy_timeout`` reaches execute()."""
found = []
for node in ast.walk(ast.parse(source)):
if not (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Attribute)
and node.func.attr == "execute"
and node.args
):
continue
statement = node.args[0]
if (
isinstance(statement, ast.Call)
and isinstance(statement.func, ast.Name)
and statement.func.id == "text"
and statement.args
):
statement = statement.args[0]
if isinstance(statement, ast.JoinedStr) and any(
isinstance(part, ast.Constant) and "busy_timeout" in str(part.value)
for part in statement.values
):
found.append(node.lineno)
return found
def test_busy_timeout_pragmas_are_built_from_an_int_template() -> None:
# Given the modules that set a connection's busy timeout
from local_deep_research.database import sqlcipher_utils
# When their execute() calls are scanned
offenders = {
module.__name__: _busy_timeout_f_string_executions(
Path(module.__file__).read_text(encoding="utf-8")
)
for module in (encrypted_db, sqlcipher_utils)
}
# Then no PRAGMA busy_timeout is an f-string handed to execute() (the
# repo Semgrep rule sql-execute-with-string-format); each one comes
# from a fixed "{:d}" template formatted with an int
assert offenders == {
encrypted_db.__name__: [],
sqlcipher_utils.__name__: [],
}
def test_gated_checkpoint_pragma_carries_only_an_integer(
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Given the gated checkpoint bound
bound = encrypted_db._gated_checkpoint_busy_timeout_ms()
# Then its statement carries exactly that integer
assert encrypted_db._gated_checkpoint_busy_timeout_pragma() == (
f"PRAGMA busy_timeout = {bound}"
)
# And a value that is not an integer never reaches the statement
monkeypatch.setattr(
encrypted_db,
"_gated_checkpoint_busy_timeout_ms",
lambda: "5000; PRAGMA key = 'x'",
)
with pytest.raises(ValueError):
encrypted_db._gated_checkpoint_busy_timeout_pragma()
def test_pre_rekey_checkpoint_is_passive_and_never_refuses(
manager: DatabaseManager,
) -> None:
# Given an old-key connection whose WAL a reader keeps pinned (busy)
events: list[str] = []
connection = _raw_connection(
_RawVerification(checkpoint=(1, 7, 3)), events=events, label="old"
)
# When the pre-rekey checkpoint runs, it neither waits nor refuses
manager._pre_rekey_checkpoint(connection)
# Then only a PASSIVE checkpoint (no busy handler) was issued
assert events == ["old:PRAGMA wal_checkpoint(PASSIVE)"]
@pytest.mark.parametrize(
"error_number", [errno.EINVAL, getattr(errno, "ENOTSUP", errno.EINVAL)]
)
def test_unsupported_directory_fsync_does_not_fail_epoch_publication(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, error_number: int
) -> None:
# Given a filesystem that cannot fsync directories at all
if os.name == "nt":
pytest.skip("directory fsync is skipped entirely on Windows")
db_path = tmp_path / "alice.db"
db_path.touch()
real_fsync = os.fsync
def no_directory_fsync(fd: int) -> None:
if _is_directory_fd(fd):
raise OSError(error_number, "unsupported")
real_fsync(fd)
monkeypatch.setattr(os, "fsync", no_directory_fsync)
# When the first epoch is published there
with acquire_rekey_lock(db_path):
epoch = rekey_lock_module.read_or_initialize_rekey_epoch(db_path)
# Then the rename is as durable as that filesystem allows, not an error
assert read_rekey_epoch_state(db_path) == RekeyEpochState(epoch, None)
def test_failing_directory_fsync_still_fails_epoch_publication(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
# Given a directory fsync that genuinely fails (EIO)
if os.name == "nt":
pytest.skip("directory fsync is skipped entirely on Windows")
db_path = tmp_path / "alice.db"
db_path.touch()
real_fsync = os.fsync
def failing_directory_fsync(fd: int) -> None:
if _is_directory_fd(fd):
raise OSError(errno.EIO, "I/O error")
real_fsync(fd)
monkeypatch.setattr(os, "fsync", failing_directory_fsync)
# When the first epoch is published, the failure still propagates
with acquire_rekey_lock(db_path):
with pytest.raises(OSError) as raised:
rekey_lock_module.read_or_initialize_rekey_epoch(db_path)
assert raised.value.errno == errno.EIO
def _is_directory_fd(fd: int) -> bool:
return stat.S_ISDIR(os.fstat(fd).st_mode)
def _make_epoch_file_unreadable(db_path: Path) -> None:
# A directory where the epoch file should be: read_text raises
# IsADirectoryError, an OSError other than FileNotFoundError, so the
# reader raises RekeyEpochUnreadableError (as for EMFILE or EIO).
epoch_file = db_path.with_name(f"{db_path.name}.rekey.epoch")
epoch_file.unlink()
epoch_file.mkdir()
@pytest.mark.parametrize(
("disturb", "expected_error"),
[
pytest.param(
_make_epoch_file_unreadable,
RekeyEpochUnreadableError,
id="epoch-file-unreadable",
),
pytest.param(
rekey_lock_module.begin_rekey_epoch,
RekeyRecoveryRequiredError,
id="pending-epoch-appeared",
),
pytest.param(
rekey_lock_module.advance_rekey_epoch,
RekeyRecoveryRequiredError,
id="committed-epoch-moved",
),
],
)
def test_post_open_epoch_recheck_that_cannot_vouch_fails_closed(
manager: DatabaseManager, disturb, expected_error
) -> None:
# Given a cold open that proved the password, after which the re-read
# of the epoch fails, finds a pending epoch, or finds a different
# committed epoch (only a writer bypassing the held sidecar can cause
# the last two)
db_path = _existing_db(manager, "alice")
_initialize_epoch(db_path)
engine = MagicMock()
def cold_open_then_disturb(*_args, **_kwargs) -> MagicMock:
disturb(db_path)
return engine
with (
patch.object(
manager,
"_open_user_database_cold",
side_effect=cold_open_then_disturb,
),
patch.object(manager, "_run_phase2_rekey") as phase2,
):
# When the user logs in, nothing is known against the credential:
# the open raises the RekeyOperationalError the login route answers
# with 503 (lockout untouched), never the None a wrong password
# returns (401, counted toward lockout)
with pytest.raises(expected_error):
manager.open_user_database("alice", "password")
# Then the engine is still discarded unpublished
engine.dispose.assert_called_once_with()
assert "alice" not in manager.connections
phase2.assert_not_called()
def test_cold_open_epoch_publication_io_failure_is_typed(
manager: DatabaseManager, monkeypatch: pytest.MonkeyPatch
) -> None:
# Given a database whose first epoch (post-upgrade) cannot be written
db_path = _existing_db(manager, "alice")
def full_disk(*_args, **_kwargs):
raise OSError(errno.ENOSPC, "No space left on device")
monkeypatch.setattr(rekey_lock_module.tempfile, "mkstemp", full_disk)
with patch.object(manager, "_open_user_database_cold") as cold_open:
# When the user logs in, the I/O failure is a typed sidecar error
# (the login route answers 503 without counting it toward the
# lockout), not a raw OSError that escapes as a server error
with pytest.raises(RekeySidecarIOError) as raised:
manager.open_user_database("alice", "password")
assert raised.value.db_path == db_path
assert raised.value.operation is RekeySidecarOperation.PUBLISH
assert raised.value.errno == errno.ENOSPC
assert raised.value.__cause__ is None
assert raised.value.__suppress_context__ is True
cold_open.assert_not_called()
def test_concurrent_cold_opens_fail_within_one_bound_while_sidecar_held(
manager: DatabaseManager,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Given a held sidecar (a long rekey) and a slow key derivation
db_path = _existing_db(manager, "alice")
_initialize_epoch(db_path)
bound = 0.4
derivation = 0.2
monkeypatch.setattr(encrypted_db, "_SIDECAR_WAIT_TIMEOUT_SECONDS", bound)
def slow_derivation(_password, **_kwargs) -> bytes:
time.sleep(derivation)
return b"\x00" * 32
monkeypatch.setattr(encrypted_db, "get_key_from_password", slow_derivation)
holder_ready = threading.Event()
release_holder = threading.Event()
def hold_sidecar() -> None:
with acquire_rekey_lock(db_path):
holder_ready.set()
assert release_holder.wait(timeout=30)
holder = threading.Thread(target=hold_sidecar, daemon=True)
holder.start()
assert holder_ready.wait(timeout=5)
openers = 6
def open_once() -> tuple[str, float]:
start = time.monotonic()
try:
manager.open_user_database("alice", "password")
except RekeyLockContentionError:
return "contention", time.monotonic() - start
return "returned", time.monotonic() - start
try:
# When several opens of that user arrive at once
started = time.monotonic()
with ThreadPoolExecutor(max_workers=openers) as pool:
outcomes = list(pool.map(lambda _: open_once(), range(openers)))
elapsed = time.monotonic() - started
finally:
release_holder.set()
holder.join(timeout=5)
# Then every one fails with the typed contention error within about one
# bound (plus one derivation), not one after another behind init_lock
assert [kind for kind, _ in outcomes] == ["contention"] * openers
serial = openers * (bound + derivation)
assert elapsed < 2 * (bound + derivation) + 0.5 < serial
assert (
max(duration for _, duration in outcomes)
< 2 * (bound + derivation) + 0.5
)
def test_recovery_that_cannot_prove_the_key_does_not_commit(
manager: DatabaseManager,
) -> None:
# Given a pending generation and a password whose connection opens but
# cannot decrypt the schema page (the key proof fails)
db_path = _existing_db(manager, "alice")
_initialize_epoch(db_path)
begin_rekey_epoch(db_path)
state = read_rekey_epoch_state(db_path)
unproven = _raw_connection(_RawVerification(schema_readable=False))
with patch.object(
manager, "_make_sqlcipher_connection", return_value=unproven
):
# When recovery runs with it
with pytest.raises(RekeyRecoveryRequiredError):
manager._recover_pending_rekey(db_path, "password", state)
# Then nothing is committed or cleared
assert read_rekey_epoch_state(db_path) == state
unproven.close.assert_called_once_with()
def test_recovery_runs_the_key_proof_before_committing(
manager: DatabaseManager,
) -> None:
# Given a pending generation and a password that proves the key
db_path = _existing_db(manager, "alice")
_initialize_epoch(db_path)
pending = begin_rekey_epoch(db_path)
state = read_rekey_epoch_state(db_path)
events: list[str] = []
proven = _raw_connection(events=events, label="recovery")
real_commit = commit_rekey_epoch
def commit_after_proof(path: Path, candidate: str) -> None:
assert "recovery:SELECT count(*) FROM sqlite_master" in events
events.append("commit")
real_commit(path, candidate)
with (
patch.object(
manager, "_make_sqlcipher_connection", return_value=proven
),
patch.object(
encrypted_db, "commit_rekey_epoch", side_effect=commit_after_proof
),
):
# When recovery runs
assert manager._recover_pending_rekey(db_path, "password", state)
# Then the key proof ran first and the pending epoch is committed
assert "commit" in events
assert read_rekey_epoch_state(db_path) == RekeyEpochState(pending, None)
def test_lifecycle_lock_timeout_spends_the_request_wait_budget(
manager: DatabaseManager,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Given a cached engine whose lifecycle lock another claim holds
db_path = _existing_db(manager, "alice")
epoch = _initialize_epoch(db_path)
manager._cache_connection(
"alice",
manager._make_connection_cache_entry(MagicMock(), "password", epoch),
)
monkeypatch.setattr(encrypted_db, "_SIDECAR_WAIT_TIMEOUT_SECONDS", 0.2)
lifecycle_lock = manager._get_rekey_lock("alice")
assert lifecycle_lock.acquire(timeout=1)
try:
with encrypted_db.sidecar_wait_budget():
assert encrypted_db._sidecar_wait_seconds() == 0.2
# When a claim gives up on the lifecycle lock
with pytest.raises(RekeyLockContentionError):
manager.get_session("alice")
# Then the request's later waits try once without waiting
assert encrypted_db._sidecar_wait_seconds() == 0.0
finally:
lifecycle_lock.release()