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.

fix(scheduler): scope document-scheduler sessions so no SQLite read snapshot spans the download loop (#6720)

Sca06c9921704
diff --git a/changelog.d/6720.bugfix.md b/changelog.d/6720.bugfix.md
new file mode 100644
index 0000000000..28e415c372
--- /dev/null
+++ b/changelog.d/6720.bugfix.md
@@ -0,0 +1,21 @@
+The document scheduler no longer holds a database session across its
+PDF download/extract loop. The pre-fix code kept one session open for the
+whole pass, and on this codebase's pysqlite configuration
+(`isolation_level=""`) that pinned two distinct lock-pain paths while
+multi-second downloads ran:
+
+ * Staged or uncommitted writes from the read session spanned the
+ network IO, so a competing writer (the rate-limit tracker, the
+ research queue, the UI) hit `database is locked` after the configured
+ `busy_timeout` (2s by default).
+ * Any explicit read transaction held a SHARED lock on the WAL until
+ commit/rollback, so `PRAGMA wal_checkpoint(TRUNCATE)` from a
+ competing connection blocked for the whole pass.
+
+Both surfaced as cascading `database is locked` and
+`Failed to persist rate limit estimate` warnings. The scheduler now uses
+three short scopes: a read session that loads the research and resource
+lists and closes before any network IO, a session-free download loop
+(each `DownloadService` call opens its own), and a short write session
+for the final `last_run` update. This removes the root cause rather than
+only widening the timeout.
diff --git a/src/local_deep_research/scheduler/background.py b/src/local_deep_research/scheduler/background.py
index 8a9c221da9..08e92a2ad3 100644
--- a/src/local_deep_research/scheduler/background.py
+++ b/src/local_deep_research/scheduler/background.py
@@ -1002,9 +1002,45 @@ class BackgroundJobScheduler:
# Need database session for queries and updates
from ..database.session_context import get_user_db_session
- from ..database.models.research import ResearchHistory
+ from ..database.models.research import (
+ ResearchHistory,
+ ResearchResource,
+ )
+ from ..research_library.utils import is_downloadable_url
from ..settings.manager import SettingsManager
+ # ------------------------------------------------------------------
+ # Transaction scope (root-cause fix for "database is locked")
+ # ------------------------------------------------------------------
+ # This method used to hold ONE session open across the entire
+ # download/extract loop. SQLite keeps a read snapshot open for the
+ # lifetime of a transaction, and a long-lived read snapshot pins the
+ # WAL so it cannot checkpoint -- which blocks every other writer
+ # (the rate-limit tracker, the research queue, the UI) for as long
+ # as the PDF downloads take. Production logs showed the symptom as
+ # cascading ``database is locked`` warnings plus
+ # ``Failed to persist rate limit estimate``.
+ #
+ # We now use three SHORT scopes instead:
+ #
+ # 1. read session -- settings, egress backstop, research list and
+ # per-research resource lists. Closed before
+ # the loop starts.
+ # 2. no session -- the download/extract loop. DownloadService
+ # opens (and closes) its own per-operation
+ # sessions, so this worker holds no SQLite
+ # transaction while network IO runs.
+ # 3. write session -- the final ``last_run`` update.
+ #
+ # Everything the loop needs is materialised as plain values in
+ # step 1, so no ORM instance has to lazily reload mid-loop (a lazy
+ # load would silently re-open a read transaction and reintroduce
+ # the problem).
+ # ------------------------------------------------------------------
+ research_to_process: list[dict] = []
+ resources_by_research: dict[str, list[tuple[int, str]]] = {}
+ user_settings_snapshot = None
+
with get_user_db_session(username, password) as db:
settings_manager = SettingsManager(db)
@@ -1014,15 +1050,23 @@ class BackgroundJobScheduler:
# fetches documents below. DownloadService's evaluate_url PEP
# still gates each fetch (primary); this restores defense-in-
# depth parity with an interactive run. @thread_cleanup clears
- # the context when this method returns.
+ # the context when this method returns. Thread-local, so it
+ # survives the read session closing below.
if not self._arm_egress_backstop(settings_manager, username):
return
- # Query for completed research since last run
+ # Query for completed research since last run. Select only the
+ # three columns the loop needs (id / title / completed_at) so
+ # nothing is bound to an ORM identity that could lazily reload
+ # after the session closes.
logger.debug(
f"[DOC_SCHEDULER] Querying for completed research since {last_run}"
)
- query = db.query(ResearchHistory).filter(
+ query = db.query(
+ ResearchHistory.id,
+ ResearchHistory.title,
+ ResearchHistory.completed_at,
+ ).filter(
ResearchHistory.status == ResearchStatus.COMPLETED,
ResearchHistory.completed_at.is_not(
None
@@ -1039,54 +1083,61 @@ class BackgroundJobScheduler:
ResearchHistory.completed_at.desc()
).limit(20)
- research_sessions = query.all()
+ research_rows = query.all()
+ research_to_process = [
+ {
+ "id": row.id,
+ "title": row.title,
+ "completed_at": row.completed_at,
+ }
+ for row in research_rows
+ ]
logger.debug(
- f"[DOC_SCHEDULER] Query executed, found {len(research_sessions)} sessions"
+ f"[DOC_SCHEDULER] Query executed, found {len(research_to_process)} sessions"
)
- if not research_sessions:
+ if not research_to_process:
logger.info(
f"[DOC_SCHEDULER] No new completed research sessions found for user {username}"
)
return
logger.info(
- f"[DOC_SCHEDULER] Found {len(research_sessions)} research sessions to process for {username}"
+ f"[DOC_SCHEDULER] Found {len(research_to_process)} research sessions to process for {username}"
)
# Log details of each research session
for i, research in enumerate(
- research_sessions[:5]
+ research_to_process[:5]
): # Log first 5 details
title_safe = (
- (research.title[:50] + "...")
- if research.title
+ (research["title"][:50] + "...")
+ if research["title"]
else "No title"
)
- completed_safe = (
- research.completed_at
- if research.completed_at
- else "No completion time"
- )
- logger.debug(
- f"[DOC_SCHEDULER] Session {i + 1}: id={research.id}, title={title_safe}, completed={completed_safe}"
- )
# Handle completed_at which might be a string or datetime
completed_at_obj = None
- if research.completed_at:
- if isinstance(research.completed_at, str):
+ if research["completed_at"]:
+ if isinstance(research["completed_at"], str):
try:
completed_at_obj = datetime.fromisoformat(
- research.completed_at.replace("Z", "+00:00")
+ research["completed_at"].replace(
+ "Z", "+00:00"
+ )
)
except (ValueError, TypeError, AttributeError):
completed_at_obj = None
else:
- completed_at_obj = research.completed_at
+ completed_at_obj = research["completed_at"]
logger.debug(
- f"[DOC_SCHEDULER] - completed_at type: {type(research.completed_at)}"
+ f"[DOC_SCHEDULER] Session {i + 1}: id={research['id']}, "
+ f"title={title_safe}, "
+ f"completed={completed_at_obj or 'No completion time'}"
+ )
+ logger.debug(
+ f"[DOC_SCHEDULER] - completed_at type: {type(research['completed_at'])}"
)
logger.debug(
f"[DOC_SCHEDULER] - completed_at timezone: {completed_at_obj.tzinfo if completed_at_obj else 'None'}"
@@ -1099,10 +1150,14 @@ class BackgroundJobScheduler:
# Capture a settings snapshot for this user/run so the
# DownloadService below can build an EgressContext and
# gate each per-resource URL. Without this the scheduler
- # would bypass policy entirely. Reuses the outer `db`
- # session (line 743) — get_settings_manager() in a
- # background thread must be passed a db_session
- # explicitly per the pre-commit thread-safety check.
+ # would bypass policy entirely. get_settings_manager() in a
+ # background thread must be passed a db_session explicitly
+ # per the pre-commit thread-safety check.
+ #
+ # Ordered BEFORE the resource prefetch on purpose: the
+ # snapshot is the policy gate, so a failure here must skip the
+ # work entirely rather than burn queries on resources we are
+ # about to abandon.
try:
user_settings_snapshot = (
settings_manager.get_settings_snapshot(strict=True)
@@ -1119,202 +1174,198 @@ class BackgroundJobScheduler:
)
return
- processed_count = 0
- for research in research_sessions:
- try:
- logger.info(
- f"[DOC_SCHEDULER] Processing research {research.id} for user {username}"
- )
+ # Materialise each research's downloadable resources as plain
+ # ``(id, url)`` tuples. ``research_id`` / ``url`` are selected
+ # as columns (not ORM entities) and filtered here, so the loop
+ # below needs no database access at all.
+ for research in research_to_process:
+ resource_rows = (
+ db.query(ResearchResource.id, ResearchResource.url)
+ .filter_by(research_id=research["id"])
+ .all()
+ )
+ resources_by_research[research["id"]] = [
+ (row.id, row.url)
+ for row in resource_rows
+ if is_downloadable_url(row.url)
+ ]
- # Set search context so rate limiting works in both
- # download_pdfs and extract_text paths
- from ..utilities.thread_context import (
- set_search_context,
- )
+ # ---- read session closed; loop below holds no SQLite transaction ----
- set_search_context(
- {
- "research_id": str(research.id),
- "username": username,
- "user_password": password,
- "research_phase": "document_scheduler",
- }
- )
+ from ..research_library.services.download_service import (
+ DownloadService,
+ )
+ from ..utilities.thread_context import set_search_context
- # Call actual processing APIs
- if settings.download_pdfs:
- logger.info(
- f"[DOC_SCHEDULER] Downloading PDFs for research {research.id}"
- )
- try:
- # Use the DownloadService to queue PDF downloads
- from ..research_library.services.download_service import (
- DownloadService,
- )
+ researches_processed = 0
+ for research in research_to_process:
+ try:
+ logger.info(
+ f"[DOC_SCHEDULER] Processing research {research['id']} for user {username}"
+ )
- with DownloadService(
- username,
- password,
- settings_snapshot=user_settings_snapshot,
- ) as download_service:
- queued_count = download_service.queue_research_downloads(
- research.id
- )
- logger.info(
- f"[DOC_SCHEDULER] Queued {queued_count} PDF downloads for research {research.id}"
- )
- except Exception as e:
- # Recover the shared thread-local session
- # before continuing — without rollback the
- # next phase (text extract / RAG) and the
- # post-loop last_run commit run on a
- # poisoned session (issue #3827).
- safe_rollback(db, "DOC_SCHEDULER PDF download")
- # ``password`` is in scope and was passed
- # into ``DownloadService``. Drop traceback
- # + redact str(e) to avoid leaking the
- # SQLCipher master password under
- # ``diagnose=True``.
- safe_msg = redact_secrets(str(e), password)
- logger.warning(
- f"[DOC_SCHEDULER] Failed to download PDFs for research {research.id}: {safe_msg}"
- )
+ # Set search context so rate limiting works in both
+ # download_pdfs and extract_text paths
+ set_search_context(
+ {
+ "research_id": str(research["id"]),
+ "username": username,
+ "user_password": password,
+ "research_phase": "document_scheduler",
+ }
+ )
- if settings.extract_text:
- logger.info(
- f"[DOC_SCHEDULER] Extracting text for research {research.id}"
- )
- try:
- # Use the DownloadService to extract text for all resources
- from ..research_library.services.download_service import (
- DownloadService,
+ # Call actual processing APIs
+ if settings.download_pdfs:
+ logger.info(
+ f"[DOC_SCHEDULER] Downloading PDFs for research {research['id']}"
+ )
+ try:
+ # Use the DownloadService to queue PDF downloads
+ with DownloadService(
+ username,
+ password,
+ settings_snapshot=user_settings_snapshot,
+ ) as download_service:
+ queued_count = (
+ download_service.queue_research_downloads(
+ research["id"]
+ )
)
- from ..database.models.research import (
- ResearchResource,
+ logger.info(
+ f"[DOC_SCHEDULER] Queued {queued_count} PDF downloads for research {research['id']}"
)
+ except Exception as e:
+ # ``password`` is in scope and was passed
+ # into ``DownloadService``. Drop traceback
+ # + redact str(e) to avoid leaking the
+ # SQLCipher master password under
+ # ``diagnose=True``.
+ safe_msg = redact_secrets(str(e), password)
+ logger.warning(
+ f"[DOC_SCHEDULER] Failed to download PDFs for research {research['id']}: {safe_msg}"
+ )
- from ..research_library.utils import (
- is_downloadable_url,
+ if settings.extract_text:
+ logger.info(
+ f"[DOC_SCHEDULER] Extracting text for research {research['id']}"
+ )
+ try:
+ # Use the DownloadService to extract text for all resources
+ with DownloadService(
+ username,
+ password,
+ settings_snapshot=user_settings_snapshot,
+ ) as download_service:
+ # Resources were materialised as plain
+ # ``(id, url)`` tuples while the read
+ # session was open, so this loop performs
+ # no database access of its own.
+ resources = resources_by_research.get(
+ research["id"], []
)
-
- with DownloadService(
- username,
- password,
- settings_snapshot=user_settings_snapshot,
- ) as download_service:
- # Get all resources for this research (reuse existing db session)
- all_resources = (
- db.query(ResearchResource)
- .filter_by(research_id=research.id)
- .all()
- )
- # Filter: only process downloadable resources (academic/PDF)
- resources = [
- r
- for r in all_resources
- if is_downloadable_url(r.url)
- ]
- processed_count = 0
- for resource in resources:
- # We need to pass the password to the download service
- # The DownloadService creates its own database sessions, so we need to ensure password is available
- try:
- success, error = (
- download_service.download_as_text(
- resource.id
- )
+ resources_processed = 0
+ for resource_id, _resource_url in resources:
+ # The DownloadService creates its own
+ # database sessions, so the password is
+ # passed through the constructor above.
+ try:
+ success, error = (
+ download_service.download_as_text(
+ resource_id
)
- if success:
- processed_count += 1
- logger.info(
- f"[DOC_SCHEDULER] Successfully extracted text for resource {resource.id}"
- )
- else:
- logger.warning(
- f"[DOC_SCHEDULER] Failed to extract text for resource {resource.id}: {error}"
- )
- except Exception as resource_error:
- # Roll back FIRST so the next
- # iteration's queries don't
- # cascade on a poisoned session
- # (issue #3827).
- safe_rollback(
- db,
- "DOC_SCHEDULER resource",
- )
- # ``password`` is in scope and
- # was passed into the enclosing
- # ``DownloadService``. Drop the
- # traceback chain + redact str(e)
- # to avoid leaking the SQLCipher
- # master password.
- safe_msg = redact_secrets(
- str(resource_error), password
+ )
+ if success:
+ resources_processed += 1
+ logger.info(
+ f"[DOC_SCHEDULER] Successfully extracted text for resource {resource_id}"
)
+ else:
logger.warning(
- f"[DOC_SCHEDULER] Error processing resource {resource.id}: {safe_msg}"
+ f"[DOC_SCHEDULER] Failed to extract text for resource {resource_id}: {error}"
)
- logger.info(
- f"[DOC_SCHEDULER] Text extraction completed for research {research.id}: {processed_count}/{len(resources)} resources processed"
- )
- except Exception as e:
- safe_rollback(
- db, "DOC_SCHEDULER text extraction"
- )
- # ``password`` is in scope from the outer
- # ``_process_user_documents`` retrieval —
- # same redact + warning pattern as the
- # inner handlers in this function.
- safe_msg = redact_secrets(str(e), password)
- logger.warning(
- f"[DOC_SCHEDULER] Failed to extract text for research {research.id}: {safe_msg}"
+ except Exception as resource_error:
+ # No shared session to recover here:
+ # each ``download_as_text`` call opens
+ # and closes its own session, so one
+ # resource's failure cannot poison the
+ # next iteration (issue #3827 is
+ # structurally closed by the scoped
+ # sessions rather than by a rollback).
+ # ``password`` is in scope and was
+ # passed into the enclosing
+ # ``DownloadService``. Drop the
+ # traceback chain + redact str(e) to
+ # avoid leaking the SQLCipher master
+ # password.
+ safe_msg = redact_secrets(
+ str(resource_error), password
+ )
+ logger.warning(
+ f"[DOC_SCHEDULER] Error processing resource {resource_id}: {safe_msg}"
+ )
+ logger.info(
+ f"[DOC_SCHEDULER] Text extraction completed for research {research['id']}: {resources_processed}/{len(resources)} resources processed"
)
+ except Exception as e:
+ # ``password`` is in scope from the outer
+ # ``_process_user_documents`` retrieval —
+ # same redact + warning pattern as the
+ # inner handlers in this function.
+ safe_msg = redact_secrets(str(e), password)
+ logger.warning(
+ f"[DOC_SCHEDULER] Failed to extract text for research {research['id']}: {safe_msg}"
+ )
- # NOTE: RAG indexing of research downloads used to live
- # here (the old ``if settings.generate_rag:`` block).
- # It has been retired — the unified
- # ``_reconcile_unindexed_documents`` reconciler now
- # indexes ALL unindexed documents (including research
- # downloads that have no DocumentCollection row yet) on
- # its own schedule, gated by ``generate_rag OR
- # sweep_library_collections``. The download_pdfs and
- # extract_text passes above remain here because they
- # produce the ``text_content`` the reconciler indexes.
-
- processed_count += 1
- logger.debug(
- f"[DOC_SCHEDULER] Successfully queued processing for research {research.id}"
- )
+ # NOTE: RAG indexing of research downloads used to live
+ # here (the old ``if settings.generate_rag:`` block).
+ # It has been retired — the unified
+ # ``_reconcile_unindexed_documents`` reconciler now
+ # indexes ALL unindexed documents (including research
+ # downloads that have no DocumentCollection row yet) on
+ # its own schedule, gated by ``generate_rag OR
+ # sweep_library_collections``. The download_pdfs and
+ # extract_text passes above remain here because they
+ # produce the ``text_content`` the reconciler indexes.
+
+ researches_processed += 1
+ logger.debug(
+ f"[DOC_SCHEDULER] Successfully queued processing for research {research['id']}"
+ )
- except Exception as e:
- safe_rollback(db, "DOC_SCHEDULER research")
- # ``password`` is in scope from the outer
- # ``_process_user_documents`` retrieval. Drop the
- # traceback chain and redact str(e).
- safe_msg = redact_secrets(str(e), password)
- logger.warning(
- f"[DOC_SCHEDULER] Error processing research {research.id} for user {username}: {safe_msg}"
- )
+ except Exception as e:
+ # ``password`` is in scope from the outer
+ # ``_process_user_documents`` retrieval. Drop the
+ # traceback chain and redact str(e).
+ safe_msg = redact_secrets(str(e), password)
+ logger.warning(
+ f"[DOC_SCHEDULER] Error processing research {research['id']} for user {username}: {safe_msg}"
+ )
- # Update last run time in user's settings.
- # Intentionally NOT wrapped in try/finally: if upstream setup
- # fails (DB open, SettingsManager init, initial query),
- # last_run should stay put so the next tick retries.
- # Advancing here would mask a persistent failure (corrupted
- # DB, wrong password). See closed PR #3288.
- current_time = datetime.now(UTC).isoformat()
- settings_manager.set_setting(
+ # Write session: only the last_run update lives here, so the
+ # write lock is held for the duration of a single settings row
+ # rather than across the whole download pass.
+ #
+ # Update last run time in user's settings.
+ # Intentionally NOT wrapped in try/finally: if upstream setup
+ # fails (DB open, SettingsManager init, initial query),
+ # last_run should stay put so the next tick retries.
+ # Advancing here would mask a persistent failure (corrupted
+ # DB, wrong password). See closed PR #3288.
+ current_time = datetime.now(UTC).isoformat()
+ with get_user_db_session(username, password) as write_db:
+ SettingsManager(write_db).set_setting(
"document_scheduler.last_run", current_time, commit=True
)
- logger.debug(
- f"[DOC_SCHEDULER] Updated last run time for {username} to {current_time}"
- )
+ logger.debug(
+ f"[DOC_SCHEDULER] Updated last run time for {username} to {current_time}"
+ )
- end_time = datetime.now(UTC)
- duration = (end_time - start_time).total_seconds()
- logger.info(
- f"[DOC_SCHEDULER] Completed document processing for user {username}: {processed_count} sessions processed in {duration:.2f}s"
- )
+ end_time = datetime.now(UTC)
+ duration = (end_time - start_time).total_seconds()
+ logger.info(
+ f"[DOC_SCHEDULER] Completed document processing for user {username}: {researches_processed} research runs processed in {duration:.2f}s"
+ )
except Exception as e:
# ``password`` is pre-declared as ``None`` at the top of the
diff --git a/tests/research_library/services/test_download_service_3827_fix.py b/tests/research_library/services/test_download_service_3827_fix.py
index 09e43ba70d..e0adab71f7 100644
--- a/tests/research_library/services/test_download_service_3827_fix.py
+++ b/tests/research_library/services/test_download_service_3827_fix.py
@@ -26,6 +26,7 @@ behaviour is exercised end-to-end.
import hashlib
import uuid
+from contextlib import contextmanager
from unittest.mock import MagicMock, patch
import pytest
@@ -720,17 +721,33 @@ def test_download_bulk_second_item_processes_after_first_download_raises():
# ---------------------------------------------------------------------------
-# Change 3: the resource-level except in `_process_user_documents` calls
-# db.rollback() at the TOP of the block. Drives the relevant path of the
-# scheduler with focused mocks and verifies the rollback fires.
+# Change 3 (rewritten for scoped sessions): ``_process_user_documents`` no
+# longer holds ONE session across the download loop. It reads everything it
+# needs in a short read session, runs the loop with NO held session (each
+# ``DownloadService`` call opens its own), then writes ``last_run`` in a
+# short write session.
+#
+# That is the root-cause fix for the production ``database is locked``
+# contention: a long-lived SQLite read snapshot pins the WAL and blocks
+# every other writer for as long as the PDF downloads take.
+#
+# The #3827 requirement (one bad resource must not poison the next) is now
+# structural rather than defensive -- there is no shared session to poison.
+# These tests pin both properties.
# ---------------------------------------------------------------------------
-def test_process_user_documents_rolls_back_on_resource_error():
- """When `download_as_text` raises while processing a resource inside
- `_process_user_documents`, the per-resource except handler must call
- `db.rollback()` so the next resource's queries don't cascade on a
- poisoned session.
+def test_process_user_documents_closes_read_session_before_downloading():
+ """The read session must be CLOSED before the first download starts.
+
+ This is the structural guarantee that fixes the ``database is locked``
+ contention: no SQLite read snapshot may be held while network IO runs.
+ Before the fix, a single session stayed open across the whole loop.
+
+ Also pins the #3827 requirement: a resource that raises must not stop
+ the next resource from being processed. That is now guaranteed because
+ ``download_as_text`` opens and closes its own session -- there is no
+ shared session for a failure to poison.
"""
from datetime import datetime, UTC
@@ -757,54 +774,62 @@ def test_process_user_documents_rolls_back_on_resource_error():
last_run="",
)
- # Build a minimal research session with two downloadable resources.
- research = MagicMock()
- research.id = "research-1"
- research.title = "T"
- research.completed_at = None
- resource_a = MagicMock()
- resource_a.id = 1
- resource_a.url = "https://example.com/a.pdf"
- resource_b = MagicMock()
- resource_b.id = 2
- resource_b.url = "https://example.com/b.pdf"
+ # A single research row exposing only the three columns the refactor
+ # selects, plus two downloadable resources.
+ research_row = MagicMock(id="research-1", title="T", completed_at=None)
+ resource_rows = [
+ MagicMock(id=1, url="https://example.com/a.pdf"),
+ MagicMock(id=2, url="https://example.com/b.pdf"),
+ ]
db = MagicMock()
research_query = MagicMock()
research_query.filter.return_value = research_query
research_query.order_by.return_value = research_query
research_query.limit.return_value = research_query
- research_query.all.return_value = [research]
+ research_query.all.return_value = [research_row]
resource_query = MagicMock()
resource_query.filter_by.return_value = resource_query
- resource_query.all.return_value = [resource_a, resource_b]
- # First db.query() = ResearchHistory query, second = ResearchResource.
+ resource_query.all.return_value = resource_rows
+ # Read session: ResearchHistory columns, then ResearchResource columns.
db.query.side_effect = [research_query, resource_query]
- @MagicMock
- def _ctx():
- pass
+ # Record the interleaving of session lifetime and download calls so we
+ # can assert ordering (the read session must close before the first
+ # download).
+ events: list[str] = []
from contextlib import contextmanager as _cm
@_cm
def fake_get_user_db_session(*a, **kw):
- yield db
+ events.append("session_open")
+ try:
+ yield db
+ finally:
+ events.append("session_close")
download_service = MagicMock()
download_service.__enter__ = MagicMock(return_value=download_service)
download_service.__exit__ = MagicMock(return_value=False)
- # First resource raises, second succeeds. With the rollback in place,
- # the second call must complete normally.
- download_service.download_as_text.side_effect = [
- IntegrityError("INSERT", {}, Exception("UNIQUE constraint failed")),
- (True, None),
- ]
+
+ # A single ``side_effect`` callable (not a list): the list form RETURNS
+ # its items rather than calling them, so a closure is the only way to
+ # both record ordering and selectively fail the first resource.
+ def _download_as_text(resource_id):
+ events.append(f"download:{resource_id}")
+ if resource_id == 1:
+ raise IntegrityError(
+ "INSERT", {}, Exception("UNIQUE constraint failed")
+ )
+ return (True, None)
+
+ download_service.download_as_text.side_effect = _download_as_text
settings_manager = MagicMock()
# The egress backstop aborts unless the strict snapshot is a real dict
# with a resolvable primary engine; supply one so processing reaches the
- # per-resource rollback path this test exercises.
+ # download loop this test exercises.
settings_manager.get_settings_snapshot.return_value = {
"search.tool": "searxng"
}
@@ -834,30 +859,45 @@ def test_process_user_documents_rolls_back_on_resource_error():
):
scheduler._process_user_documents("testuser")
- # The per-resource except at line ~921 must have called db.rollback()
- # so the second resource's call could proceed on a clean session.
- assert db.rollback.called, (
- "db.rollback() must fire in the resource-level except so the loop "
- "can continue without PendingRollbackError on the next iteration."
+ # Both resources were attempted: the first one's IntegrityError did not
+ # cascade into the second (#3827).
+ assert download_service.download_as_text.call_count == 2, (
+ "A failing resource must not prevent the next from being processed."
)
- # Both resources were attempted.
- assert download_service.download_as_text.call_count == 2
+ # The read session closed BEFORE the first download. This is the whole
+ # point of the refactor: no SQLite transaction is held during network IO.
+ first_close = events.index("session_close")
+ first_download = next(
+ i for i, e in enumerate(events) if e.startswith("download:")
+ )
+ assert first_close < first_download, (
+ "The read session must be closed before the first download so no "
+ f"SQLite read snapshot is held during network IO. Events: {events}"
+ )
+
+ # And the write session (the final last_run update) opens only AFTER the
+ # downloads, so the write lock is held for one settings row, not the
+ # whole pass.
+ last_download = max(
+ i for i, e in enumerate(events) if e.startswith("download:")
+ )
+ last_open = len(events) - 1 - events[::-1].index("session_open")
+ assert last_download < last_open, (
+ "The last_run write session must open after all downloads have "
+ f"finished. Events: {events}"
+ )
-# ---------------------------------------------------------------------------
-# Change 3 (continued): the text-extraction WRAPPER except at ~line 944 also
-# calls safe_rollback. Triggered when an exception escapes the inner
-# per-resource handling — e.g. the `db.query(ResearchResource).filter_by(...)`
-# call itself raises before the per-resource loop starts.
-# ---------------------------------------------------------------------------
+def test_process_user_documents_resource_prefetch_failure_stays_fail_closed():
+ """A database failure while materialising the resource list must not
+ advance ``last_run``.
-def test_process_user_documents_rolls_back_on_text_extraction_wrapper_error():
- """If the text-extraction block raises before the per-resource loop —
- e.g. the resources query itself fails — the WRAPPER except must roll back,
- otherwise the post-loop last_run commit runs on a poisoned session. (RAG
- indexing has moved to _reconcile_unindexed_documents, but text-extraction
- rollback is still required for the last_run commit.)
+ ``last_run`` advancing on a failed pass would silently skip that
+ research on every future tick. This pins the existing fail-closed
+ policy (see the ``Intentionally NOT wrapped in try/finally`` comment in
+ the implementation) for the prefetch failure path introduced by the
+ scoped-session refactor.
"""
from datetime import datetime, UTC
@@ -883,20 +923,16 @@ def test_process_user_documents_rolls_back_on_text_extraction_wrapper_error():
last_run="",
)
- research = MagicMock()
- research.id = "research-x"
- research.title = "T"
- research.completed_at = None
+ research_row = MagicMock(id="research-x", title="T", completed_at=None)
db = MagicMock()
research_query = MagicMock()
research_query.filter.return_value = research_query
research_query.order_by.return_value = research_query
research_query.limit.return_value = research_query
- research_query.all.return_value = [research]
- # Second db.query() call (for ResearchResource) — RAISES, which escapes
- # the inner per-resource try/except entirely and lands in the wrapper
- # at line ~944.
+ research_query.all.return_value = [research_row]
+ # The second query (ResearchResource columns) raises, so the failure
+ # escapes the read session and lands in the method-level except.
resource_query_failure = RuntimeError("simulated DB query failure")
db.query.side_effect = [research_query, resource_query_failure]
@@ -911,8 +947,6 @@ def test_process_user_documents_rolls_back_on_text_extraction_wrapper_error():
download_service.__exit__ = MagicMock(return_value=False)
settings_manager = MagicMock()
- # The egress backstop aborts unless the strict snapshot is a real dict
- # with a resolvable primary engine.
settings_manager.get_settings_snapshot.return_value = {
"search.tool": "searxng"
}
@@ -936,13 +970,22 @@ def test_process_user_documents_rolls_back_on_text_extraction_wrapper_error():
return_value=download_service,
),
):
+ # Must not raise: the method-level except contains the failure.
scheduler._process_user_documents("testuser")
- # The wrapper at line ~944 must have rolled back so any later code
- # (RAG, last_run commit) sees a clean session.
- assert db.rollback.called, (
- "db.rollback() must fire in the text-extraction wrapper except at "
- "line ~944 so subsequent phases run on a clean session."
+ # No download was attempted because the resource list never materialised.
+ download_service.download_as_text.assert_not_called()
+
+ # Fail closed: last_run must NOT advance, so the next tick retries this
+ # user rather than silently skipping the research forever.
+ last_run_calls = [
+ call
+ for call in settings_manager.set_setting.call_args_list
+ if call.args and call.args[0] == "document_scheduler.last_run"
+ ]
+ assert not last_run_calls, (
+ "last_run must not advance when the resource prefetch failed; "
+ f"saw {last_run_calls!r}."
)
@@ -1023,3 +1066,597 @@ def test_process_user_documents_no_longer_indexes_rag_inline():
# generate_rag alone no longer enables this pass: it short-circuits before
# opening a DB session (download/extract are both off).
mock_session.assert_not_called()
+
+
+# ---------------------------------------------------------------------------
+# Real-machinery contention test (reviewer ask #1 from PR #6720):
+# ---------------------------------------------------------------------------
+# Both earlier tests fake ``get_user_db_session`` with a do-nothing CM, which
+# pins call ordering but NOT the actual SQLite lock-release semantics. The
+# scoped-sessions fix relies on ``session_context.py``'s depth-0 rollback in
+# the thread-local session manager: exiting the outermost
+# ``get_user_db_session`` block on the scheduler thread must release the
+# SQLite SHARED lock that an explicit BEGIN / autobegin read transaction
+# holds, or competing writers (rate-limit tracker, research queue, UI) time
+# out with ``database is locked``.
+#
+# This test wires the REAL ``get_user_db_session`` machinery to a real
+# file-backed WAL SQLite database and, from a second connection inside the
+# mocked ``download_as_text`` side-effect, runs:
+#
+# * ``BEGIN IMMEDIATE; INSERT; COMMIT`` -- exercises the write lock
+# * ``PRAGMA wal_checkpoint(TRUNCATE)`` -- exercises the read-snapshot
+# blocker (a TRUNCATE blocks
+# on the longest-running read
+# transaction)
+#
+# Both must succeed (no ``database is locked``) BETWEEN downloads. On main
+# the read session is held for the whole loop, so the WAL checkpoint blocks
+# or the second INSERT times out under a short ``busy_timeout``. On this
+# branch the read session closes before the first download and the worker
+# holds no SQLite transaction while network IO runs.
+# ---------------------------------------------------------------------------
+
+
+def _build_wal_engine(tmp_path):
+ """Build a file-backed WAL SQLite engine with the app's metadata schema.
+
+ Real WAL mode (not :memory:) because the contention the fix removes is
+ only observable on a checkpointable database — :memory: has no WAL file
+ to checkpoint and would silently pass on main.
+ """
+ from sqlalchemy import event
+ from sqlalchemy.pool import QueuePool
+
+ db_path = tmp_path / "contention_test.db"
+ engine = create_engine(
+ f"sqlite:///{db_path}",
+ connect_args={"check_same_thread": False, "timeout": 30},
+ poolclass=QueuePool,
+ )
+
+ # WAL + the same pragmas the encrypted_db engine applies. Registered as
+ # a connect listener so every connection (including the one the
+ # scheduler's thread-local session checks out) gets them, not just the
+ # first.
+ @event.listens_for(engine, "connect")
+ def _enable_wal(dbapi, _connection_record):
+ cursor = dbapi.cursor()
+ cursor.execute("PRAGMA journal_mode = WAL")
+ cursor.execute("PRAGMA synchronous = NORMAL")
+ cursor.execute("PRAGMA foreign_keys = ON")
+ cursor.execute("PRAGMA busy_timeout = 5000")
+ cursor.close()
+
+ Base.metadata.create_all(engine)
+ return engine, db_path
+
+
+def _seed_research_and_resources(engine, n_resources):
+ """Insert one completed research row plus ``n_resources`` resource rows."""
+ from sqlalchemy import text as _text
+
+ Session = sessionmaker(bind=engine)
+ research_id = str(uuid.uuid4())
+ with Session() as session:
+ research = ResearchHistory(
+ id=research_id,
+ query="contention test",
+ mode="quick",
+ status="completed",
+ created_at="2026-09-21T00:00:00",
+ completed_at="2026-09-21T00:00:01",
+ title="Contention Test Research",
+ )
+ session.add(research)
+ session.flush()
+ for i in range(n_resources):
+ session.add(
+ ResearchResource(
+ research_id=research_id,
+ title=f"Resource {i}",
+ url=f"https://example.com/{i}.pdf",
+ source_type="academic",
+ created_at="2026-09-21T00:00:00",
+ )
+ )
+ session.commit()
+ # Probe table for the contention test. Created upfront (committed)
+ # so the read session's staged INSERT and the contender's INSERT
+ # are pure DML holding real locks, not DDL.
+ session.execute(
+ _text(
+ "CREATE TABLE IF NOT EXISTS _contention_probe ("
+ "id INTEGER PRIMARY KEY, label TEXT, ts TEXT)"
+ )
+ )
+ session.commit()
+ return research_id
+
+
+def test_download_loop_releases_read_snapshot_real_sqlite(tmp_path):
+ """Real-WAL contention probe: a competing BEGIN IMMEDIATE + INSERT +
+ COMMIT plus a WAL TRUNCATE checkpoint must BOTH succeed between
+ downloads on the scoped-sessions branch.
+
+ The read session holds an explicit staged transaction (SELECT +
+ uncommitted INSERT into ``_contention_probe``). If
+ ``session_context``'s depth-0 rollback regresses to a no-op, that
+ transaction survives into the download loop: the competing write
+ fails with ``database is locked`` and the checkpoint returns
+ ``busy=1``. Asserting the checkpoint ``busy`` column (not just
+ exceptions) is required — TRUNCATE reports contention via busy=1
+ without raising.
+ """
+ from local_deep_research.database.encrypted_db import db_manager
+ from local_deep_research.database.session_context import (
+ get_user_db_session,
+ )
+ from local_deep_research.scheduler.background import (
+ BackgroundJobScheduler,
+ DocumentSchedulerSettings,
+ )
+
+ engine, db_path = _build_wal_engine(tmp_path)
+ SessionFactory = sessionmaker(bind=engine)
+ _seed_research_and_resources(engine, n_resources=3)
+
+ def _contending_write() -> None:
+ """From a second connection: short-busy_timeout BEGIN IMMEDIATE +
+ INSERT + COMMIT. A leaked RESERVED lock (staged/uncommitted write)
+ from the scheduler thread surfaces as ``database is locked`` under
+ the 200ms busy_timeout."""
+ contender = SessionFactory()
+ try:
+ conn = contender.connection().connection.dbapi_connection
+ conn.execute("PRAGMA busy_timeout = 200")
+ conn.execute("BEGIN IMMEDIATE")
+ conn.execute(
+ "INSERT INTO _contention_probe (label, ts) "
+ "VALUES (1, datetime('now'))"
+ )
+ conn.execute("COMMIT")
+ finally:
+ contender.close()
+
+ def _contending_checkpoint() -> int:
+ """From a second connection: WAL TRUNCATE checkpoint. Returns the
+ ``busy`` column. A leaked transaction (SHARED read or RESERVED
+ write) makes TRUNCATE return ``busy=1`` instead of raising, so
+ callers must assert ``busy == 0`` — checking only for exceptions
+ silently passes while blocked."""
+ contender = SessionFactory()
+ try:
+ conn = contender.connection().connection.dbapi_connection
+ conn.execute("PRAGMA busy_timeout = 200")
+ # wal_checkpoint(TRUNCATE) is the assertion: it cannot
+ # truncate the WAL while a reader/writer transaction is open.
+ # The pragma returns (busy, log, checkpointed) rows; busy=1
+ # means blocked. It does NOT raise on contention.
+ rows = list(
+ conn.execute("PRAGMA wal_checkpoint(TRUNCATE)").fetchall()
+ )
+ return int(rows[0][0]) if rows else 0
+ finally:
+ contender.close()
+
+ # (kind, error, checkpoint_busy). write probes leave busy=None;
+ # checkpoint probes record the busy column (0 == released, 1 == blocked).
+ contention_attempts: list[tuple[str, str | None, int | None]] = []
+
+ # Track the ordering of session open/close and downloads so the
+ # failure surface is concrete on main (read session still open when
+ # the first download runs).
+ events: list[str] = []
+ # Proves the read session actually held a transaction before close
+ # (so the test is sensitive to a missing rollback) and that the
+ # outermost exit released it (pool connection returned).
+ held_proof: dict = {}
+ _is_first_session = [True]
+
+ @contextmanager
+ def _tracking_session_cm(username, password):
+ from sqlalchemy import text as _text
+
+ events.append("session_open")
+ with get_user_db_session(username, password) as session:
+ if _is_first_session[0]:
+ _is_first_session[0] = False
+ # Explicit held transaction: a SELECT (SHARED read mark)
+ # plus a staged, uncommitted INSERT (RESERVED write lock).
+ # With the app's pysqlite ``isolation_level=""`` a pure
+ # SELECT holds no WAL mark — implicit BEGIN fires only
+ # before DML — so the staged write is what reproduces
+ # production pain (competing writer gets
+ # ``database is locked``; checkpoint returns busy=1).
+ # If session_context's depth-0 rollback regresses to a
+ # no-op, this transaction survives into the download loop
+ # and both probes below fail.
+ session.execute(
+ _text("SELECT id FROM research_history LIMIT 1")
+ )
+ session.execute(
+ _text(
+ "INSERT INTO _contention_probe (label, ts) "
+ "VALUES ('staged', datetime('now'))"
+ )
+ )
+ assert session.in_transaction(), (
+ "read session must hold a transaction to prove release"
+ )
+ held_proof["was_in_transaction"] = True
+ yield session
+ events.append("session_close")
+ if "was_in_transaction" in held_proof and "released" not in held_proof:
+ from local_deep_research.database.thread_local_session import (
+ thread_session_manager,
+ )
+
+ cached = thread_session_manager.get_current_session()
+ # After the outermost exit the depth-0 rollback must have
+ # released the transaction and returned the pooled connection.
+ # A no-op rollback leaves the cached session in-transaction
+ # with its connection still checked out.
+ still_in_txn = bool(
+ cached.in_transaction() if cached is not None else False
+ )
+ held_proof["released"] = not still_in_txn
+ held_proof["pool_checkedout"] = engine.pool.checkedout()
+
+ def _download_as_text(resource_id):
+ events.append(f"download:{resource_id}")
+ # The probe: from a SECOND connection, write and checkpoint.
+ # If the scheduler thread is still holding the staged transaction,
+ # the write raises ``database is locked`` and the checkpoint
+ # returns busy=1.
+ try:
+ _contending_write()
+ contention_attempts.append(("write", None, None))
+ except Exception as exc: # noqa: BLE001 - capture SQLite error
+ contention_attempts.append(("write", str(exc), None))
+ try:
+ busy = _contending_checkpoint()
+ if busy != 0:
+ contention_attempts.append(
+ ("checkpoint", f"wal_checkpoint busy={busy}", busy)
+ )
+ else:
+ contention_attempts.append(("checkpoint", None, busy))
+ except Exception as exc: # noqa: BLE001 - capture SQLite error
+ contention_attempts.append(("checkpoint", str(exc), None))
+ return (True, None)
+
+ BackgroundJobScheduler._instance = None
+ with patch("local_deep_research.scheduler.background.BackgroundScheduler"):
+ scheduler = BackgroundJobScheduler()
+ scheduler.user_sessions["testuser"] = {
+ "scheduled_jobs": set(),
+ "last_activity": None,
+ }
+ scheduler._credential_store.store("testuser", "testpass")
+
+ settings = DocumentSchedulerSettings(
+ download_pdfs=False,
+ extract_text=True,
+ generate_rag=False,
+ last_run="",
+ )
+
+ download_service_mock = MagicMock()
+ download_service_mock.__enter__ = MagicMock(
+ return_value=download_service_mock
+ )
+ download_service_mock.__exit__ = MagicMock(return_value=False)
+ download_service_mock.download_as_text.side_effect = _download_as_text
+ download_service_mock.queue_research_downloads.return_value = 0
+
+ settings_manager = MagicMock()
+ settings_manager.get_settings_snapshot.return_value = {
+ "search.tool": "searxng"
+ }
+
+ with (
+ patch.object(
+ scheduler,
+ "_get_document_scheduler_settings",
+ return_value=settings,
+ ),
+ patch.object(db_manager, "has_encryption", False),
+ patch.object(
+ db_manager,
+ "open_user_database",
+ return_value=engine,
+ ),
+ patch.object(
+ db_manager,
+ "create_thread_safe_session_for_metrics",
+ side_effect=lambda _username, _password: SessionFactory(),
+ ),
+ patch(
+ "local_deep_research.database.session_context.get_user_db_session",
+ side_effect=_tracking_session_cm,
+ ),
+ patch(
+ "local_deep_research.settings.manager.SettingsManager",
+ return_value=settings_manager,
+ ),
+ patch(
+ "local_deep_research.research_library.services.download_service.DownloadService",
+ return_value=download_service_mock,
+ ),
+ patch(
+ "local_deep_research.research_library.utils.is_downloadable_url",
+ return_value=True,
+ ),
+ ):
+ scheduler._process_user_documents("testuser")
+
+ # All three resources were processed.
+ download_events = [e for e in events if e.startswith("download:")]
+ assert download_events == [
+ "download:1",
+ "download:2",
+ "download:3",
+ ], f"downloads must run for every resource; got {download_events}"
+
+ # Between every download the WAL allowed a competing writer AND a
+ # checkpoint. If the read session leaked into the loop (pre-fix
+ # whole-pass session, or a depth-0 rollback regression), the write
+ # carries ``database is locked`` and the checkpoint returns busy=1
+ # under the 200ms busy_timeout.
+ assert contention_attempts, "no contention probes were run"
+ assert held_proof.get("was_in_transaction") is True, (
+ "read session never held a transaction — the test would pass "
+ "even with rollback disabled and proves nothing"
+ )
+ assert held_proof.get("released") is True, (
+ "read session transaction survived its outermost close "
+ f"(pool checkedout={held_proof.get('pool_checkedout')}); "
+ "session_context depth-0 rollback did not release it"
+ )
+ for kind, err, busy in contention_attempts:
+ assert err is None, (
+ f"Contention probe failed for {kind}: {err!r}. The "
+ "scoped-sessions fix relies on the depth-0 rollback in "
+ "session_context.get_user_db_session releasing the SQLite "
+ "transaction between downloads; a regression there "
+ "would let the whole pass hold the WAL and block any "
+ "competing writer (production: rate-limit tracker, "
+ "research queue, UI)."
+ )
+ if kind == "checkpoint":
+ assert busy == 0, (
+ f"WAL checkpoint blocked (busy={busy}): a read/write "
+ "transaction survived into the download loop"
+ )
+
+ # And the read session actually closed before the first download —
+ # the structural contract the test pins. Two ``session_open`` /
+ # ``session_close`` pairs are expected: the read session for
+ # research + resources, and the final write session for the
+ # ``last_run`` update.
+ open_indices = [i for i, e in enumerate(events) if e == "session_open"]
+ close_indices = [i for i, e in enumerate(events) if e == "session_close"]
+ assert len(open_indices) >= 2 and len(close_indices) >= 2, (
+ f"expected at least 2 outermost session open/close pairs; "
+ f"events={events}"
+ )
+ first_close = close_indices[0]
+ first_download = next(
+ i for i, e in enumerate(events) if e.startswith("download:")
+ )
+ assert first_close < first_download, (
+ "The read session must be closed before the first download so "
+ f"no SQLite read snapshot is held during network IO. Events: {events}"
+ )
+ assert open_indices[1] > close_indices[0], (
+ "The write session must open AFTER the read session closes. "
+ f"Events: {events}"
+ )
+
+ # Cleanup: release the thread-local session (which is bound to this
+ # test's engine) so later tests don't reuse a disposed engine, then
+ # dispose the engine so the WAL/SHM handles are released.
+ try:
+ from local_deep_research.database.thread_local_session import (
+ thread_session_manager,
+ )
+
+ thread_session_manager.cleanup_thread()
+ except Exception:
+ pass
+ try:
+ from local_deep_research.security.egress.audit_hook import (
+ clear_active_context as _clear_ctx,
+ )
+
+ _clear_ctx()
+ except Exception:
+ pass
+ engine.dispose()
+ if db_path.exists():
+ db_path.unlink()
+
+
+# ---------------------------------------------------------------------------
+# Egress-backstop survival test (reviewer ask #2 from PR #6720):
+# ---------------------------------------------------------------------------
+# The scoped-sessions refactor asserts that the PEP-578 audit-hook egress
+# backstop survives the read session closing. With no session in the loop
+# the backstop becomes the SECONDARY net that still gates outbound HTTP from
+# this worker thread — losing it would mean scheduled downloads could
+# egress to a host the user's policy forbids, while the primary PEPs in
+# DownloadService still block. The earlier tests either mocked
+# ``_arm_egress_backstop`` entirely or wrapped it in the same ``with`` block
+# as the queries, so a regression that cleared the context at the read
+# session close (the most natural mistake) was invisible.
+#
+# This test calls the REAL ``_arm_egress_backstop`` and asserts
+# ``get_active_context()`` is non-None inside the download loop's
+# ``download_as_text`` side-effect — the security-relevant half of
+# "session-free loop".
+# ---------------------------------------------------------------------------
+
+
+def test_process_user_documents_egress_backstop_survives_read_session_close(
+ tmp_path,
+):
+ """The egress audit-hook context armed in the read session must still
+ be active when ``download_as_text`` runs (after the read session has
+ closed). A regression that cleared the context at session close would
+ silently disarm the secondary net for every scheduled download.
+ """
+ from datetime import datetime, UTC
+
+ from local_deep_research.scheduler.background import (
+ BackgroundJobScheduler,
+ DocumentSchedulerSettings,
+ )
+ from local_deep_research.security.egress.audit_hook import (
+ clear_active_context,
+ get_active_context,
+ )
+
+ # Build a scheduler with the standard test setup but allow the REAL
+ # _arm_egress_backstop to run, so we exercise the thread-local
+ # set_active_context() wiring.
+ BackgroundJobScheduler._instance = None
+ with patch("local_deep_research.scheduler.background.BackgroundScheduler"):
+ scheduler = BackgroundJobScheduler()
+ scheduler.user_sessions["testuser"] = {
+ "scheduled_jobs": set(),
+ "last_activity": datetime.now(UTC),
+ }
+ scheduler._credential_store.store("testuser", "testpass")
+
+ settings = DocumentSchedulerSettings(
+ download_pdfs=False,
+ extract_text=True,
+ generate_rag=False,
+ last_run="",
+ )
+
+ # Track what get_active_context() returns from inside the download
+ # loop. After the read session closes the context MUST still be set
+ # for the secondary net to remain active.
+ context_inside_loop: list[bool] = []
+
+ def _download_as_text(resource_id):
+ from local_deep_research.security.egress.audit_hook import (
+ get_active_context as _get,
+ )
+
+ context_inside_loop.append(_get() is not None)
+ return (True, None)
+
+ # Standard MagicMock session — the contention probe above already
+ # proved the real session machinery's release contract; this test is
+ # about egress-context lifetime, not SQLite locks.
+ from contextlib import contextmanager as _cm
+
+ db = MagicMock()
+ research_row = MagicMock(id="r-1", title="T", completed_at=None)
+ resource_rows = [
+ MagicMock(id=1, url="https://example.com/a.pdf"),
+ MagicMock(id=2, url="https://example.com/b.pdf"),
+ ]
+ research_query = MagicMock()
+ research_query.filter.return_value = research_query
+ research_query.order_by.return_value = research_query
+ research_query.limit.return_value = research_query
+ research_query.all.return_value = [research_row]
+ resource_query = MagicMock()
+ resource_query.filter_by.return_value = resource_query
+ resource_query.all.return_value = resource_rows
+ db.query.side_effect = [research_query, resource_query]
+
+ @_cm
+ def fake_get_user_db_session(*a, **kw):
+ yield db
+
+ download_service_mock = MagicMock()
+ download_service_mock.__enter__ = MagicMock(
+ return_value=download_service_mock
+ )
+ download_service_mock.__exit__ = MagicMock(return_value=False)
+ download_service_mock.download_as_text.side_effect = _download_as_text
+
+ # SettingsManager must hand back a snapshot with a resolvable primary
+ # engine — resolve_run_primary_engine is the single source of truth
+ # and raises on missing search.tool, which the outer except in
+ # _arm_egress_backstop catches and translates into a False return
+ # (skipping the work).
+ settings_manager = MagicMock()
+ settings_manager.get_settings_snapshot.return_value = {
+ "search.tool": "searxng"
+ }
+
+ # Defence in depth: even if a prior test leaked an active context
+ # into this thread, start clean.
+ clear_active_context()
+ # Captured immediately after _process_user_documents returns but
+ # BEFORE this test's own teardown clears the thread-local. If the
+ # production @thread_cleanup regresses to a no-op, this stays
+ # non-None and the test fails. Checking get_active_context() only
+ # after the finally below would mask that regression because the
+ # test itself would have cleared it.
+ context_after_production_cleanup = None
+ try:
+ with (
+ patch.object(
+ scheduler,
+ "_get_document_scheduler_settings",
+ return_value=settings,
+ ),
+ patch(
+ "local_deep_research.database.session_context.get_user_db_session",
+ side_effect=fake_get_user_db_session,
+ ),
+ patch(
+ "local_deep_research.settings.manager.SettingsManager",
+ return_value=settings_manager,
+ ),
+ patch(
+ "local_deep_research.research_library.services.download_service.DownloadService",
+ return_value=download_service_mock,
+ ),
+ patch(
+ "local_deep_research.research_library.utils.is_downloadable_url",
+ return_value=True,
+ ),
+ ):
+ scheduler._process_user_documents("testuser")
+ # Production @thread_cleanup runs on exit of
+ # _process_user_documents, so by the time we are here the
+ # egress context must already be cleared by production —
+ # not by this test's finally.
+ context_after_production_cleanup = get_active_context()
+ finally:
+ clear_active_context()
+
+ # Both downloads observed the egress backstop as active — proving the
+ # thread-local context survived the read session close.
+ assert context_inside_loop, (
+ "download_as_text was never called; cannot verify egress survival"
+ )
+ assert all(context_inside_loop), (
+ "The PEP-578 audit-hook egress context was NOT active inside "
+ "the download loop. The scoped-sessions fix relies on the "
+ "context surviving the read session close (thread-local, so "
+ "the session close has no effect); a regression that cleared "
+ "the context at session close would silently disarm the "
+ "secondary egress net for every scheduled download."
+ f" Observed: {context_inside_loop}"
+ )
+
+ # And the @thread_cleanup on _process_user_documents cleared the
+ # backstop on exit, so the next worker on this thread doesn't
+ # inherit the policy from a previous run. Assert the value captured
+ # BEFORE this test's own finally — asserting get_active_context()
+ # here would pass even if production never cleared, because the
+ # finally above already did.
+ assert context_after_production_cleanup is None, (
+ "@thread_cleanup must clear the egress context on exit so the "
+ "next worker on this thread doesn't inherit the previous user's "
+ "policy."
+ )