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.

3.8 KB
Raw Blame
"""In-process serialization of native SQLCipher open and close.

The SQLite 3.51.1 bundled in ``sqlcipher3-binary`` 0.6.0 deadlocks when one
thread opens a WAL-mode database file while another closes a connection to
the same file. The driver keeps the GIL while it closes, so the deadlock
froze a whole pytest worker in CI
(``test_metrics_entry_wrong_password_storm_racing_close_and_reopen``).
The shared ``sqlcipher_compat.connect_sqlcipher`` lifecycle guard serializes
native open and close on affected builds, including connection finalization.

The stress test runs in a subprocess with a hard timeout: an in-process
deadlock that holds the GIL cannot be bounded by the test itself.
"""

import os
import subprocess
import sys
import textwrap
from pathlib import Path

import pytest

from local_deep_research.database import sqlcipher_utils


SRC_DIR = Path(sqlcipher_utils.__file__).resolve().parents[2]
STRESS_TIMEOUT_S = 45

_STRESS_SCRIPT = textwrap.dedent(
    """
    import sys, tempfile, threading
    from pathlib import Path
    from local_deep_research.database.sqlcipher_utils import (
        create_sqlcipher_connection,
    )

    good, bad = "ab" * 32, "cd" * 32
    db = Path(tempfile.mkdtemp()) / "stress.db"
    conn = create_sqlcipher_connection(db, hex_key=good, creation_mode=True)
    conn.execute("CREATE TABLE t (a INTEGER)")
    conn.execute("INSERT INTO t VALUES (1)")
    conn.commit()
    conn.close()

    iterations = int(sys.argv[1])
    errors = []

    def right_key():
        try:
            for _ in range(iterations):
                c = create_sqlcipher_connection(
                    db, hex_key=good,
                    connect_kwargs={"check_same_thread": False},
                )
                c.execute("SELECT a FROM t").fetchall()
                c.close()
        except Exception as exc:
            errors.append(repr(exc))

    def wrong_key():
        try:
            for _ in range(iterations):
                try:
                    create_sqlcipher_connection(
                        db, hex_key=bad,
                        connect_kwargs={"check_same_thread": False},
                    )
                except Exception:  # wrong key: DatabaseError or ValueError
                    pass
                else:
                    errors.append("wrong key opened the database")
        except Exception as exc:
            errors.append(repr(exc))

    threads = [
        threading.Thread(target=right_key),
        threading.Thread(target=right_key),
        threading.Thread(target=wrong_key),
    ]
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    print("ERRORS" if errors else "OK", errors, flush=True)
    """
)


def test_concurrent_open_and_close_of_one_file_does_not_deadlock(tmp_path):
    """Three threads open and close the same WAL database (two with the
    right key, one with a wrong key). Without the lifecycle guard this
    freezes the process within the first few hundred cycles."""
    pytest.importorskip("sqlcipher3", reason="requires SQLCipher")
    script = tmp_path / "stress.py"
    script.write_text(_STRESS_SCRIPT)
    env = dict(os.environ)
    env["PYTHONPATH"] = os.pathsep.join(
        [str(SRC_DIR), env.get("PYTHONPATH", "")]
    ).rstrip(os.pathsep)
    try:
        result = subprocess.run(
            [sys.executable, str(script), "400"],
            capture_output=True,
            text=True,
            timeout=STRESS_TIMEOUT_S,
            env=env,
            cwd=tmp_path,
        )
    except subprocess.TimeoutExpired:
        pytest.fail(
            "concurrent open/close of one SQLCipher file deadlocked "
            f"(no exit within {STRESS_TIMEOUT_S}s)"
        )
    assert result.returncode == 0, result.stderr[-2000:]
    assert result.stdout.strip().startswith("OK"), result.stdout[-2000:]