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(library): atomically claim download-queue rows in bulk download (#5081)

Ehsanb9e6197cd60b
diff --git a/changelog.d/4691.bugfix.md b/changelog.d/4691.bugfix.md
new file mode 100644
index 0000000000..90adb57b12
--- /dev/null
+++ b/changelog.d/4691.bugfix.md
@@ -0,0 +1 @@
+Bulk downloads no longer double-process a queued item when two `download_bulk` requests run at once (the user clicks Download twice, or a second bulk run starts before the first finishes). Each queue row is now atomically claimed (PENDING to PROCESSING) before it is downloaded, so of the requests that go through the download queue (`download-bulk` and `download-source`), exactly one handles each row. Every path that resets a queued row back to PENDING (the retry pass at the start of every bulk run, `queue-all-undownloaded`, and `download-source`) now leaves a row another run is still downloading alone, and `download-source` reports such a row as already in progress instead of starting a second download of it. A claim is released back to PENDING if the download raises or if the post-claim resource lookup fails, and rows that finish without recording their own status (text-only extraction, and resources already downloaded) are completed rather than left stuck in PROCESSING.
diff --git a/src/local_deep_research/research_library/routes/library_routes.py b/src/local_deep_research/research_library/routes/library_routes.py
index a73c5c3993..ed3245c2cb 100644
--- a/src/local_deep_research/research_library/routes/library_routes.py
+++ b/src/local_deep_research/research_library/routes/library_routes.py
@@ -9,6 +9,7 @@ Provides web endpoints for:
import json
import math
+from datetime import UTC, datetime
from io import BytesIO
from pathlib import Path
from flask import (
@@ -733,6 +734,82 @@ def download_research_pdfs(research_id):
return jsonify({"success": True, "queued": queued})
+def _claim_download_queue_item(db_session, queue_item_id):
+ """Atomically claim a PENDING download-queue row before processing it.
+
+ Flip the row from PENDING to PROCESSING in a single conditional UPDATE and
+ return True only when this caller won the claim. Two concurrent
+ ``download_bulk`` streams for the same user (the user clicks Download
+ twice, or a second bulk run starts before the first finishes) both read
+ the same PENDING rows and, with no claim, both call ``download_resource``
+ for every row (issue #4691). The row-count guard makes the
+ PENDING -> PROCESSING transition the single point where exactly one stream
+ wins each row; the loser sees 0 updated rows and skips it.
+ """
+ claimed = (
+ db_session.query(LibraryDownloadQueue)
+ .filter_by(id=queue_item_id, status=DocumentStatus.PENDING)
+ .update(
+ {LibraryDownloadQueue.status: DocumentStatus.PROCESSING},
+ synchronize_session=False,
+ )
+ )
+ db_session.commit()
+ return claimed == 1
+
+
+def _release_download_queue_item(db_session, queue_item_id):
+ """Return a claimed row from PROCESSING back to PENDING so it stays
+ retryable when the download raised before a terminal COMPLETED/FAILED
+ status was recorded (issue #4691), matching the pre-fix behaviour where an
+ errored row was left PENDING. Scoped to PROCESSING so it only ever undoes
+ this stream's own claim.
+ """
+ db_session.query(LibraryDownloadQueue).filter_by(
+ id=queue_item_id, status=DocumentStatus.PROCESSING
+ ).update(
+ {LibraryDownloadQueue.status: DocumentStatus.PENDING},
+ synchronize_session=False,
+ )
+ db_session.commit()
+
+
+def _finalize_download_queue_item(db_session, queue_item_id, success):
+ """Record the outcome of a claimed row that returned without raising.
+
+ ``download_resource`` writes a terminal COMPLETED/FAILED status itself for
+ the rows it actually downloads, and this is a no-op for those: the row is
+ no longer PROCESSING, so the guard matches nothing and pdf-mode behaviour
+ is unchanged. It fires only on the paths that otherwise strand a claim
+ (issue #4691):
+
+ - ``mode="text_only"``: ``download_as_text`` and its call tree never touch
+ ``LibraryDownloadQueue``, so a successful extraction would leave the row
+ PROCESSING forever. Nothing in ``src/`` reaps PROCESSING rows, and a
+ successful extraction also writes a COMPLETED Document, which blocks both
+ the pre-pass reset and ``queue_all_undownloaded``'s outer-join filter.
+ - ``download_resource``'s already-downloaded early return, which reports
+ success before reaching its own queue update.
+
+ Pre-fix, both paths left the row PENDING and merely re-scanned it, so
+ failure returns to PENDING rather than FAILED to keep that retryability.
+ """
+ db_session.query(LibraryDownloadQueue).filter_by(
+ id=queue_item_id, status=DocumentStatus.PROCESSING
+ ).update(
+ {
+ LibraryDownloadQueue.status: (
+ DocumentStatus.COMPLETED if success else DocumentStatus.PENDING
+ ),
+ LibraryDownloadQueue.completed_at: (
+ datetime.now(UTC) if success else None
+ ),
+ },
+ synchronize_session=False,
+ )
+ db_session.commit()
+
+
@library_bp.route("/api/download-bulk", methods=["POST"])
@login_required
@require_json_body()
@@ -845,29 +922,48 @@ def download_bulk():
# Process each queued item
for queue_item in queue_items:
+ # Atomically claim this row before downloading so a
+ # second concurrent download_bulk stream can't also
+ # process it (issue #4691). A row already claimed by
+ # another stream returns 0 updated rows; skip it.
+ queue_item_id = queue_item.id
+ if not _claim_download_queue_item(
+ session, queue_item_id
+ ):
+ continue
+
current += 1
progress = (
int((current / total) * 100) if total > 0 else 100
)
- # Get resource info
- resource = session.query(ResearchResource).get(
- queue_item.resource_id
- )
- file_name = (
- resource.title[:50]
- if resource
- else f"document_{current}.pdf"
- )
-
# Attempt actual download with error handling
skip_reason = None
status = "skipped" # Default to skipped
success = False
error_msg = None
+ # Everything from the resource lookup onward runs
+ # inside the try below, so file_name needs a value
+ # even if that block raises before setting one.
+ file_name = f"document_{current}.pdf"
try:
+ # Get resource info. This sits INSIDE the try
+ # because the claim above is already committed as
+ # PROCESSING: any raise between the claim and this
+ # handler would leave the row that way forever,
+ # since nothing in src/ reaps PROCESSING rows and
+ # every reset path now refuses to touch them
+ # (issue #4691). ResearchResource.title is
+ # nullable, so resource.title[:50] raises TypeError
+ # on a resource whose title was never populated.
+ resource = session.query(ResearchResource).get(
+ queue_item.resource_id
+ )
+ if resource and resource.title:
+ file_name = resource.title[:50]
+
logger.debug(
f"Attempting {'PDF download' if mode == 'pdf' else 'text extraction'} for resource {queue_item.resource_id}"
)
@@ -889,6 +985,14 @@ def download_bulk():
success = result
skip_reason = None
+ # Finalize the claim for the paths that don't
+ # record a terminal status themselves (text_only,
+ # and download_resource's already-downloaded early
+ # return); a no-op when they already did (#4691).
+ _finalize_download_queue_item(
+ session, queue_item_id, success
+ )
+
status = "success" if success else "skipped"
if skip_reason and not success:
error_msg = skip_reason
@@ -908,6 +1012,11 @@ def download_bulk():
# item before this handler ever runs again
# (issue #3827).
safe_rollback(session, "SSE download")
+ # Release the claim so a later run can retry this
+ # row: the download raised before
+ # download_resource recorded a terminal status
+ # (issue #4691).
+ _release_download_queue_item(session, queue_item_id)
# Log error but continue processing
error_msg = str(e)
error_type = type(e).__name__
@@ -1120,21 +1229,43 @@ def queue_all_undownloaded():
)
if existing_queue:
- # If it exists but isn't pending, reset it to pending
- if existing_queue.status != DocumentStatus.PENDING:
- existing_queue.status = DocumentStatus.PENDING
- existing_queue.completed_at = None
- queued_count += 1
- research_ids.add(resource.research_id)
+ # Reset a non-pending row back to PENDING so it is retried,
+ # but never one a concurrent download stream has claimed:
+ # PROCESSING satisfies `!= PENDING`, so an unguarded reset
+ # un-claims an in-flight row and lets a second stream
+ # re-download the same resource (issue #4691). The query above
+ # selects in-flight rows, since a download that has not
+ # finished has no completed Document yet. The guard lives
+ # inside the UPDATE, not in an `if` above it, so the check and
+ # the write cannot straddle another stream's claim.
+ reset = (
+ db_session.query(LibraryDownloadQueue)
+ .filter(
+ LibraryDownloadQueue.id == existing_queue.id,
+ LibraryDownloadQueue.status != DocumentStatus.PENDING,
+ LibraryDownloadQueue.status
+ != DocumentStatus.PROCESSING,
+ )
+ .update(
+ {
+ LibraryDownloadQueue.status: DocumentStatus.PENDING,
+ LibraryDownloadQueue.completed_at: None,
+ },
+ synchronize_session=False,
+ )
+ )
+ # Counted either way, matching the pre-fix behaviour: a row
+ # already PENDING, a row just reset, and a row another stream
+ # is downloading are all "queued" to the caller.
+ queued_count += 1
+ research_ids.add(resource.research_id)
+ if reset:
logger.debug(
f"Reset queue entry for resource {resource.id} to pending"
)
else:
- # Already pending, still count it
- queued_count += 1
- research_ids.add(resource.research_id)
logger.debug(
- f"Resource {resource.id} already pending in queue"
+ f"Resource {resource.id} already pending or in flight"
)
else:
# Add new entry to queue
@@ -1334,19 +1465,67 @@ def download_source():
status=DocumentStatus.PENDING,
)
db_session.add(queue_entry)
+ db_session.commit()
else:
- queue_entry.status = DocumentStatus.PENDING
- queue_entry.priority = 1
+ # Reset the row so this manual download can claim it, but never
+ # one a concurrent stream is already downloading: the reset is
+ # followed immediately by a download in this request thread, so
+ # un-claiming an in-flight row starts a second download of the
+ # same resource (issue #4691). The already-downloaded early return
+ # above cannot prevent that, because a download still in flight
+ # has not written a completed Document yet.
+ reset = (
+ db_session.query(LibraryDownloadQueue)
+ .filter(
+ LibraryDownloadQueue.id == queue_entry.id,
+ LibraryDownloadQueue.status != DocumentStatus.PROCESSING,
+ )
+ .update(
+ {
+ LibraryDownloadQueue.status: DocumentStatus.PENDING,
+ LibraryDownloadQueue.priority: 1,
+ },
+ synchronize_session=False,
+ )
+ )
+ db_session.commit()
+ if not reset:
+ return jsonify(
+ {
+ "success": False,
+ "message": "Download already in progress",
+ }
+ ), 409
- db_session.commit()
+ queue_item_id = queue_entry.id
+
+ # Claim the row before downloading, exactly as download_bulk does, so
+ # a bulk stream that reaches this row between the reset above and the
+ # download below cannot also process it (issue #4691).
+ if not _claim_download_queue_item(db_session, queue_item_id):
+ return jsonify(
+ {"success": False, "message": "Download already in progress"}
+ ), 409
# Start download immediately
- with DownloadService(username, user_password) as service:
- success, message = service.download_resource(resource.id)
+ try:
+ with DownloadService(username, user_password) as service:
+ success, message = service.download_resource(resource.id)
+ except Exception:
+ safe_rollback(db_session, "download_source")
+ # Release the claim so a later run can retry this row: the
+ # download raised before download_resource recorded a terminal
+ # status (issue #4691).
+ _release_download_queue_item(db_session, queue_item_id)
+ raise
+
+ # download_resource writes a terminal status for the rows it actually
+ # downloads, so this is a no-op for those; it fires on the paths that
+ # don't (its already-downloaded early return), which would otherwise
+ # strand the claim in PROCESSING.
+ _finalize_download_queue_item(db_session, queue_item_id, success)
- if success:
- return jsonify(
- {"success": True, "message": "Download completed"}
- )
- # Log internal message, but show only generic message to user
- return jsonify({"success": False, "message": "Download failed"})
+ if success:
+ return jsonify({"success": True, "message": "Download completed"})
+ # Log internal message, but show only generic message to user
+ return jsonify({"success": False, "message": "Download failed"})
diff --git a/src/local_deep_research/research_library/services/download_service.py b/src/local_deep_research/research_library/services/download_service.py
index a7dd01a79b..45319c04ec 100644
--- a/src/local_deep_research/research_library/services/download_service.py
+++ b/src/local_deep_research/research_library/services/download_service.py
@@ -441,6 +441,9 @@ class DownloadService:
PENDING queue entry or a COMPLETED Document are skipped. As of #4685,
``download_bulk`` calls this unconditionally on every bulk run, so
previously-failed downloads are automatically retried each time.
+ A PROCESSING entry is in flight in another ``download_bulk`` stream,
+ so it is left alone rather than reset: resetting it would let this
+ run re-claim and re-download it (#4691).
"""
queued = 0
@@ -505,11 +508,32 @@ class DownloadService:
)
if any_queue:
- # Reset the existing queue entry
- any_queue.status = DocumentStatus.PENDING
- any_queue.research_id = research_id
- any_queue.collection_id = collection_id
- queued += 1
+ # Reset the existing queue entry, but never one a
+ # concurrent download_bulk stream has claimed: a
+ # PROCESSING row is in flight, and resetting it to
+ # PENDING lets this stream re-claim and re-download
+ # a resource the other stream is still downloading
+ # (issue #4691). The status guard lives inside the
+ # UPDATE, not in an `if` above it, so the check and
+ # the write cannot straddle another stream's claim.
+ reset = (
+ session.query(LibraryDownloadQueue)
+ .filter(
+ LibraryDownloadQueue.id == any_queue.id,
+ LibraryDownloadQueue.status
+ != DocumentStatus.PROCESSING,
+ )
+ .update(
+ {
+ LibraryDownloadQueue.status: DocumentStatus.PENDING,
+ LibraryDownloadQueue.research_id: research_id,
+ LibraryDownloadQueue.collection_id: collection_id,
+ },
+ synchronize_session=False,
+ )
+ )
+ if reset:
+ queued += 1
else:
# Add new queue entry
queue_entry = LibraryDownloadQueue(
diff --git a/tests/research_library/routes/_route_helpers_library.py b/tests/research_library/routes/_route_helpers_library.py
index 7933d96f8c..891355efb8 100644
--- a/tests/research_library/routes/_route_helpers_library.py
+++ b/tests/research_library/routes/_route_helpers_library.py
@@ -75,6 +75,10 @@ def _build_mock_query(
q.limit.return_value = q
q.offset.return_value = q
q.delete.return_value = 0
+ # A conditional UPDATE (e.g. download_bulk's atomic PENDING -> PROCESSING
+ # claim, issue #4691) returns the number of rows affected; default to 1 so
+ # the claim succeeds in mock-driven route tests.
+ q.update.return_value = 1
q.is_.return_value = q
return q
diff --git a/tests/research_library/routes/test_download_bulk_concurrent_claim_4691.py b/tests/research_library/routes/test_download_bulk_concurrent_claim_4691.py
new file mode 100644
index 0000000000..7fe475a5da
--- /dev/null
+++ b/tests/research_library/routes/test_download_bulk_concurrent_claim_4691.py
@@ -0,0 +1,786 @@
+"""Regression tests for issue #4691: concurrent ``download_bulk`` calls
+double-process the same queue items.
+
+Two simultaneous ``/api/download-bulk`` requests for the same user (the user
+clicks Download twice, or a second bulk run starts while the first is still
+running) both query the ``LibraryDownloadQueue`` rows with ``status=PENDING``
+and, with no atomic claim, both call ``download_resource`` for every row. The
+``COMPLETED``-Document dedup inside ``download_resource`` only guards the state
+*after* one stream finishes, so during the in-flight window both streams
+download the same resource.
+
+The fix claims each row (``PENDING -> PROCESSING`` in a single conditional
+UPDATE) before downloading; the row-count guard makes exactly one stream win
+each row, and the loser skips it. On the download-exception path the claim is
+released back to ``PENDING`` so the row stays retryable.
+
+A claim is only worth as much as the other writers of the status column
+respect, so two of these tests drive the real collaborators rather than mocks:
+``download_bulk``'s pre-pass calls ``queue_research_downloads``, whose reset
+branch must leave an in-flight PROCESSING row alone, and a claimed row must
+reach a terminal status on the paths that don't record one themselves
+(``mode="text_only"``, and ``download_resource``'s already-downloaded early
+return), rather than being stranded in PROCESSING.
+
+The remaining writers of ``DownloadQueue.status`` get the same treatment at
+the end of this module: ``queue_all_undownloaded`` and ``download_source``
+both reset a row back to PENDING, and PROCESSING satisfies their guards, so
+each can hand a row a bulk stream is downloading to a second downloader. The
+claim also has to survive its own post-claim window: it is committed before
+the resource lookup, so a raise there strands the row in PROCESSING, which
+nothing in ``src/`` reaps.
+
+These tests use a real on-disk SQLite database so the conditional-UPDATE claim
+runs against a real engine, not a mock.
+"""
+
+from contextlib import ExitStack, contextmanager
+from unittest.mock import MagicMock, patch
+
+from flask import Flask
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+
+from local_deep_research.database.models import Base
+from local_deep_research.database.models.library import (
+ DocumentStatus,
+ DownloadQueue as LibraryDownloadQueue,
+)
+from local_deep_research.database.models.research import (
+ ResearchHistory,
+ ResearchResource,
+)
+from local_deep_research.research_library.routes import (
+ library_routes as routes_mod,
+)
+
+
+RESEARCH_ID = "r1"
+
+
+def _engine_with_pending_row(tmp_path, name):
+ """Build a real SQLite DB seeded with one PENDING download-queue row.
+
+ Returns (Session factory, queue_item_id, resource_id).
+ """
+ engine = create_engine(f"sqlite:///{tmp_path}/{name}.db")
+ Base.metadata.create_all(engine)
+ Session = sessionmaker(bind=engine)
+
+ s = Session()
+ s.add(
+ ResearchHistory(
+ id=RESEARCH_ID,
+ query="test query",
+ mode="quick",
+ status="completed",
+ created_at="2026-05-09T00:00:00",
+ )
+ )
+ resource = ResearchResource(
+ research_id=RESEARCH_ID,
+ title="Test Paper",
+ url="https://arxiv.org/abs/2401.0001",
+ source_type="academic",
+ created_at="2026-05-09T00:00:00",
+ )
+ s.add(resource)
+ s.commit()
+ row = LibraryDownloadQueue(
+ resource_id=resource.id,
+ research_id=RESEARCH_ID,
+ status=DocumentStatus.PENDING,
+ )
+ s.add(row)
+ s.commit()
+ queue_item_id = row.id
+ resource_id = resource.id
+ s.close()
+ return Session, queue_item_id, resource_id
+
+
+def _set_status(Session, queue_item_id, status):
+ s = Session()
+ s.query(LibraryDownloadQueue).filter_by(id=queue_item_id).update(
+ {LibraryDownloadQueue.status: status}, synchronize_session=False
+ )
+ s.commit()
+ s.close()
+
+
+def _status_of(Session, queue_item_id):
+ s = Session()
+ status = s.query(LibraryDownloadQueue).get(queue_item_id).status
+ s.close()
+ return status
+
+
+def _auth_db_manager():
+ return MagicMock(
+ is_user_connected=MagicMock(return_value=True),
+ connections={"testuser": True},
+ has_encryption=False,
+ )
+
+
+def test_download_bulk_claims_pending_row_before_downloading(tmp_path):
+ """The row must be claimed (PENDING -> PROCESSING) BEFORE
+ ``download_resource`` runs, so a concurrent stream querying ``PENDING``
+ won't also grab it. On unfixed main the row is still ``PENDING`` when the
+ download starts, which is exactly the #4691 double-processing window.
+ """
+ Session, queue_item_id, resource_id = _engine_with_pending_row(
+ tmp_path, "claim_before"
+ )
+
+ observed = {}
+
+ def fake_download_resource(rid):
+ # An independent session models a concurrent stream: read the row's
+ # status as it stands the moment the download begins.
+ s2 = Session()
+ row = s2.query(LibraryDownloadQueue).get(queue_item_id)
+ observed["status_at_download"] = row.status
+ s2.close()
+ return (True, None)
+
+ @contextmanager
+ def fake_db_session(*a, **kw):
+ with Session() as sess:
+ yield sess
+
+ ds_mock = MagicMock()
+ ds_mock.queue_research_downloads.return_value = 0
+ ds_mock.download_resource.side_effect = fake_download_resource
+
+ app = Flask(__name__)
+ app.config["SECRET_KEY"] = "test"
+ app.config["TESTING"] = True
+ app.register_blueprint(routes_mod.library_bp)
+
+ with (
+ patch.object(
+ routes_mod, "get_user_db_session", side_effect=fake_db_session
+ ),
+ patch.object(
+ routes_mod, "get_authenticated_user_password", return_value="pw"
+ ),
+ patch.object(routes_mod, "DownloadService", return_value=ds_mock),
+ patch(
+ "local_deep_research.web.auth.decorators.db_manager",
+ _auth_db_manager(),
+ ),
+ ):
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+ sess["session_id"] = "sid"
+ resp = client.post(
+ "/library/api/download-bulk",
+ json={"research_ids": [RESEARCH_ID], "mode": "pdf"},
+ )
+ # The SSE generator is lazy; consume the body so the loop runs.
+ body = resp.get_data(as_text=True)
+
+ assert resp.status_code == 200, body
+ assert ds_mock.download_resource.called, body
+ assert observed.get("status_at_download") == DocumentStatus.PROCESSING, (
+ "download_bulk must claim the row (PENDING -> PROCESSING) before "
+ "calling download_resource so a concurrent stream can't also process "
+ f"it; saw {observed.get('status_at_download')} (issue #4691)."
+ )
+
+
+def test_pending_row_can_be_claimed_only_once(tmp_path):
+ """The atomic claim is the single point of mutual exclusion: the first
+ claim of a PENDING row wins (True) and flips it to PROCESSING; a second
+ claim (the losing concurrent stream) sees 0 updated rows and returns False.
+ """
+ from local_deep_research.research_library.routes.library_routes import (
+ _claim_download_queue_item,
+ )
+
+ Session, queue_item_id, _ = _engine_with_pending_row(tmp_path, "claim_once")
+ s = Session()
+
+ first = _claim_download_queue_item(s, queue_item_id)
+ second = _claim_download_queue_item(s, queue_item_id)
+
+ assert first is True
+ assert second is False
+ s.expire_all()
+ row = s.query(LibraryDownloadQueue).get(queue_item_id)
+ assert row.status == DocumentStatus.PROCESSING
+ s.close()
+
+
+def test_missing_row_is_not_claimed(tmp_path):
+ """Claiming a non-existent row returns False rather than raising."""
+ from local_deep_research.research_library.routes.library_routes import (
+ _claim_download_queue_item,
+ )
+
+ Session, _, _ = _engine_with_pending_row(tmp_path, "claim_missing")
+ s = Session()
+ assert _claim_download_queue_item(s, 999999) is False
+ s.close()
+
+
+def test_claim_is_released_back_to_pending_on_error(tmp_path):
+ """A claimed row returned via _release_download_queue_item is PENDING again
+ and can be re-claimed, so a download that raised stays retryable.
+ """
+ from local_deep_research.research_library.routes.library_routes import (
+ _claim_download_queue_item,
+ _release_download_queue_item,
+ )
+
+ Session, queue_item_id, _ = _engine_with_pending_row(
+ tmp_path, "claim_release"
+ )
+ s = Session()
+
+ assert _claim_download_queue_item(s, queue_item_id) is True
+ s.expire_all()
+ assert (
+ s.query(LibraryDownloadQueue).get(queue_item_id).status
+ == DocumentStatus.PROCESSING
+ )
+
+ _release_download_queue_item(s, queue_item_id)
+ s.expire_all()
+ assert (
+ s.query(LibraryDownloadQueue).get(queue_item_id).status
+ == DocumentStatus.PENDING
+ )
+ # Retryable: a later run can claim it again.
+ assert _claim_download_queue_item(s, queue_item_id) is True
+ s.close()
+
+
+def test_queue_research_downloads_leaves_in_flight_row_alone(tmp_path):
+ """The pre-pass must not un-claim a row another stream is downloading.
+
+ ``download_bulk`` calls ``queue_research_downloads`` unconditionally on
+ every run (#4685). Its reset branch gates on "no PENDING queue row and no
+ COMPLETED Document", which a PROCESSING row satisfies, so an unguarded
+ reset flips an in-flight row back to PENDING and the second stream claims
+ and re-downloads it: issue #4691 again, narrowed to the in-flight window.
+
+ This drives the real ``queue_research_downloads`` rather than a mock,
+ because it is the collaborator whose behaviour decides whether the claim
+ survives.
+ """
+ from local_deep_research.research_library.services import (
+ download_service as ds_mod,
+ )
+
+ Session, queue_item_id, _ = _engine_with_pending_row(tmp_path, "prepass")
+ # Stream 1 has claimed the row and is downloading it right now.
+ _set_status(Session, queue_item_id, DocumentStatus.PROCESSING)
+
+ @contextmanager
+ def fake_db_session(*a, **kw):
+ with Session() as sess:
+ yield sess
+
+ with (
+ patch.object(
+ ds_mod, "get_user_db_session", side_effect=fake_db_session
+ ),
+ # RetryManager opens the real user DB in the constructor and plays no
+ # part in the pre-pass under test.
+ patch.object(ds_mod, "RetryManager", MagicMock()),
+ patch.object(
+ ds_mod.DownloadService,
+ "_check_url_against_policy",
+ return_value=(True, None),
+ ),
+ patch.object(
+ ds_mod.DownloadService, "_is_downloadable", return_value=True
+ ),
+ ):
+ service = ds_mod.DownloadService("testuser", "pw")
+ # Stream 2's pre-pass runs while stream 1 holds the claim.
+ queued = service.queue_research_downloads(
+ RESEARCH_ID, collection_id="c1"
+ )
+
+ assert _status_of(Session, queue_item_id) == DocumentStatus.PROCESSING, (
+ "queue_research_downloads must leave a PROCESSING row claimed; "
+ "resetting it to PENDING lets a second bulk stream re-download a "
+ "resource the first stream is still downloading (issue #4691)."
+ )
+ assert queued == 0, (
+ f"an in-flight row must not be counted as newly queued; queued={queued}"
+ )
+
+
+def test_queue_research_downloads_still_resets_failed_row(tmp_path):
+ """The guard must be narrow: a FAILED row is not in flight, so the
+ documented retry-on-every-bulk-run behaviour (#4685) still resets it.
+ """
+ from local_deep_research.research_library.services import (
+ download_service as ds_mod,
+ )
+
+ Session, queue_item_id, _ = _engine_with_pending_row(
+ tmp_path, "prepass_failed"
+ )
+ _set_status(Session, queue_item_id, DocumentStatus.FAILED)
+
+ @contextmanager
+ def fake_db_session(*a, **kw):
+ with Session() as sess:
+ yield sess
+
+ with (
+ patch.object(
+ ds_mod, "get_user_db_session", side_effect=fake_db_session
+ ),
+ # RetryManager opens the real user DB in the constructor and plays no
+ # part in the pre-pass under test.
+ patch.object(ds_mod, "RetryManager", MagicMock()),
+ patch.object(
+ ds_mod.DownloadService,
+ "_check_url_against_policy",
+ return_value=(True, None),
+ ),
+ patch.object(
+ ds_mod.DownloadService, "_is_downloadable", return_value=True
+ ),
+ ):
+ service = ds_mod.DownloadService("testuser", "pw")
+ queued = service.queue_research_downloads(
+ RESEARCH_ID, collection_id="c1"
+ )
+
+ assert _status_of(Session, queue_item_id) == DocumentStatus.PENDING
+ assert queued == 1
+
+
+def test_text_only_bulk_finalizes_claim(tmp_path):
+ """A successful text extraction must leave the row COMPLETED, not stranded.
+
+ ``download_as_text`` and its call tree never write ``LibraryDownloadQueue``
+ and nothing in ``src/`` reaps PROCESSING rows, so without a finalize the
+ claim is permanent: the successful extraction also writes a COMPLETED
+ Document, which blocks the pre-pass reset and ``queue_all_undownloaded``.
+ Pre-fix these rows stayed PENDING, so stranding them is a regression in
+ normal successful operation.
+ """
+ Session, queue_item_id, _ = _engine_with_pending_row(tmp_path, "text_only")
+
+ @contextmanager
+ def fake_db_session(*a, **kw):
+ with Session() as sess:
+ yield sess
+
+ ds_mock = MagicMock()
+ ds_mock.queue_research_downloads.return_value = 0
+ # Faithful to the real call tree: it never touches LibraryDownloadQueue.
+ ds_mock.download_as_text.return_value = (True, None)
+
+ app = Flask(__name__)
+ app.config["SECRET_KEY"] = "test"
+ app.config["TESTING"] = True
+ app.register_blueprint(routes_mod.library_bp)
+
+ with (
+ patch.object(
+ routes_mod, "get_user_db_session", side_effect=fake_db_session
+ ),
+ patch.object(
+ routes_mod, "get_authenticated_user_password", return_value="pw"
+ ),
+ patch.object(routes_mod, "DownloadService", return_value=ds_mock),
+ patch(
+ "local_deep_research.web.auth.decorators.db_manager",
+ _auth_db_manager(),
+ ),
+ ):
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+ sess["session_id"] = "sid"
+ resp = client.post(
+ "/library/api/download-bulk",
+ json={"research_ids": [RESEARCH_ID], "mode": "text_only"},
+ )
+ body = resp.get_data(as_text=True)
+
+ assert resp.status_code == 200, body
+ assert ds_mock.download_as_text.called, body
+ assert _status_of(Session, queue_item_id) == DocumentStatus.COMPLETED, (
+ "a claimed row must reach a terminal status after a successful "
+ "text_only extraction; download_as_text never writes the queue, so "
+ "the claim is stranded in PROCESSING forever (issue #4691)."
+ )
+
+
+def test_text_only_bulk_failure_returns_row_to_pending(tmp_path):
+ """A failed text extraction returns the row to PENDING, matching the
+ pre-fix behaviour where the row stayed retryable.
+ """
+ Session, queue_item_id, _ = _engine_with_pending_row(tmp_path, "text_fail")
+
+ @contextmanager
+ def fake_db_session(*a, **kw):
+ with Session() as sess:
+ yield sess
+
+ ds_mock = MagicMock()
+ ds_mock.queue_research_downloads.return_value = 0
+ ds_mock.download_as_text.return_value = (False, "no text available")
+
+ app = Flask(__name__)
+ app.config["SECRET_KEY"] = "test"
+ app.config["TESTING"] = True
+ app.register_blueprint(routes_mod.library_bp)
+
+ with (
+ patch.object(
+ routes_mod, "get_user_db_session", side_effect=fake_db_session
+ ),
+ patch.object(
+ routes_mod, "get_authenticated_user_password", return_value="pw"
+ ),
+ patch.object(routes_mod, "DownloadService", return_value=ds_mock),
+ patch(
+ "local_deep_research.web.auth.decorators.db_manager",
+ _auth_db_manager(),
+ ),
+ ):
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+ sess["session_id"] = "sid"
+ resp = client.post(
+ "/library/api/download-bulk",
+ json={"research_ids": [RESEARCH_ID], "mode": "text_only"},
+ )
+ body = resp.get_data(as_text=True)
+
+ assert resp.status_code == 200, body
+ assert _status_of(Session, queue_item_id) == DocumentStatus.PENDING
+
+
+def test_pdf_mode_terminal_status_is_not_overwritten(tmp_path):
+ """pdf-mode behaviour is unchanged: ``download_resource`` records its own
+ terminal status, so the finalize must be a no-op there. A row it marked
+ FAILED must not be rewritten to COMPLETED by a truthy return.
+ """
+ Session, queue_item_id, _ = _engine_with_pending_row(tmp_path, "pdf_noop")
+
+ def fake_download_resource(rid):
+ # Model download_resource's own terminal write.
+ _set_status(Session, queue_item_id, DocumentStatus.FAILED)
+ return (True, None)
+
+ @contextmanager
+ def fake_db_session(*a, **kw):
+ with Session() as sess:
+ yield sess
+
+ ds_mock = MagicMock()
+ ds_mock.queue_research_downloads.return_value = 0
+ ds_mock.download_resource.side_effect = fake_download_resource
+
+ app = Flask(__name__)
+ app.config["SECRET_KEY"] = "test"
+ app.config["TESTING"] = True
+ app.register_blueprint(routes_mod.library_bp)
+
+ with (
+ patch.object(
+ routes_mod, "get_user_db_session", side_effect=fake_db_session
+ ),
+ patch.object(
+ routes_mod, "get_authenticated_user_password", return_value="pw"
+ ),
+ patch.object(routes_mod, "DownloadService", return_value=ds_mock),
+ patch(
+ "local_deep_research.web.auth.decorators.db_manager",
+ _auth_db_manager(),
+ ),
+ ):
+ with app.test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+ sess["session_id"] = "sid"
+ client.post(
+ "/library/api/download-bulk",
+ json={"research_ids": [RESEARCH_ID], "mode": "pdf"},
+ ).get_data(as_text=True)
+
+ assert _status_of(Session, queue_item_id) == DocumentStatus.FAILED, (
+ "the finalize is scoped to PROCESSING so it must not overwrite a "
+ "terminal status download_resource already recorded."
+ )
+
+
+# Guard against a schema change silently making the claim a no-op: a fresh
+# PENDING row must actually be flippable to PROCESSING at the DB level.
+def test_status_column_round_trips_processing(tmp_path):
+ Session, queue_item_id, _ = _engine_with_pending_row(tmp_path, "roundtrip")
+ s = Session()
+ row = s.query(LibraryDownloadQueue).get(queue_item_id)
+ assert row.status == DocumentStatus.PENDING
+ row.status = DocumentStatus.PROCESSING
+ s.commit()
+ s.expire_all()
+ assert (
+ s.query(LibraryDownloadQueue).get(queue_item_id).status
+ == DocumentStatus.PROCESSING
+ )
+ s.close()
+
+
+# ---------------------------------------------------------------------------
+# The claim is only worth as much as the OTHER writers of the status column
+# respect. The tests above cover download_bulk's own claim and the pre-pass in
+# queue_research_downloads. The remaining writers of DownloadQueue.status are
+# the two reset paths below, plus the post-claim window inside download_bulk
+# itself. Each one can hand the same row to a second downloader.
+# ---------------------------------------------------------------------------
+
+
+def _resource_filter_mock(resource_ids):
+ """A ResourceFilter that lets every listed resource through the retry
+ policy, so queue_all_undownloaded reaches its queue-reset branch.
+ """
+ instance = MagicMock()
+ instance.filter_downloadable_resources.return_value = [
+ MagicMock(resource_id=rid, can_retry=True, reason="ok")
+ for rid in resource_ids
+ ]
+ instance.get_filter_summary.return_value = MagicMock(
+ to_dict=MagicMock(return_value={})
+ )
+ instance.get_skipped_resources_info.return_value = []
+ return instance
+
+
+def _app():
+ app = Flask(__name__)
+ app.config["SECRET_KEY"] = "test"
+ app.config["TESTING"] = True
+ app.register_blueprint(routes_mod.library_bp)
+ return app
+
+
+def test_queue_all_undownloaded_does_not_reset_an_in_flight_claim(tmp_path):
+ """``queue_all_undownloaded`` resets any row that is not PENDING back to
+ PENDING. PROCESSING satisfies ``!= PENDING``, so it un-claims a row a
+ ``download_bulk`` stream is downloading right now, and the freed row is
+ then re-claimable while the first stream is still working on it: the
+ #4691 double-processing window, reopened from a different route.
+
+ Its own selection query (outerjoin Document, filter Document.id.is_(None))
+ SELECTS in-flight rows, because a download in progress has not written a
+ completed Document yet.
+ """
+ Session, queue_item_id, resource_id = _engine_with_pending_row(
+ tmp_path, "qau_inflight"
+ )
+ # A concurrent download_bulk stream holds the claim.
+ _set_status(Session, queue_item_id, DocumentStatus.PROCESSING)
+
+ @contextmanager
+ def fake_db_session(*a, **kw):
+ with Session() as sess:
+ yield sess
+
+ with (
+ patch.object(
+ routes_mod, "get_user_db_session", side_effect=fake_db_session
+ ),
+ patch.object(
+ routes_mod, "get_authenticated_user_password", return_value="pw"
+ ),
+ patch.object(
+ routes_mod,
+ "ResourceFilter",
+ return_value=_resource_filter_mock([resource_id]),
+ ),
+ patch(
+ "local_deep_research.web.auth.decorators.db_manager",
+ _auth_db_manager(),
+ ),
+ ):
+ with _app().test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+ resp = client.post("/library/api/queue-all-undownloaded", json={})
+
+ assert resp.status_code == 200, resp.get_data(as_text=True)
+ assert _status_of(Session, queue_item_id) == DocumentStatus.PROCESSING, (
+ "queue_all_undownloaded must leave a PROCESSING row alone: resetting "
+ "it to PENDING un-claims a download another stream is running and "
+ "lets a second stream re-claim the same row (issue #4691)."
+ )
+
+
+def test_download_source_does_not_reset_an_in_flight_claim(tmp_path):
+ """``download_source`` resets the row to PENDING unconditionally and then
+ downloads immediately in the request thread, without claiming it. Its only
+ early return is ``existing and existing.status == "completed"``, which is
+ inert during the race window: an in-flight bulk download has not written a
+ completed Document yet.
+
+ So the row must stay PROCESSING and no second download may start.
+ """
+ Session, queue_item_id, resource_id = _engine_with_pending_row(
+ tmp_path, "dsrc_inflight"
+ )
+ _set_status(Session, queue_item_id, DocumentStatus.PROCESSING)
+
+ @contextmanager
+ def fake_db_session(*a, **kw):
+ with Session() as sess:
+ yield sess
+
+ ds_mock = MagicMock()
+ ds_mock.download_resource.return_value = (True, None)
+ ds_mock.__enter__ = lambda self: self
+ ds_mock.__exit__ = lambda self, *a: False
+
+ with (
+ patch.object(
+ routes_mod, "get_user_db_session", side_effect=fake_db_session
+ ),
+ patch.object(
+ routes_mod, "get_authenticated_user_password", return_value="pw"
+ ),
+ patch.object(routes_mod, "DownloadService", return_value=ds_mock),
+ patch.object(
+ routes_mod, "get_document_for_resource", return_value=None
+ ),
+ patch(
+ "local_deep_research.web.auth.decorators.db_manager",
+ _auth_db_manager(),
+ ),
+ ):
+ with _app().test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+ resp = client.post(
+ "/library/api/download-source",
+ json={
+ "research_id": RESEARCH_ID,
+ "url": "https://arxiv.org/abs/2401.0001",
+ },
+ )
+
+ body = resp.get_data(as_text=True)
+ # download_resource is asserted NOT to run, so mocking it cannot hide a
+ # status write: a call that never happens writes nothing either way.
+ assert not ds_mock.download_resource.called, (
+ "download_source must not start a second download of a resource a "
+ f"concurrent stream has already claimed (issue #4691); got {body}"
+ )
+ assert _status_of(Session, queue_item_id) == DocumentStatus.PROCESSING, (
+ "download_source must not un-claim an in-flight PROCESSING row."
+ )
+
+
+def _run_download_bulk(Session, extra_patches=()):
+ """Drive one /api/download-bulk request against the real SQLite DB."""
+
+ @contextmanager
+ def fake_db_session(*a, **kw):
+ with Session() as sess:
+ yield sess
+
+ ds_mock = MagicMock()
+ ds_mock.queue_research_downloads.return_value = 0
+ ds_mock.download_resource.return_value = (True, None)
+
+ with ExitStack() as stack:
+ for cm in (
+ patch.object(
+ routes_mod, "get_user_db_session", side_effect=fake_db_session
+ ),
+ patch.object(
+ routes_mod, "get_authenticated_user_password", return_value="pw"
+ ),
+ patch.object(routes_mod, "DownloadService", return_value=ds_mock),
+ patch(
+ "local_deep_research.web.auth.decorators.db_manager",
+ _auth_db_manager(),
+ ),
+ *extra_patches,
+ ):
+ stack.enter_context(cm)
+
+ with _app().test_client() as client:
+ with client.session_transaction() as sess:
+ sess["username"] = "testuser"
+ sess["session_id"] = "sid"
+ client.post(
+ "/library/api/download-bulk",
+ json={"research_ids": [RESEARCH_ID], "mode": "pdf"},
+ ).get_data(as_text=True)
+ return ds_mock
+
+
+def test_download_bulk_does_not_strand_a_claim_when_title_is_null(tmp_path):
+ """``ResearchResource.title`` is a nullable Text column, so
+ ``resource.title[:50]`` raises TypeError for a resource whose title was
+ never populated. That lookup ran after the claim was committed but before
+ the try that releases it, so the row stayed PROCESSING forever: nothing in
+ ``src/`` reaps PROCESSING rows, and every reset path now refuses to touch
+ them.
+ """
+ Session, queue_item_id, resource_id = _engine_with_pending_row(
+ tmp_path, "strand_null_title"
+ )
+ s = Session()
+ s.query(ResearchResource).filter_by(id=resource_id).update({"title": None})
+ s.commit()
+ s.close()
+
+ _run_download_bulk(Session)
+
+ # Assert the FINAL state: "claimed at download time" and "not stranded
+ # afterwards" are different properties, and only this one catches a leak.
+ assert _status_of(Session, queue_item_id) == DocumentStatus.COMPLETED, (
+ "a null title must not strand the claim: the row should reach a "
+ "terminal status like any other successful download (issue #4691)."
+ )
+
+
+def test_download_bulk_releases_the_claim_when_post_claim_work_raises(
+ tmp_path,
+):
+ """The null title above is one instance of a structural window, not the
+ whole of it, so the fix is guaranteed cleanup rather than a null-check on
+ that one expression.
+
+ Fault-inject a raise into the post-claim resource lookup and assert the
+ claim is released, which is the property that has to hold for *any* raise
+ in that window.
+ """
+ Session, queue_item_id, _ = _engine_with_pending_row(
+ tmp_path, "release_post_claim"
+ )
+
+ class _NotAnEntity:
+ """Not a mapped class, so the post-claim
+ ``session.query(ResearchResource)`` raises ArgumentError. The specific
+ exception does not matter; what is under test is that *any* raise in
+ the post-claim window releases the claim.
+ """
+
+ _run_download_bulk(
+ Session,
+ extra_patches=(
+ patch.object(routes_mod, "ResearchResource", _NotAnEntity),
+ ),
+ )
+
+ assert _status_of(Session, queue_item_id) == DocumentStatus.PENDING, (
+ "a raise in the post-claim window must release the claim back to "
+ "PENDING so the row stays retryable, rather than leaving it stuck in "
+ "PROCESSING (issue #4691)."
+ )
diff --git a/tests/research_library/routes/test_library_routes_extra_coverage.py b/tests/research_library/routes/test_library_routes_extra_coverage.py
index 67b2cf5d1f..bd79df4d43 100644
--- a/tests/research_library/routes/test_library_routes_extra_coverage.py
+++ b/tests/research_library/routes/test_library_routes_extra_coverage.py
@@ -961,9 +961,17 @@ class TestQueueAllUndownloadedEdgeCases:
assert resp.status_code == 200
data = resp.get_json()
assert data["queued"] >= 1
- # Entry should have been reset to PENDING
- assert existing_entry.status == DocumentStatus.PENDING
- assert existing_entry.completed_at is None
+ # The reset is a conditional UPDATE now, not an ORM attribute
+ # assignment, so that it cannot un-claim a row a concurrent
+ # download stream is working on (issue #4691). Assert the values
+ # it writes rather than a mutation on the mock entry.
+ main_q.update.assert_called_once()
+ values = {
+ col.key: val
+ for col, val in main_q.update.call_args[0][0].items()
+ }
+ assert values["status"] == DocumentStatus.PENDING
+ assert values["completed_at"] is None
def test_existing_queue_entry_already_pending_counted(self, app):
"""When a queue entry is already PENDING, it is still counted."""
@@ -1113,6 +1121,15 @@ class TestDownloadSourceExistingQueueEntry:
content_type="application/json",
)
assert resp.status_code == 200
- # Entry should have been reset
- assert existing_entry.status == DocumentStatus.PENDING
- assert existing_entry.priority == 1
+ # The reset is a conditional UPDATE now, not an ORM attribute
+ # assignment, so that it cannot un-claim a row a concurrent
+ # download stream is working on (issue #4691). Assert the values
+ # it writes rather than a mutation on the mock entry.
+ reset_values = {
+ col.key: val
+ for col, val in resource_q.update.call_args_list[0][0][
+ 0
+ ].items()
+ }
+ assert reset_values["status"] == DocumentStatus.PENDING
+ assert reset_values["priority"] == 1
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 553c3ff5fa..526c905eba 100644
--- a/tests/research_library/services/test_download_service_3827_fix.py
+++ b/tests/research_library/services/test_download_service_3827_fix.py
@@ -635,26 +635,26 @@ def test_download_bulk_rolls_back_session_when_download_raises():
resource_b.research_id = "r1"
outer_session = MagicMock()
- # Track session.query side effects in order:
- # - count() for total pending (returns 2)
- # - .all() for queue_items list (returns [a, b])
- # - .get(101) for resource A (line 843 first iteration)
- # - .get(102) for resource B (line 843 second iteration — must succeed
- # after rollback)
- count_q = MagicMock()
- count_q.filter_by.return_value = count_q
- count_q.count.return_value = 2
- items_q = MagicMock()
- items_q.filter_by.return_value = items_q
- items_q.all.return_value = [q_item_a, q_item_b]
+ # Route session.query(...) by model so the call order is robust to the
+ # atomic PENDING -> PROCESSING claim added for issue #4691 (which issues
+ # extra DownloadQueue UPDATEs per item):
+ # - DownloadQueue queries serve count() (2 pending), .all() ([a, b]) and
+ # the claim/release .update() (returns 1 = claim wins)
+ # - ResearchResource queries serve .get(101)/.get(102)
+ queue_q = MagicMock()
+ queue_q.filter_by.return_value = queue_q
+ queue_q.count.return_value = 2
+ queue_q.all.return_value = [q_item_a, q_item_b]
+ queue_q.update.return_value = 1
resource_q = MagicMock()
resource_q.get.side_effect = [resource_a, resource_b]
- outer_session.query.side_effect = [
- count_q,
- items_q,
- resource_q,
- resource_q,
- ]
+
+ def _query_router(model):
+ if getattr(model, "__name__", "") == "DownloadQueue":
+ return queue_q
+ return resource_q
+
+ outer_session.query.side_effect = _query_router
@contextmanager
def fake_db_session(*a, **kw):
diff --git a/tests/research_library/services/test_download_service_coverage.py b/tests/research_library/services/test_download_service_coverage.py
index d9a1ac34c1..6696e71e55 100644
--- a/tests/research_library/services/test_download_service_coverage.py
+++ b/tests/research_library/services/test_download_service_coverage.py
@@ -28,6 +28,10 @@ from unittest.mock import MagicMock, patch
import pytest
+from local_deep_research.database.models.library import (
+ DocumentStatus,
+ DownloadQueue as LibraryDownloadQueue,
+)
from local_deep_research.research_library.services.download_service import (
DownloadService,
)
@@ -479,6 +483,7 @@ class TestQueueResearchDownloads:
resource.url = "https://example.com/paper.pdf"
any_queue = MagicMock()
+ any_queue.id = 7
mock_session = MagicMock()
mock_session.query.return_value.filter_by.return_value.all.return_value = [
@@ -491,6 +496,11 @@ class TestQueueResearchDownloads:
None, # existing_doc
any_queue, # any_queue
]
+ # The reset is a conditional UPDATE guarded on status != PROCESSING so
+ # it can't un-claim a row a concurrent download_bulk stream is already
+ # downloading (issue #4691); it reports the rows it changed.
+ update = mock_session.query.return_value.filter.return_value.update
+ update.return_value = 1
with (
patch(
@@ -503,7 +513,46 @@ class TestQueueResearchDownloads:
"res-1", collection_id="col-5"
)
assert result == 1
- assert any_queue.collection_id == "col-5"
+ values = update.call_args[0][0]
+ assert values[LibraryDownloadQueue.status] == DocumentStatus.PENDING
+ assert values[LibraryDownloadQueue.research_id] == "res-1"
+ assert values[LibraryDownloadQueue.collection_id] == "col-5"
+
+ def test_in_flight_queue_entry_is_not_reset(self, svc):
+ """A row a concurrent stream has claimed (PROCESSING) is left alone:
+ the guarded UPDATE matches nothing, so it is not counted as queued
+ (issue #4691).
+ """
+ resource = MagicMock()
+ resource.id = 1
+ resource.document_id = None
+ resource.url = "https://example.com/paper.pdf"
+
+ any_queue = MagicMock()
+ any_queue.id = 7
+
+ mock_session = MagicMock()
+ mock_session.query.return_value.filter_by.return_value.all.return_value = [
+ resource
+ ]
+ mock_session.query.return_value.filter_by.return_value.first.side_effect = [
+ None, # existing_queue
+ None, # existing_doc
+ any_queue, # any_queue
+ ]
+ mock_session.query.return_value.filter.return_value.update.return_value = 0
+
+ with (
+ patch(
+ f"{MODULE}.get_user_db_session",
+ return_value=self._make_session_ctx(mock_session),
+ ),
+ patch(f"{MODULE}.is_downloadable_url", return_value=True),
+ ):
+ result = svc.queue_research_downloads(
+ "res-1", collection_id="col-5"
+ )
+ assert result == 0
def test_not_downloadable_skipped(self, svc):
resource = MagicMock()