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.

2.3 KB
Raw Blame
"""Stable-identity regressions for database per-user lock registries."""

import pytest

from local_deep_research.database import library_init
from local_deep_research.database.backup import backup_service


@pytest.fixture(
    params=[
        pytest.param(
            (library_init, "_get_user_init_lock", "pop_user_init_lock"),
            id="library-init",
        ),
        pytest.param(
            (backup_service, "_get_user_lock", "pop_user_lock"),
            id="backup",
        ),
    ]
)
def lock_registry(request, monkeypatch):
    """Give each regression an isolated copy of the selected registry."""
    module, getter_name, pop_name = request.param
    registry_name = (
        "_user_init_locks" if module is library_init else "_user_locks"
    )
    monkeypatch.setattr(module, registry_name, {})
    return getattr(module, getter_name), getattr(module, pop_name)


def test_user_close_cannot_replace_a_held_lock(lock_registry):
    """Cleanup must not expose a new lock while the original is held."""
    get_lock, pop_lock = lock_registry
    held_lock = get_lock("held-lock-user")
    held_lock.acquire()

    try:
        pop_lock("held-lock-user")
        contender_lock = get_lock("held-lock-user")
        contender_acquired = contender_lock.acquire(blocking=False)
        if contender_acquired:
            contender_lock.release()

        assert contender_lock is held_lock
        assert contender_acquired is False
    finally:
        held_lock.release()


def test_user_close_cannot_replace_a_looked_up_unacquired_lock(lock_registry):
    """Pin the lookup -> cleanup -> replacement lookup -> acquire race.

    The first operation has already received its lock but has not acquired it
    when cleanup runs. A second operation then looks up and acquires the lock.
    The first operation must still contend on that exact same object.
    """
    get_lock, pop_lock = lock_registry
    first_reference = get_lock("lookup-before-acquire-user")

    pop_lock("lookup-before-acquire-user")
    second_reference = get_lock("lookup-before-acquire-user")
    second_reference.acquire()
    try:
        first_acquired = first_reference.acquire(blocking=False)
        if first_acquired:
            first_reference.release()
    finally:
        second_reference.release()

    assert second_reference is first_reference
    assert first_acquired is False