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(rag): surface cancellation status write failures (#5365)

Ziyang Guo9bc6aeed65b8
diff --git a/changelog.d/+cancel-indexing-write-failure.bugfix.md b/changelog.d/+cancel-indexing-write-failure.bugfix.md
new file mode 100644
index 0000000000..39ecd6262b
--- /dev/null
+++ b/changelog.d/+cancel-indexing-write-failure.bugfix.md
@@ -0,0 +1 @@
+Cancelling collection indexing now returns an error when the cancellation status cannot be persisted, instead of reporting success while the task remains active.
diff --git a/src/local_deep_research/research_library/routes/rag_routes.py b/src/local_deep_research/research_library/routes/rag_routes.py
index d48f20ed06..7ac64efc85 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -3189,7 +3189,7 @@ def _do_update_task_status(
task.status = status
# Set completed_at for ALL terminal statuses so
# ``cleanup_old_tasks`` (which filters on
- # ``status in ["completed", "failed"] AND
+ # ``status in ["completed", "failed", "cancelled"] AND
# completed_at < cutoff_date``) can reap them.
# Previously only "completed" set the timestamp,
# leaving failed/cancelled rows permanent.
@@ -3531,9 +3531,12 @@ def cancel_indexing(collection_id):
), 404
if matched_task:
- # Mark as cancelled via _update_task_status so completed_at is set for reaping
+ # This endpoint must surface a failed cancellation write.
+ # Background workers use the best-effort wrapper, but returning
+ # success here after it swallowed an error would mislead the
+ # caller while the task remains active.
task_id_to_cancel = matched_task.task_id
- _update_task_status(
+ _do_update_task_status(
username,
db_password,
task_id_to_cancel,
diff --git a/tests/research_library/routes/test_rag_routes_indexing_coverage.py b/tests/research_library/routes/test_rag_routes_indexing_coverage.py
index e12e8e1dfb..c3fd4daf70 100644
--- a/tests/research_library/routes/test_rag_routes_indexing_coverage.py
+++ b/tests/research_library/routes/test_rag_routes_indexing_coverage.py
@@ -938,6 +938,85 @@ class TestCancelIndexing:
assert data["success"] is False
assert data["error"] == "No active indexing task for this collection"
+ def test_cancel_indexing_updates_task_via_strict_updater(self, app):
+ """A matched task uses the updater whose failures propagate."""
+ task = Mock()
+ task.task_id = "task-1"
+ task.status = "processing"
+ task.metadata_json = {"collection_id": "coll-1"}
+
+ db_session = _make_db_session()
+ db_session.query = Mock(
+ return_value=_build_mock_query(first_result=task)
+ )
+
+ mock_password_store = Mock()
+ mock_password_store.get_session_password.return_value = "db-pass"
+
+ with (
+ patch(f"{MODULE}._do_update_task_status") as mock_update,
+ _auth_client(
+ app,
+ mock_db_session=db_session,
+ extra_patches=[
+ patch(
+ f"{_DB_PASS}.session_password_store",
+ mock_password_store,
+ ),
+ ],
+ ) as (client, ctx),
+ ):
+ resp = client.post("/library/api/collections/coll-1/index/cancel")
+
+ assert resp.status_code == 200
+ assert resp.get_json()["success"] is True
+ mock_update.assert_called_once_with(
+ "testuser",
+ "db-pass",
+ "task-1",
+ status="cancelled",
+ progress_message="Cancellation requested...",
+ )
+
+ def test_cancel_indexing_returns_500_when_status_update_fails(self, app):
+ """An exhausted status write cannot produce a false success."""
+ task = Mock()
+ task.task_id = "task-1"
+ task.status = "processing"
+ task.metadata_json = {"collection_id": "coll-1"}
+
+ db_session = _make_db_session()
+ db_session.query = Mock(
+ return_value=_build_mock_query(first_result=task)
+ )
+
+ mock_password_store = Mock()
+ mock_password_store.get_session_password.return_value = "db-pass"
+
+ with (
+ patch(
+ f"{MODULE}._do_update_task_status",
+ side_effect=RuntimeError("write failed"),
+ ),
+ _auth_client(
+ app,
+ mock_db_session=db_session,
+ extra_patches=[
+ patch(
+ f"{_DB_PASS}.session_password_store",
+ mock_password_store,
+ ),
+ ],
+ ) as (client, ctx),
+ ):
+ resp = client.post("/library/api/collections/coll-1/index/cancel")
+
+ assert resp.status_code == 500
+ assert resp.get_json() == {
+ "success": False,
+ "error": "Failed to cancel indexing. Please try again.",
+ }
+
def test_cancel_indexing_active_sse_stream(self, app):
"""Cancels an active SSE indexing stream via process-local registry."""
import threading