~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix: reorder _start_research to eliminate stuck IN_PROGRESS window (#3481)
LearningCircuitafbf8601f991
diff --git a/src/local_deep_research/web/queue/processor_v2.py b/src/local_deep_research/web/queue/processor_v2.pyindex eda57fadbe..80ec5a2f5b 100644--- a/src/local_deep_research/web/queue/processor_v2.py+++ b/src/local_deep_research/web/queue/processor_v2.py@@ -34,6 +34,10 @@ MAX_RESEARCH_LOOKUP_RETRIES = 3INITIAL_RESEARCH_LOOKUP_DELAY = 0.5 # secondsRETRY_BACKOFF_MULTIPLIER = 2+# Give up on a queued research after this many consecutive spawn failures.+# Each failure leaves is_processing=False so the next loop tick retries.+SPAWN_RETRY_LIMIT = 3+class QueueProcessorV2:"""@@ -67,6 +71,14 @@ class QueueProcessorV2:self.pending_operations = {}self._pending_operations_lock = threading.Lock()+ # Count consecutive spawn failures per research_id. Entries are+ # popped on success or after hitting SPAWN_RETRY_LIMIT (then the+ # research is marked FAILED). In-memory is sufficient: a restart+ # resets the counter and the research gets a fresh N retries,+ # which is the desired behavior if the underlying system issue+ # (thread pool, memory) cleared.+ self._spawn_retry_counts: dict[str, int] = {}+def start(self):"""Start the queue processor thread."""if self.running:@@ -568,15 +580,14 @@ class QueueProcessorV2:)for queued_research in queued:+ research_id = queued_research.research_idtry:# Mark as processingqueued_research.is_processing = Truedb_session.commit()# Update task status- queue_service.update_task_status(- queued_research.research_id, "processing"- )+ queue_service.update_task_status(research_id, "processing")# Start the researchself._start_research(@@ -586,28 +597,109 @@ class QueueProcessorV2:queued_research,)- # Remove from queue+ # Success — clear any prior spawn-failure count and+ # remove the queue row.+ self._spawn_retry_counts.pop(research_id, None)db_session.delete(queued_research)db_session.commit()logger.info(- f"Started queued research {queued_research.research_id} "- f"for user {username}"+ f"Started queued research {research_id} for user {username}")except Exception:logger.exception(- f"Error starting queued research {queued_research.research_id}"+ f"Error starting queued research {research_id}")- # Reset processing flag- queued_research.is_processing = False- db_session.commit()+ # Session may be in PendingRollbackError state after a+ # failed commit inside _start_research.+ try:+ db_session.rollback()+ except Exception:+ logger.debug(+ "Rollback after start failure",+ exc_info=True,+ )- # Update task status- queue_service.update_task_status(- queued_research.research_id,- ResearchStatus.FAILED,- error_message="Failed to start research",+ attempts = self._spawn_retry_counts.get(research_id, 0) + 1+ self._spawn_retry_counts[research_id] = attempts++ # Re-query in case rollback expired the ORM object.+ fresh_queued = (+ db_session.query(QueuedResearch)+ .filter_by(username=username, research_id=research_id)+ .first()+ )++ if attempts < SPAWN_RETRY_LIMIT:+ # Transient failure — allow the next loop tick to+ # retry. _start_research now mutates no DB state+ # before the spawn succeeds, so the only fix-up+ # needed is resetting is_processing.+ logger.warning(+ f"Spawn failed for research {research_id} "+ f"(attempt {attempts}/{SPAWN_RETRY_LIMIT}), "+ "leaving queued for retry"+ )+ if fresh_queued:+ fresh_queued.is_processing = False+ try:+ db_session.commit()+ except Exception:+ logger.exception(+ "Failed to reset is_processing for "+ f"research {research_id}"+ )+ try:+ db_session.rollback()+ except Exception:+ logger.debug(+ "Rollback after is_processing reset",+ exc_info=True,+ )+ continue++ # Exhausted retries — mark terminal FAILED, delete the+ # queue row to stop re-dispatch, and notify the user.+ logger.exception(+ f"Spawn failed for research {research_id} "+ f"after {attempts} attempts; marking FAILED"+ )+ self._spawn_retry_counts.pop(research_id, None)+ try:+ research = (+ db_session.query(ResearchHistory)+ .filter_by(id=research_id)+ .first()+ )+ if research:+ research.status = ResearchStatus.FAILED+ if fresh_queued:+ db_session.delete(fresh_queued)+ db_session.commit()+ except Exception:+ logger.exception(+ "Failed to persist terminal FAILED state for "+ f"research {research_id}"+ )+ try:+ db_session.rollback()+ except Exception:+ logger.debug(+ "Rollback after terminal update failure",+ exc_info=True,+ )++ # notify_research_failed opens its own session and+ # sends the user notification. Called exactly once+ # per research_id because the counter is popped above.+ self.notify_research_failed(+ username=username,+ research_id=research_id,+ error_message=(+ f"Failed to start research after {attempts} attempts"+ ),+ user_password=password,)def _start_research(@@ -617,8 +709,14 @@ class QueueProcessorV2:password: str,queued_research,):- """Start a queued research."""- # Update research status+ """Start a queued research.++ Spawns the background thread first, then persists state. If+ ``start_research_process`` raises, no DB mutation has happened+ yet, so the caller's error handler can safely retry without+ having to roll back IN_PROGRESS status or a pending active+ record.+ """research = (db_session.query(ResearchHistory).filter_by(id=queued_research.research_id)@@ -630,20 +728,6 @@ class QueueProcessorV2:f"Research {queued_research.research_id} not found")- research.status = ResearchStatus.IN_PROGRESS- db_session.commit()-- # Create active research record- active_record = UserActiveResearch(- username=username,- research_id=queued_research.research_id,- status=ResearchStatus.IN_PROGRESS,- thread_id="pending",- settings_snapshot=queued_research.settings_snapshot,- )- db_session.add(active_record)- db_session.commit()-# Extract settingssettings_snapshot = queued_research.settings_snapshot or {}@@ -658,7 +742,8 @@ class QueueProcessorV2:submission_params = settings_snapshotcomplete_settings = {}- # Start the research process with password+ # Spawn first. If this raises, caller retries with no leftover+ # DB state to clean up.research_thread = start_research_process(queued_research.research_id,queued_research.query,@@ -680,8 +765,18 @@ class QueueProcessorV2:settings_snapshot=complete_settings,)- # Update thread ID- active_record.thread_id = str(research_thread.ident) # type: ignore[assignment]+ # Thread is running. Persist state. If this commit fails the+ # thread still runs and writes its own terminal status via+ # queue_error_update; logging is sufficient.+ research.status = ResearchStatus.IN_PROGRESS+ active_record = UserActiveResearch(+ username=username,+ research_id=queued_research.research_id,+ status=ResearchStatus.IN_PROGRESS,+ thread_id=str(research_thread.ident),+ settings_snapshot=queued_research.settings_snapshot,+ )+ db_session.add(active_record)db_session.commit()def process_user_request(self, username: str, session_id: str) -> int:diff --git a/tests/web/queue/test_processor_v2_concurrency.py b/tests/web/queue/test_processor_v2_concurrency.pyindex f2130a1fba..26aa030e6b 100644--- a/tests/web/queue/test_processor_v2_concurrency.py+++ b/tests/web/queue/test_processor_v2_concurrency.py@@ -12,7 +12,6 @@ from unittest.mock import Mock, patchimport pytestfrom local_deep_research.web.queue.processor_v2 import QueueProcessorV2-from local_deep_research.constants import ResearchStatus# ---------------------------------------------------------------------------@@ -51,7 +50,8 @@ class TestStartQueuedResearchesErrorRecovery:"""When _start_research throws, verify is_processing reset and task FAILED."""def test_error_resets_is_processing(self, processor):- """On exception, is_processing must be set back to False (line 568)."""+ """On exception, is_processing must be set back to False so the+ next loop tick can retry."""queued = Mock()queued.is_processing = Falsequeued.research_id = "res-1"@@ -62,6 +62,8 @@ class TestStartQueuedResearchesErrorRecovery:mock_query.order_by.return_value = mock_querymock_query.limit.return_value = mock_querymock_query.all.return_value = [queued]+ # Error handler re-queries the row; return the same object.+ mock_query.first.return_value = queuedmock_session.query.return_value = mock_querymock_queue_service = Mock()@@ -75,8 +77,9 @@ class TestStartQueuedResearchesErrorRecovery:assert queued.is_processing is False- def test_error_sets_task_status_failed(self, processor):- """On exception, task status must be set to FAILED (line 574)."""+ def test_transient_error_leaves_queued_for_retry(self, processor):+ """On a single spawn failure, the queue row stays and is_processing+ is reset, allowing the next loop tick to retry."""queued = Mock()queued.is_processing = Falsequeued.research_id = "res-2"@@ -87,6 +90,7 @@ class TestStartQueuedResearchesErrorRecovery:mock_query.order_by.return_value = mock_querymock_query.limit.return_value = mock_querymock_query.all.return_value = [queued]+ mock_query.first.return_value = queuedmock_session.query.return_value = mock_querymock_queue_service = Mock()@@ -96,11 +100,86 @@ class TestStartQueuedResearchesErrorRecovery:mock_session, mock_queue_service, "alice", "pw", 1)- mock_queue_service.update_task_status.assert_any_call(- "res-2",- ResearchStatus.FAILED,- error_message="Failed to start research",+ # Row must remain queued for retry — not deleted, not marked FAILED.+ mock_session.delete.assert_not_called()+ assert queued.is_processing is False+ assert processor._spawn_retry_counts["res-2"] == 1++ def test_terminal_failure_after_retry_limit(self, processor):+ """After SPAWN_RETRY_LIMIT consecutive failures, row is deleted,+ ResearchHistory.status is set to FAILED, and notify_research_failed+ is invoked exactly once."""+ from local_deep_research.web.queue.processor_v2 import (+ SPAWN_RETRY_LIMIT,+ )++ queued = Mock()+ queued.is_processing = False+ queued.research_id = "res-3"++ research_row = Mock()++ mock_session = Mock()+ mock_query = Mock()+ mock_query.filter_by.return_value = mock_query+ mock_query.order_by.return_value = mock_query+ mock_query.limit.return_value = mock_query+ # Each iteration re-queries; both QueuedResearch and+ # ResearchHistory go through this chain.+ mock_query.all.return_value = [queued]+ mock_query.first.side_effect = lambda: (+ research_row+ if mock_query.filter_by.call_args is not None+ else queued)+ mock_session.query.return_value = mock_query++ mock_queue_service = Mock()+ processor._start_research = Mock(side_effect=RuntimeError("boom"))+ processor.notify_research_failed = Mock()++ # Simulate SPAWN_RETRY_LIMIT loop iterations: each call processes+ # the same queued row (mock returns it every time) and fails.+ for _ in range(SPAWN_RETRY_LIMIT):+ mock_query.first.return_value = queued # re-query returns row+ processor._start_queued_researches(+ mock_session, mock_queue_service, "alice", "pw", 1+ )++ # After exhausting attempts: terminal path triggered once.+ assert "res-3" not in processor._spawn_retry_counts+ assert processor.notify_research_failed.call_count == 1+ call_args = processor.notify_research_failed.call_args+ assert call_args.kwargs["username"] == "alice"+ assert call_args.kwargs["research_id"] == "res-3"++ def test_counter_resets_on_successful_start(self, processor):+ """If a research eventually succeeds after prior failures, the+ retry counter entry is popped so a future fresh failure gets a+ full retry budget."""+ queued = Mock()+ queued.is_processing = False+ queued.research_id = "res-4"++ mock_session = Mock()+ mock_query = Mock()+ mock_query.filter_by.return_value = mock_query+ mock_query.order_by.return_value = mock_query+ mock_query.limit.return_value = mock_query+ mock_query.all.return_value = [queued]+ mock_query.first.return_value = queued+ mock_session.query.return_value = mock_query++ mock_queue_service = Mock()+ processor._spawn_retry_counts["res-4"] = 2 # prior failures+ processor._start_research = Mock() # success this time++ processor._start_queued_researches(+ mock_session, mock_queue_service, "alice", "pw", 1+ )++ assert "res-4" not in processor._spawn_retry_counts+ mock_session.delete.assert_called_with(queued)def test_success_deletes_queued_record(self, processor):"""On success, the queued_research is deleted from DB."""