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: Handle edge cases in research termination API

LearningCircuit6611f8f08165
diff --git a/src/local_deep_research/web/routes/research_routes.py b/src/local_deep_research/web/routes/research_routes.py
index 399801ff91..14ca2a9330 100644
--- a/src/local_deep_research/web/routes/research_routes.py
+++ b/src/local_deep_research/web/routes/research_routes.py
@@ -637,16 +637,13 @@ def terminate_research(research_id):
status = research.status
- # If it's not in progress, return an error
- if status != "in_progress":
- return (
- jsonify(
- {
- "status": "error",
- "message": "Research is not in progress",
- }
- ),
- 400,
+ # If it's already completed or suspended, return success
+ if status in ["completed", "suspended", "error"]:
+ return jsonify(
+ {
+ "status": "success",
+ "message": f"Research already {status}",
+ }
)
# Check if it's in the active_research dict
diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.py
index 4709b12719..ed4a01fcba 100644
--- a/src/local_deep_research/web/services/research_service.py
+++ b/src/local_deep_research/web/services/research_service.py
@@ -1424,42 +1424,73 @@ def cancel_research(research_id, username=None):
Returns:
bool: True if the research was found and cancelled, False otherwise
"""
- # Import globals from research routes
- from ..routes.globals import get_globals
+ try:
+ # Import globals from research routes
+ from ..routes.globals import get_globals
- globals_dict = get_globals()
- active_research = globals_dict["active_research"]
- termination_flags = globals_dict["termination_flags"]
+ globals_dict = get_globals()
+ active_research = globals_dict["active_research"]
+ termination_flags = globals_dict["termination_flags"]
- # Set termination flag
- termination_flags[research_id] = True
+ # Set termination flag
+ termination_flags[research_id] = True
- # Check if the research is active
- if research_id in active_research:
- # Call handle_termination to update database
- handle_termination(research_id, active_research, termination_flags)
- return True
- else:
- # Update database directly if not found in active_research
- # Get username from parameter or session
- if not username:
- from flask import session
+ # Check if the research is active
+ if research_id in active_research:
+ # Call handle_termination to update database
+ handle_termination(
+ research_id, active_research, termination_flags, username
+ )
+ return True
+ else:
+ # Update database directly if not found in active_research
+ # Get username from parameter or session
+ if not username:
+ from flask import session
- username = session.get("username")
+ username = session.get("username")
- if not username:
- return False
+ if not username:
+ logger.warning(
+ f"No username available for cancelling research {research_id}"
+ )
+ return False
- with get_user_db_session(username) as db_session:
- research = (
- db_session.query(ResearchHistory)
- .filter_by(id=research_id)
- .first()
- )
- if not research:
+ try:
+ with get_user_db_session(username) as db_session:
+ research = (
+ db_session.query(ResearchHistory)
+ .filter_by(id=research_id)
+ .first()
+ )
+ if not research:
+ logger.info(
+ f"Research {research_id} not found in database"
+ )
+ return False
+
+ # Check if already completed or suspended
+ if research.status in ["completed", "suspended", "error"]:
+ logger.info(
+ f"Research {research_id} already in terminal state: {research.status}"
+ )
+ return True # Consider this a success since it's already stopped
+
+ # If it exists but isn't in active_research, still update status
+ research.status = "suspended"
+ db_session.commit()
+ logger.info(
+ f"Successfully suspended research {research_id}"
+ )
+ except Exception as e:
+ logger.exception(
+ f"Error accessing database for research {research_id}: {e}"
+ )
return False
- # If it exists but isn't in active_research, still update status
- research.status = "suspended"
- db_session.commit()
return True
+ except Exception as e:
+ logger.exception(
+ f"Unexpected error in cancel_research for {research_id}: {e}"
+ )
+ return False