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.

9.0 KB
Raw Blame
"""Logical collection locks stay independent of filesystem path semantics."""

import os
import threading
from uuid import uuid4

import pytest

from local_deep_research.research_library.services import (
    library_rag_service as rag,
)


@pytest.fixture
def lock_users():
    # Do not clear the shared registry: unrelated workers may own its locks.
    prefix = f"collection-lock-test-{uuid4().hex}"
    users = (f"{prefix}-owner", f"{prefix}-other")
    try:
        yield users
    finally:
        for username in users:
            rag.pop_faiss_locks_for_user(username)


@pytest.mark.parametrize("purpose", ["transaction", "rebuild"])
def test_collection_locks_never_resolve_ids_as_filesystem_paths(
    monkeypatch, lock_users, purpose
):
    def refuse_filesystem_resolution(*_args, **_kwargs):
        raise AssertionError("Collection IDs must not reach Path.resolve")

    with monkeypatch.context() as patch:
        patch.setattr(rag.Path, "resolve", refuse_filesystem_resolution)
        lock = rag._get_collection_lock(
            lock_users[0], "C:\\collection:opaque/../id", purpose=purpose
        )
        with rag._hold_collection_lock(
            lock_users[0], "C:\\collection:opaque/../id", purpose=purpose
        ) as held:
            assert held is lock
            assert held.locked()
    assert not lock.locked()


def test_pathlike_collection_ids_stay_distinct_and_ignore_working_directory(
    tmp_path, monkeypatch, lock_users
):
    identifiers = (
        "collection",
        "./collection",
        "folder/../collection",
        "collection/",
        "/collection",
        "C:\\folder\\..\\collection",
    )
    first_directory = tmp_path / "first"
    second_directory = tmp_path / "second"
    first_directory.mkdir()
    second_directory.mkdir()
    monkeypatch.chdir(first_directory)
    locks = [
        rag._get_collection_lock(lock_users[0], name, purpose="transaction")
        for name in identifiers
    ]
    assert len({id(lock) for lock in locks}) == len(identifiers)

    monkeypatch.chdir(second_directory)
    for name, expected in zip(identifiers, locks, strict=True):
        assert (
            rag._get_collection_lock(lock_users[0], name, purpose="transaction")
            is expected
        )


def test_collection_locks_separate_users_purposes_and_physical_paths(
    tmp_path, lock_users
):
    identifier = str(tmp_path / "collection")
    locks = (
        rag._get_collection_lock(
            lock_users[0], identifier, purpose="transaction"
        ),
        rag._get_collection_lock(lock_users[0], identifier, purpose="rebuild"),
        rag._get_collection_lock(
            lock_users[1], identifier, purpose="transaction"
        ),
        rag._get_faiss_write_lock(lock_users[0], identifier),
    )
    assert len({id(lock) for lock in locks}) == len(locks)


@pytest.mark.parametrize("purpose", ["transaction", "rebuild"])
def test_other_thread_is_excluded_until_collection_lock_is_released(
    monkeypatch, lock_users, purpose
):
    attempted = threading.Event()
    blocking_attempted = threading.Event()
    entered = threading.Event()
    release_worker = threading.Event()
    nonblocking_results = []
    errors = []

    def contender():
        try:
            with rag._hold_collection_lock(
                lock_users[0], "collection", purpose=purpose, blocking=False
            ) as available:
                nonblocking_results.append(available)
            attempted.set()
            with rag._hold_collection_lock(
                lock_users[0], "collection", purpose=purpose
            ) as held:
                assert held is not None and held.locked()
                entered.set()
                assert release_worker.wait(10), "Worker was not released"
        except BaseException as exc:
            errors.append(exc)
            attempted.set()

    worker = threading.Thread(target=contender, daemon=True)
    try:
        with rag._hold_collection_lock(
            lock_users[0], "collection", purpose=purpose
        ) as owner_lock:
            acquire = owner_lock.acquire

            def observe_acquire(*args, **kwargs):
                if kwargs.get("blocking", True):
                    blocking_attempted.set()
                return acquire(*args, **kwargs)

            monkeypatch.setattr(owner_lock, "acquire", observe_acquire)
            worker.start()
            assert attempted.wait(10), "Contender did not attempt acquisition"
            assert not errors, errors
            assert nonblocking_results == [None]
            assert blocking_attempted.wait(10), (
                "Blocking acquisition was not attempted"
            )
            assert not entered.is_set()
        assert entered.wait(10), (
            "Blocking contender did not acquire after release"
        )
    finally:
        release_worker.set()
        if worker.ident is not None:
            worker.join(10)
    assert not worker.is_alive(), "Contender did not finish"
    assert not errors, errors


