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.8 KB
Raw Blame
"""Concurrent journal reads must use independent SQLite connections.

Release 1.10.7's shared StaticPool connection could corrupt fetched rows.
Exercise real ORM row processing on the production read-only accessor, so
dependency upgrades also cover the path implicated in the #6876 crash.
"""

import threading
from concurrent.futures import ThreadPoolExecutor

from sqlalchemy import create_engine
from sqlalchemy.orm import Session

from local_deep_research.journal_quality.db import JournalQualityDB
from local_deep_research.journal_quality.models import (
    JournalQualityBase,
    Source,
)


def test_concurrent_journal_reads_keep_connections_and_rows_isolated(
    tmp_path, monkeypatch
):
    path = tmp_path / "journal_quality.db"
    writer = create_engine(f"sqlite:///{path}")
    try:
        JournalQualityBase.metadata.create_all(writer)
        with Session(writer) as session:
            session.add_all(
                Source(
                    id=i,
                    name=f"Journal {i}",
                    name_lower=f"journal {i}",
                    h_index=i,
                    is_in_doaj=i % 2 == 0,
                    is_predatory=False,
                )
                for i in range(1, 65)
            )
            session.commit()
    finally:
        writer.dispose()

    db = JournalQualityDB()
    monkeypatch.setattr(db, "_resolve_db_path", lambda: path)
    # Warm the pool before the threads start. An unsafe StaticPool must
    # reuse its cached handle, rather than race to create several handles.
    with db.session() as session:
        assert session.query(Source).count() == 64
    workers = 12
    ready = threading.Barrier(workers)
    connections = []
    lock = threading.Lock()
    expected = [(i, f"Journal {i}", i, i % 2 == 0) for i in range(1, 65)]

    def read(worker):
        with db.session() as session:
            connection = session.connection().connection.dbapi_connection
            with lock:
                # Retain the objects so IDs cannot be reused after close.
                connections.append(connection)
            ready.wait(timeout=10)
            with lock:
                assert len({id(conn) for conn in connections}) == workers
            for _ in range(30):
                rows = session.query(Source).order_by(Source.id).all()
                assert [
                    (row.id, row.name, row.h_index, row.is_in_doaj)
                    for row in rows
                ] == expected
                row = session.query(Source).filter_by(id=worker + 1).first()
                assert row is not None and row.name == f"Journal {worker + 1}"

    try:
        with ThreadPoolExecutor(max_workers=workers) as pool:
            list(pool.map(read, range(workers)))
        assert len({id(connection) for connection in connections}) == workers
    finally:
        db.reset()