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: prevent DetachedInstanceError in remaining route functions (#2141)

LearningCircuitf36c1e619a44
diff --git a/src/local_deep_research/web/routes/api_routes.py b/src/local_deep_research/web/routes/api_routes.py
index 51f58379e3..87a680d051 100644
--- a/src/local_deep_research/web/routes/api_routes.py
+++ b/src/local_deep_research/web/routes/api_routes.py
@@ -149,21 +149,20 @@ def api_research_status(research_id):
.first()
)
- if research is None:
- return jsonify({"error": "Research not found"}), 404
+ if research is None:
+ return jsonify({"error": "Research not found"}), 404
- # Get metadata
- metadata = research.research_meta or {}
-
- return jsonify(
- {
+ # Extract attributes while session is active
+ # to avoid DetachedInstanceError after the with block exits
+ result = {
"status": research.status,
"progress": research.progress,
"completed_at": research.completed_at,
"report_path": research.report_path,
- "metadata": metadata,
+ "metadata": research.research_meta or {},
}
- )
+
+ return jsonify(result)
except Exception:
logger.exception("Error getting research status")
return jsonify(
@@ -270,10 +269,10 @@ def api_add_resource(research_id):
.first()
)
- if not research:
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
+ if not research:
+ return jsonify(
+ {"status": "error", "message": "Research not found"}
+ ), 404
# Add the resource
resource_id = add_resource(
diff --git a/src/local_deep_research/web/routes/history_routes.py b/src/local_deep_research/web/routes/history_routes.py
index a7dbfe2d62..af8f9542c4 100644
--- a/src/local_deep_research/web/routes/history_routes.py
+++ b/src/local_deep_research/web/routes/history_routes.py
@@ -176,21 +176,23 @@ def get_research_status(research_id):
db_session.query(ResearchHistory).filter_by(id=research_id).first()
)
- if not research:
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
-
- result = {
- "id": research.id,
- "query": research.query,
- "mode": research.mode,
- "status": research.status,
- "created_at": research.created_at,
- "completed_at": research.completed_at,
- "progress_log": research.progress_log,
- "report_path": research.report_path,
- }
+ if not research:
+ return jsonify(
+ {"status": "error", "message": "Research not found"}
+ ), 404
+
+ # Extract attributes while session is active
+ # to avoid DetachedInstanceError after the with block exits
+ result = {
+ "id": research.id,
+ "query": research.query,
+ "mode": research.mode,
+ "status": research.status,
+ "created_at": research.created_at,
+ "completed_at": research.completed_at,
+ "progress_log": research.progress_log,
+ "report_path": research.report_path,
+ }
globals_dict = get_globals()
active_research = globals_dict["active_research"]
@@ -229,18 +231,28 @@ def get_research_details(research_id):
try:
with get_user_db_session(username) as db_session:
- # Log all research IDs for this user
- all_research = db_session.query(
- ResearchHistory.id, ResearchHistory.query
- ).all()
- logger.debug(f"All research count for user: {len(all_research)}")
-
research = (
db_session.query(ResearchHistory)
.filter_by(id=research_id)
.first()
)
logger.debug(f"Research found: {research.id if research else None}")
+
+ if not research:
+ logger.error(f"Research not found for id: {research_id}")
+ return jsonify(
+ {"status": "error", "message": "Research not found"}
+ ), 404
+
+ # Extract all needed attributes while session is active
+ # to avoid DetachedInstanceError after the with block exits
+ research_data = {
+ "query": research.query,
+ "mode": research.mode,
+ "status": research.status,
+ "created_at": research.created_at,
+ "completed_at": research.completed_at,
+ }
except Exception:
logger.exception("Database error")
return jsonify(
@@ -250,12 +262,6 @@ def get_research_details(research_id):
}
), 500
- if not research:
- logger.error(f"Research not found for id: {research_id}")
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
-
# Get logs from the dedicated log database
logs = get_logs_for_research(research_id)
@@ -285,16 +291,18 @@ def get_research_details(research_id):
return jsonify(
{
"research_id": research_id,
- "query": research.query,
- "mode": research.mode,
- "status": research.status,
+ "query": research_data["query"],
+ "mode": research_data["mode"],
+ "status": research_data["status"],
"strategy": strategy_name,
"progress": active_research.get(research_id, {}).get(
"progress",
- 100 if research.status == ResearchStatus.COMPLETED else 0,
+ 100
+ if research_data["status"] == ResearchStatus.COMPLETED
+ else 0,
),
- "created_at": research.created_at,
- "completed_at": research.completed_at,
+ "created_at": research_data["created_at"],
+ "completed_at": research_data["completed_at"],
"log": logs,
}
)
@@ -413,10 +421,10 @@ def get_research_logs(research_id):
db_session.query(ResearchHistory).filter_by(id=research_id).first()
)
- if not research:
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
+ if not research:
+ return jsonify(
+ {"status": "error", "message": "Research not found"}
+ ), 404
# Retrieve logs from the database
logs = get_logs_for_research(research_id)
diff --git a/src/local_deep_research/web/routes/research_routes.py b/src/local_deep_research/web/routes/research_routes.py
index 73ae51682c..0fad153708 100644
--- a/src/local_deep_research/web/routes/research_routes.py
+++ b/src/local_deep_research/web/routes/research_routes.py
@@ -1115,44 +1115,46 @@ def get_history():
.all()
)
- history_items = []
- for research in research_records:
- # Calculate duration if completed
- duration_seconds = None
- if research.completed_at and research.created_at:
- try:
- duration_seconds = calculate_duration(
- research.created_at, research.completed_at
- )
- except Exception:
- logger.exception("Error calculating duration")
+ # Build history items while session is active to avoid
+ # DetachedInstanceError on ORM attribute access
+ history_items = []
+ for research in research_records:
+ # Calculate duration if completed
+ duration_seconds = None
+ if research.completed_at and research.created_at:
+ try:
+ duration_seconds = calculate_duration(
+ research.created_at, research.completed_at
+ )
+ except Exception:
+ logger.exception("Error calculating duration")
- # Count documents in the library for this research
- doc_count = (
- db_session.query(Document)
- .filter_by(research_id=research.id)
- .count()
- )
+ # Count documents in the library for this research
+ doc_count = (
+ db_session.query(Document)
+ .filter_by(research_id=research.id)
+ .count()
+ )
- # Create a history item
- item = {
- "id": research.id,
- "query": research.query,
- "mode": research.mode,
- "status": research.status,
- "created_at": research.created_at,
- "completed_at": research.completed_at,
- "duration_seconds": duration_seconds,
- "report_path": research.report_path,
- "metadata": research.research_meta, # Include metadata for news
- "document_count": doc_count, # Add document count
- }
+ # Create a history item
+ item = {
+ "id": research.id,
+ "query": research.query,
+ "mode": research.mode,
+ "status": research.status,
+ "created_at": research.created_at,
+ "completed_at": research.completed_at,
+ "duration_seconds": duration_seconds,
+ "report_path": research.report_path,
+ "metadata": research.research_meta, # Include metadata for news
+ "document_count": doc_count, # Add document count
+ }
- # Add title if it exists
- if hasattr(research, "title") and research.title is not None:
- item["title"] = research.title
+ # Add title if it exists
+ if hasattr(research, "title") and research.title is not None:
+ item["title"] = research.title
- history_items.append(item)
+ history_items.append(item)
return jsonify({"status": "success", "items": history_items})
except Exception:
@@ -1227,16 +1229,18 @@ def get_research_logs(research_id):
.all()
)
- logs = []
- for row in log_results:
- logs.append(
- {
- "id": row.id,
- "message": row.message,
- "timestamp": row.timestamp,
- "log_type": row.level,
- }
- )
+ # Extract log attributes while session is active
+ # to avoid DetachedInstanceError on ORM attribute access
+ logs = []
+ for row in log_results:
+ logs.append(
+ {
+ "id": row.id,
+ "message": row.message,
+ "timestamp": row.timestamp,
+ "log_type": row.level,
+ }
+ )
return jsonify(logs)