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.

test(backup): pin epoch re-read after sidecar acquisition (#7706)

Chanhyo Jungab525e8bb2ae
diff --git a/tests/database/backup/test_superseded_key_purge.py b/tests/database/backup/test_superseded_key_purge.py
index 496f30f639..4210198f77 100644
--- a/tests/database/backup/test_superseded_key_purge.py
+++ b/tests/database/backup/test_superseded_key_purge.py
@@ -909,3 +909,180 @@ def test_refresh_holds_cross_process_rekey_lock_through_the_purge(manager):
assert manager.refresh_backups_after_password_change(username, NEW)
assert observed and set(observed) == {"contended"}
+
+
+def _publish_after_purge_contention(db_path, advance, held, attempted, pipe):
+ """Publish only after the parent has actually encountered our OS lock."""
+ from pathlib import Path
+
+ try:
+ with acquire_rekey_lock(Path(db_path)):
+ held.set()
+ if not attempted.wait(30):
+ raise TimeoutError("purge never encountered the held sidecar")
+ if advance:
+ advance_rekey_epoch(Path(db_path))
+ pipe.send(("published", read_rekey_epoch_state(Path(db_path))))
+ except BaseException as error:
+ pipe.send(("error", repr(error)))
+ raise
+ finally:
+ pipe.close()
+
+
+ "advance", [True, False], ids=["advanced", "unchanged"]
+)
+def test_purge_reads_epoch_after_contended_sidecar_acquisition(
+ manager, advance
+):
+ from local_deep_research.database import rekey_lock
+ from local_deep_research.database.sqlcipher_utils import (
+ create_database_salt,
+ create_sqlcipher_connection,
+ )
+
+ # A minimal real database avoids exporting the application's full schema.
+ username = f"epoch_race_{uuid.uuid4().hex[:8]}"
+ db_path = manager._get_user_db_path(username)
+ create_database_salt(db_path)
+ connection = create_sqlcipher_connection(db_path, OLD, creation_mode=True)
+ try:
+ connection.execute("CREATE TABLE notes (body TEXT)")
+ connection.execute("INSERT INTO notes VALUES ('epoch fence')")
+ connection.commit()
+ finally:
+ connection.close()
+ old_backup = _backup(username, OLD)
+ _rekey_live_database(db_path, OLD, NEW)
+ current_backup = _backup(username, NEW)
+ service = BackupService(username=username, password=NEW)
+ assert (
+ BackupService(username=username, password=OLD)._classify_backup_key(
+ old_backup
+ )
+ == "current"
+ )
+ assert service._classify_backup_key(old_backup) == "superseded"
+ assert service._classify_backup_key(current_backup) == "current"
+ pairs = [old_backup, current_backup]
+ files = [
+ path
+ for backup in pairs
+ for path in (backup, get_salt_file_path(backup))
+ ]
+ before = {path: path.read_bytes() for path in files}
+ with acquire_rekey_lock(db_path):
+ expected = read_or_initialize_rekey_epoch(db_path)
+
+ context = multiprocessing.get_context("spawn")
+ held, attempted = context.Event(), context.Event()
+ receiver, sender = context.Pipe(duplex=False)
+ child = context.Process(
+ target=_publish_after_purge_contention,
+ args=(str(db_path), advance, held, attempted, sender),
+ )
+ events, faults, reads, warnings, lock_paths = [], [], [], [], []
+ symbol = (
+ "_acquire_windows_lock" if os.name == "nt" else "_acquire_posix_lock"
+ )
+ original_acquire = getattr(rekey_lock, symbol)
+ original_read = encrypted_db.read_rekey_epoch_state
+ original_warning = encrypted_db.logger.warning
+
+ def observe_acquire(fd, lock_path, **kwargs):
+ lock_paths.append(lock_path)
+ try:
+ result = original_acquire(fd, lock_path, **kwargs)
+ except RekeyLockContentionError:
+ events.append("contended")
+ attempted.set()
+ raise
+ except BaseException as error:
+ faults.append(error)
+ raise
+ else:
+ events.append("acquired")
+ return result
+
+ def observe_read(path):
+ try:
+ state = original_read(path)
+ except BaseException as error:
+ faults.append(error)
+ raise
+ reads.append((list(events), state))
+ return state
+
+ def observe_warning(*args, **kwargs):
+ warnings.append(args)
+ return original_warning(*args, **kwargs)
+
+ try:
+ child.start()
+ sender.close()
+ assert held.wait(30), "child never acquired the real sidecar"
+ with (
+ patch.object(rekey_lock, symbol, side_effect=observe_acquire),
+ patch.object(
+ encrypted_db, "read_rekey_epoch_state", side_effect=observe_read
+ ),
+ patch.object(
+ encrypted_db.logger, "warning", side_effect=observe_warning
+ ),
+ ):
+ manager._purge_superseded_key_backups(
+ username, NEW, expected_epoch=expected
+ )
+ assert receiver.poll(30), "child never reported durable publication"
+ outcome, published = receiver.recv()
+ child.join(timeout=10)
+ assert child.exitcode == 0, (child.exitcode, outcome, published)
+ assert outcome == "published", published
+ assert not faults, faults
+ assert not warnings, warnings
+ assert set(lock_paths) == {
+ db_path.with_name(f"{db_path.name}.rekey.lock")
+ }
+ assert "contended" in events, events
+ assert events[-1] == "acquired", events
+ assert len(reads) == 1, reads
+ assert published == read_rekey_epoch_state(db_path)
+ assert published.pending_epoch is None
+ assert (published.committed_epoch != expected) is advance
+ protected = (
+ files
+ if advance
+ else [current_backup, get_salt_file_path(current_backup)]
+ )
+ for path in protected:
+ assert path.exists(), f"epoch fence lost {path.name}"
+ assert path.read_bytes() == before[path]
+ if not advance:
+ assert not old_backup.exists(), (
+ "unchanged epoch never performed deletion"
+ )
+ assert not get_salt_file_path(old_backup).exists()
+ order, state = reads[0]
+ # The control must still delete under the pre-lock-read mutant; the
+ # advanced case is the guardian for read ordering and preservation.
+ if advance:
+ assert order and order[-1] == "acquired", (
+ "epoch read preceded OS lock acquisition"
+ )
+ assert "contended" in order, order
+ assert state == published
+ finally:
+ attempted.set()
+ if child.pid is not None:
+ child.join(timeout=10)
+ if child.is_alive():
+ child.terminate()
+ child.join(timeout=5)
+ if child.is_alive():
+ child.kill()
+ child.join(timeout=5)
+ receiver.close()
+ sender.close()
+ assert not child.is_alive(), "owned epoch writer survived cleanup"
+ child.close()