def test_user_cleanup_preserves_nested_active_collection_locks(lock_users):
    username, other_user = lock_users
    inactive = rag._get_collection_lock(username, "idle", purpose="rebuild")
    other = rag._get_collection_lock(other_user, "idle", purpose="rebuild")

    with rag._hold_collection_lock(
        username, "collection", purpose="transaction"
    ) as outer:
        with rag._hold_collection_lock(
            username, "collection", purpose="transaction", blocking=False
        ) as inner:
            assert inner is outer
            rag.pop_faiss_locks_for_user(username)
            assert (
                rag._get_collection_lock(
                    username, "collection", purpose="transaction"
                )
                is outer
            )
        # The inner release must not make the outer holder evictable.
        rag.pop_faiss_locks_for_user(username)
        assert (
            rag._get_collection_lock(
                username, "collection", purpose="transaction"
            )
            is outer
        )
        assert rag._get_collection_lock(
            username, "idle", purpose="rebuild"
        ) is not (inactive)
        assert rag._get_collection_lock(
            other_user, "idle", purpose="rebuild"
        ) is (other)

    assert not outer.locked()
    rag.pop_faiss_locks_for_user(username)
    assert (
        rag._get_collection_lock(username, "collection", purpose="transaction")
        is not outer
    )


@pytest.mark.parametrize(
    "blocking", [True, False], ids=["blocking", "nonblocking"]
)
def test_collection_holder_retries_eviction_before_acquire(
    monkeypatch, lock_users, blocking
):
    username = lock_users[0]
    stale = rag._get_collection_lock(username, "collection", purpose="rebuild")
    original_acquire = rag._TrackedRLock.acquire
    replacements = []

    def evict_before_acquire(lock, *args, **kwargs):
        if lock is stale and not replacements:
            rag.pop_faiss_locks_for_user(username)
            replacements.append(
                rag._get_collection_lock(
                    username, "collection", purpose="rebuild"
                )
            )
        return original_acquire(lock, *args, **kwargs)

    monkeypatch.setattr(rag._TrackedRLock, "acquire", evict_before_acquire)
    with rag._hold_collection_lock(
        username, "collection", purpose="rebuild", blocking=blocking
    ) as held:
        assert len(replacements) == 1
        assert held is replacements[0] and held is not stale
        assert not stale.locked()
        assert held.locked()
        rag.pop_faiss_locks_for_user(username)
        assert (
            rag._get_collection_lock(username, "collection", purpose="rebuild")
            is held
        )
    assert not held.locked()


def test_physical_relative_and_absolute_aliases_keep_sharing_a_lock(
    tmp_path, monkeypatch, lock_users
):
    (tmp_path / "indices").mkdir()
    monkeypatch.chdir(tmp_path)
    paths = (
        "indices/index.faiss",
        "indices/./index.faiss",
        "indices/../indices/index.faiss",
        str(tmp_path / "indices" / "index.faiss"),
    )
    lock = rag._get_faiss_write_lock(lock_users[0], paths[0])
    for path in paths:
        assert rag._get_faiss_write_lock(lock_users[0], path) is lock
        with rag._hold_faiss_write_lock(lock_users[0], paths[0]):
            with rag._hold_faiss_write_lock(lock_users[0], path) as held:
                assert held is lock


def test_physical_symlink_alias_keeps_sharing_a_lock(tmp_path, lock_users):
    target = tmp_path / "index.faiss"
    alias = tmp_path / "alias.faiss"
    target.touch()
    try:
        alias.symlink_to(target)
    except (OSError, NotImplementedError) as exc:
        if os.name == "nt":
            pytest.skip(f"Windows symlink creation unavailable: {exc}")
        raise

    lock = rag._get_faiss_write_lock(lock_users[0], str(target))
    assert rag._get_faiss_write_lock(lock_users[0], str(alias)) is lock
    with rag._hold_faiss_write_lock(lock_users[0], str(target)):
        with rag._hold_faiss_write_lock(lock_users[0], str(alias)) as held:
            assert held is lock