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.

chore(library): drop dead _save_and_record_locked helper (#4202)

LearningCircuit3eb124de9407
diff --git a/src/local_deep_research/research_library/services/library_rag_service.py b/src/local_deep_research/research_library/services/library_rag_service.py
index 1e691fb5d2..55479f0f1e 100644
--- a/src/local_deep_research/research_library/services/library_rag_service.py
+++ b/src/local_deep_research/research_library/services/library_rag_service.py
@@ -418,26 +418,6 @@ class LibraryRAGService:
)
raise
- def _save_and_record_locked(self, index_path: Path) -> None:
- """Atomically save the in-memory FAISS index to ``index_path``
- and record its checksum in the integrity manager.
-
- The save_local + record_file pair must be serialised across
- concurrent indexers for the same ``(username, index_path)`` so
- a second writer doesn't overwrite the file between the first
- writer's save and checksum recording — that race produces the
- checksum_mismatch / silent-deletion bug in #4197.
- """
- with _get_faiss_write_lock(self.username, str(index_path)):
- self.faiss_index.save_local(
- str(index_path.parent), index_name=index_path.stem
- )
- self.integrity_manager.record_file(
- index_path,
- related_entity_type="rag_index",
- related_entity_id=self.rag_index_record.id,
- )
-
def _merge_and_persist_locked(
self,
index_path: Path,
diff --git a/tests/research_library/services/test_library_rag_service_coverage.py b/tests/research_library/services/test_library_rag_service_coverage.py
index 6e52c215aa..55af1fc8db 100644
--- a/tests/research_library/services/test_library_rag_service_coverage.py
+++ b/tests/research_library/services/test_library_rag_service_coverage.py
@@ -932,87 +932,70 @@ class TestFaissWriteLock:
same_u2 = mod._get_faiss_write_lock("u2", str(tmp_path / "a.faiss"))
assert same_u2 is u2_lock
- def test_concurrent_save_record_serialised_for_same_key(self, tmp_path):
- """Two threads calling _save_and_record_locked on the same
- (username, index_path) MUST observe non-overlapping critical
+ def test_concurrent_holders_serialised_for_same_key(self, tmp_path):
+ """Two threads acquiring ``_get_faiss_write_lock`` for the same
+ ``(username, index_path)`` MUST observe non-overlapping critical
sections. Regression for the #4197 race.
"""
self._reset_locks()
+ mod = _import_module()
index_path = tmp_path / "idx.faiss"
- index_path.touch()
in_flight = 0
max_in_flight = 0
observed_overlap = False
lock_for_counters = threading.Lock()
- save_started = threading.Event()
- save_can_finish = threading.Event()
-
- def fake_save_local(folder, index_name):
- nonlocal in_flight, max_in_flight, observed_overlap
- with lock_for_counters:
- in_flight += 1
- if in_flight > max_in_flight:
- max_in_flight = in_flight
- if in_flight > 1:
- observed_overlap = True
- save_started.set()
- # Hold the critical section open for the second thread
- save_can_finish.wait(timeout=2.0)
- with lock_for_counters:
- in_flight -= 1
-
- def fake_record_file(path, **kwargs):
- return None
+ first_inside = threading.Event()
+ first_can_finish = threading.Event()
+ first_holder = [True] # mutable flag to mark which thread enters first
def worker():
- svc = _make_service()
- svc.faiss_index = MagicMock()
- svc.faiss_index.save_local.side_effect = fake_save_local
- svc.integrity_manager = MagicMock()
- svc.integrity_manager.record_file.side_effect = fake_record_file
- svc.rag_index_record = MagicMock(id=42)
- svc._save_and_record_locked(index_path)
+ with mod._get_faiss_write_lock("u1", str(index_path)):
+ nonlocal in_flight, max_in_flight, observed_overlap
+ with lock_for_counters:
+ in_flight += 1
+ if in_flight > max_in_flight:
+ max_in_flight = in_flight
+ if in_flight > 1:
+ observed_overlap = True
+ am_first = first_holder[0]
+ first_holder[0] = False
+ if am_first:
+ first_inside.set()
+ # Hold the critical section open so the second
+ # thread blocks behind us, not races us.
+ first_can_finish.wait(timeout=2.0)
+ with lock_for_counters:
+ in_flight -= 1
t1 = threading.Thread(target=worker)
t2 = threading.Thread(target=worker)
t1.start()
- # Make sure t1 is inside save_local before t2 starts trying
- assert save_started.wait(timeout=2.0)
+ assert first_inside.wait(timeout=2.0)
t2.start()
- # Brief moment for t2 to attempt and (hopefully) block on the lock
- save_can_finish.set()
+ first_can_finish.set()
t1.join(timeout=3.0)
t2.join(timeout=3.0)
assert not observed_overlap, (
- "Two threads ran save_local on the same index concurrently; "
- "the lock did not serialise them."
+ "Two threads held the same FAISS write lock concurrently."
)
assert max_in_flight == 1
def test_different_keys_run_in_parallel(self, tmp_path):
- """Two threads with different (username, index_path) keys must
- be able to run save+record simultaneously.
+ """Two threads acquiring locks for different
+ ``(username, index_path)`` keys must run simultaneously.
"""
self._reset_locks()
+ mod = _import_module()
barrier = threading.Barrier(2, timeout=2.0)
idx1 = tmp_path / "a.faiss"
idx2 = tmp_path / "b.faiss"
- idx1.touch()
- idx2.touch()
-
- def fake_save_local(folder, index_name):
- # If the lock blocked us, barrier.wait would time out
- barrier.wait()
def worker(index_path):
- svc = _make_service()
- svc.faiss_index = MagicMock()
- svc.faiss_index.save_local.side_effect = fake_save_local
- svc.integrity_manager = MagicMock()
- svc.rag_index_record = MagicMock(id=99)
- svc._save_and_record_locked(index_path)
+ with mod._get_faiss_write_lock("u1", str(index_path)):
+ # If the lock blocked us, barrier.wait would time out
+ barrier.wait()
t1 = threading.Thread(target=worker, args=(idx1,))
t2 = threading.Thread(target=worker, args=(idx2,))