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.

refactor(queue): extract commit/delete helpers + clarify DuplicateResearchError semantics (#3521)

LearningCircuit23cbeb52fd72
diff --git a/src/local_deep_research/exceptions.py b/src/local_deep_research/exceptions.py
index d3941d2f2a..958259263b 100644
--- a/src/local_deep_research/exceptions.py
+++ b/src/local_deep_research/exceptions.py
@@ -14,8 +14,19 @@ class ResearchTerminatedException(BaseException):
class DuplicateResearchError(Exception):
- """Raised when ``start_research_process`` is called with a research_id
- that already has a live thread registered.
+ """Raised when a research should not be (re-)spawned.
+
+ Two triggering cases, both handled identically by callers:
+
+ 1. A live thread already exists in the active-research dict for this
+ ``research_id`` — typically a retry after a prior attempt's
+ post-spawn ``UserActiveResearch`` commit failed.
+ 2. The ``ResearchHistory.status`` is non-QUEUED (``IN_PROGRESS`` from
+ a prior attempt's pre-spawn commit that succeeded; terminal
+ ``COMPLETED`` / ``FAILED`` / ``SUSPENDED`` from a thread that
+ already finished and cleaned itself out of ``_active_research``).
+ Re-spawning would either contradict the live thread or re-run a
+ finished research.
Callers that wrap the spawn in ``except Exception`` to clean up orphan
state on spawn failure MUST catch ``DuplicateResearchError`` separately
diff --git a/src/local_deep_research/web/queue/processor_v2.py b/src/local_deep_research/web/queue/processor_v2.py
index e6db9546e1..86e972e954 100644
--- a/src/local_deep_research/web/queue/processor_v2.py
+++ b/src/local_deep_research/web/queue/processor_v2.py
@@ -114,6 +114,83 @@ class QueueProcessorV2:
self._spawn_retry_counts[research_id] = attempts
return attempts
+ @staticmethod
+ def _commit_with_safe_rollback(db_session, context: str) -> bool:
+ """Commit ``db_session`` with best-effort rollback on failure.
+
+ Returns ``True`` on success, ``False`` if the commit raised.
+ The failure path logs via ``logger.exception`` and attempts a
+ rollback, itself guarded so a subsequent rollback failure is
+ logged at debug level rather than propagated.
+
+ Extracted because the ``try: commit / except: log + try:
+ rollback`` idiom repeats at ≥5 sites in this module; inlining
+ hides the defensive structure behind nested ``try`` blocks and
+ makes each callsite longer than the work it describes.
+ """
+ try:
+ db_session.commit()
+ return True
+ except Exception:
+ logger.exception(f"Commit failed: {context}")
+ try:
+ db_session.rollback()
+ except Exception:
+ logger.debug(
+ f"Rollback after commit failure ({context})",
+ exc_info=True,
+ )
+ return False
+
+ def _delete_queue_row_safely(
+ self, db_session, username: str, research_id: str
+ ) -> None:
+ """Best-effort delete of the ``QueuedResearch`` row for
+ ``(username, research_id)``.
+
+ Rolls back any pending state first (the session may be in
+ ``PendingRollbackError`` from a failed commit inside
+ ``_start_research``), re-queries the row fresh, deletes it if
+ present, and commits via ``_commit_with_safe_rollback``.
+
+ Use this for ``DuplicateResearchError`` cleanup where the goal
+ is "drop the queue row regardless of session state." Do NOT
+ use it for paths that need the delete to be atomic with other
+ writes (e.g. the terminal FAILED path bundles
+ ``ResearchHistory.status = FAILED`` with the queue-row delete
+ in a single commit — that stays inline).
+ """
+ try:
+ db_session.rollback()
+ except Exception:
+ logger.debug(
+ f"Rollback before queue-row delete for {research_id}",
+ exc_info=True,
+ )
+ try:
+ fresh_queued = (
+ db_session.query(QueuedResearch)
+ .filter_by(username=username, research_id=research_id)
+ .first()
+ )
+ if fresh_queued:
+ db_session.delete(fresh_queued)
+ self._commit_with_safe_rollback(
+ db_session,
+ f"queue-row delete for research {research_id}",
+ )
+ except Exception:
+ logger.exception(
+ f"Failed to query/delete queue row for {research_id}"
+ )
+ try:
+ db_session.rollback()
+ except Exception:
+ logger.debug(
+ f"Rollback after queue-row delete failure for {research_id}",
+ exc_info=True,
+ )
+
def start(self):
"""Start the queue processor thread."""
if self.running:
@@ -673,20 +750,10 @@ class QueueProcessorV2:
+ (" and status=QUEUED" if status_changed else "")
)
if reclaimed:
- try:
- db_session.commit()
- except Exception:
- logger.exception(
- "Failed to commit reclaim of stranded rows for "
- f"user {username}"
- )
- try:
- db_session.rollback()
- except Exception:
- logger.debug(
- "Rollback after reclaim failure",
- exc_info=True,
- )
+ if not self._commit_with_safe_rollback(
+ db_session,
+ f"reclaim of stranded rows for user {username}",
+ ):
return 0
return reclaimed
@@ -766,48 +833,24 @@ class QueueProcessorV2:
)
except DuplicateResearchError:
- # A live thread already exists for this research_id — a
- # previous attempt spawned successfully but its post-spawn
- # commit (UserActiveResearch row) failed, so the queue row
- # is still here asking us to retry. Treat as started: clear
- # the queue row and the retry counter, and do NOT increment
- # the counter or fall through to the FAILED/notify path,
- # which would terminate-status a live thread.
+ # Raised by _start_research when a prior attempt's thread
+ # is still live, OR when the ResearchHistory row is in a
+ # non-QUEUED state (IN_PROGRESS from a prior attempt's
+ # successful pre-spawn commit; terminal COMPLETED /
+ # FAILED / SUSPENDED from a thread that already finished
+ # and cleaned up). In every case the correct behavior is
+ # the same: clear the stale queue row and the retry
+ # counter, and do NOT fall through to the FAILED/notify
+ # path — that would terminate-status a live thread or
+ # emit a false failure for a completed one.
logger.warning(
- f"Research {research_id} already has a live thread "
- "(prior attempt's post-spawn commit likely failed); "
- "treating as started and clearing queue row"
+ f"Research {research_id} is already started "
+ "(live thread or non-QUEUED status); clearing stale "
+ "queue row"
)
with self._spawn_retry_counts_lock:
self._spawn_retry_counts.pop(research_id, None)
- try:
- db_session.rollback()
- except Exception:
- logger.debug(
- "Rollback before dup cleanup",
- exc_info=True,
- )
- try:
- fresh_queued = (
- db_session.query(QueuedResearch)
- .filter_by(username=username, research_id=research_id)
- .first()
- )
- if fresh_queued:
- db_session.delete(fresh_queued)
- db_session.commit()
- except Exception:
- logger.exception(
- "Failed to delete queue row after "
- f"DuplicateResearchError for research {research_id}"
- )
- try:
- db_session.rollback()
- except Exception:
- logger.debug(
- "Rollback after dup cleanup failure",
- exc_info=True,
- )
+ self._delete_queue_row_safely(db_session, username, research_id)
continue
except Exception:
@@ -845,20 +888,10 @@ class QueueProcessorV2:
)
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,
- )
+ self._commit_with_safe_rollback(
+ db_session,
+ f"is_processing reset for research {research_id}",
+ )
continue
# Exhausted retries — mark terminal FAILED, delete the
@@ -953,8 +986,8 @@ class QueueProcessorV2:
# deletes the queue row without mutating status or notifying.
if research.status != ResearchStatus.QUEUED:
raise DuplicateResearchError(
- f"Research {research_id} has non-QUEUED status "
- f"{research.status}; not re-spawning"
+ f"Research {research_id} is already started "
+ f"(status={research.status})"
)
# Claim IN_PROGRESS before spawn to close the
@@ -1005,21 +1038,11 @@ class QueueProcessorV2:
except Exception:
# Genuine spawn failure: no thread exists. Roll back the
# IN_PROGRESS claim so the retry sees a clean QUEUED row.
- try:
- research.status = ResearchStatus.QUEUED
- db_session.commit()
- except Exception:
- logger.exception(
- "Failed to reset ResearchHistory.status to QUEUED "
- f"after spawn failure for research {research_id}"
- )
- try:
- db_session.rollback()
- except Exception:
- logger.debug(
- "Rollback after status-reset failure",
- exc_info=True,
- )
+ research.status = ResearchStatus.QUEUED
+ self._commit_with_safe_rollback(
+ db_session,
+ f"status reset to QUEUED after spawn failure for research {research_id}",
+ )
raise
# Thread is running. Record the active-research row. If this
@@ -1037,27 +1060,22 @@ class QueueProcessorV2:
thread_id=str(research_thread.ident),
settings_snapshot=queued_research.settings_snapshot,
)
- try:
- db_session.add(active_record)
- db_session.commit()
- except Exception as e:
- logger.exception(
- "Failed to persist UserActiveResearch after successful "
- f"spawn for research {research_id}; thread is live. "
- "Raising DuplicateResearchError so the caller's dup "
- "branch deletes the queue row without penalising retries."
- )
- try:
- db_session.rollback()
- except Exception:
- logger.debug(
- "Rollback after UserActiveResearch commit failure",
- exc_info=True,
- )
+ db_session.add(active_record)
+ if not self._commit_with_safe_rollback(
+ db_session,
+ f"UserActiveResearch persist after spawn for research {research_id}",
+ ):
+ # Thread is live; the commit failing leaves the UAR row
+ # unrecorded but the thread running. Raise
+ # DuplicateResearchError so the caller's dup branch deletes
+ # the queue row without bumping the retry counter — if we
+ # let a plain exception count as a spawn failure, a commit
+ # failure at SPAWN_RETRY_LIMIT - 1 would mark a LIVE thread
+ # as terminal FAILED.
raise DuplicateResearchError(
f"Research {research_id} thread is live; "
"UserActiveResearch commit failed"
- ) from e
+ )
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.py
index 2a52971cc5..aa4d20de19 100644
--- a/tests/web/queue/test_processor_v2_concurrency.py
+++ b/tests/web/queue/test_processor_v2_concurrency.py
@@ -41,6 +41,99 @@ def _mock_db_session():
yield mock_session
+# ---------------------------------------------------------------------------
+# _commit_with_safe_rollback / _delete_queue_row_safely helpers
+# ---------------------------------------------------------------------------
+
+
+class TestCommitWithSafeRollback:
+ """The commit-with-safe-rollback helper is the base primitive used
+ throughout _start_queued_researches; verify each branch in isolation."""
+
+ def test_commit_success_returns_true_no_rollback(self, processor):
+ mock_session = Mock()
+ result = processor._commit_with_safe_rollback(mock_session, "ctx")
+ assert result is True
+ mock_session.commit.assert_called_once()
+ mock_session.rollback.assert_not_called()
+
+ def test_commit_failure_returns_false_and_rolls_back(self, processor):
+ mock_session = Mock()
+ mock_session.commit.side_effect = RuntimeError("commit boom")
+ result = processor._commit_with_safe_rollback(mock_session, "ctx")
+ assert result is False
+ mock_session.commit.assert_called_once()
+ mock_session.rollback.assert_called_once()
+
+ def test_commit_and_rollback_both_raise_does_not_propagate(self, processor):
+ """If both commit and rollback fail, the helper must NOT
+ propagate — the caller has no way to recover and we are already
+ in a best-effort cleanup path."""
+ mock_session = Mock()
+ mock_session.commit.side_effect = RuntimeError("commit boom")
+ mock_session.rollback.side_effect = RuntimeError("rollback boom")
+ result = processor._commit_with_safe_rollback(mock_session, "ctx")
+ assert result is False
+
+
+class TestDeleteQueueRowSafely:
+ """The dup-branch cleanup helper: rollback-first, re-query, delete,
+ commit. All failures absorbed — this is a best-effort path."""
+
+ def _session_with_queued_row(self, queued_row):
+ """Mock session where the QueuedResearch filter_by().first()
+ returns ``queued_row`` (or None)."""
+ mock_session = Mock()
+ mock_query = Mock()
+ mock_query.filter_by.return_value = mock_query
+ mock_query.first.return_value = queued_row
+ mock_session.query.return_value = mock_query
+ return mock_session
+
+ def test_deletes_existing_row_and_commits(self, processor):
+ queued_row = Mock()
+ mock_session = self._session_with_queued_row(queued_row)
+
+ processor._delete_queue_row_safely(mock_session, "alice", "r-1")
+
+ mock_session.delete.assert_called_once_with(queued_row)
+ mock_session.commit.assert_called_once()
+
+ def test_no_row_present_does_not_call_delete_but_still_commits(
+ self, processor
+ ):
+ """If the row was already deleted by a concurrent worker / prior
+ retry, the helper must not call ``delete()`` with ``None`` but
+ still issue the commit to flush the preceding rollback."""
+ mock_session = self._session_with_queued_row(None)
+
+ processor._delete_queue_row_safely(mock_session, "alice", "r-1")
+
+ mock_session.delete.assert_not_called()
+ mock_session.commit.assert_called_once()
+
+ def test_initial_rollback_failure_does_not_prevent_delete(self, processor):
+ """The pre-emptive rollback is best-effort; if it fails we still
+ try the query+delete+commit sequence."""
+ queued_row = Mock()
+ mock_session = self._session_with_queued_row(queued_row)
+ mock_session.rollback.side_effect = RuntimeError("rollback boom")
+
+ processor._delete_queue_row_safely(mock_session, "alice", "r-1")
+
+ mock_session.delete.assert_called_once_with(queued_row)
+ mock_session.commit.assert_called_once()
+
+ def test_query_failure_is_absorbed(self, processor):
+ """If the re-query itself raises (session truly poisoned), the
+ helper must not propagate — the caller has no handler for this."""
+ mock_session = Mock()
+ mock_session.query.side_effect = RuntimeError("query boom")
+
+ # No exception expected.
+ processor._delete_queue_row_safely(mock_session, "alice", "r-1")
+
+
# ---------------------------------------------------------------------------
# _start_queued_researches error recovery
# ---------------------------------------------------------------------------