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.

feat: Improve socket.io event handling and client disconnection

hashedviking8d39422dfbbf
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index bb00b4fded..70d8e3b698 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -165,9 +165,11 @@ def register_socket_events(socketio):
print(f"Client connected: {request.sid}")
@socketio.on("disconnect")
- def handle_disconnect():
+ def handle_disconnect(sid=None):
try:
- print(f"Client disconnected: {request.sid}")
+ # Use the sid parameter if provided, otherwise fall back to request.sid
+ client_sid = sid if sid else request.sid
+ print(f"Client disconnected: {client_sid}")
# Import to avoid circular imports
from .routes.research_routes import get_globals
globals_dict = get_globals()
@@ -175,16 +177,16 @@ def register_socket_events(socketio):
# Clean up subscriptions for this client
for research_id, subscribers in list(socket_subscriptions.items()):
- if request.sid in subscribers:
- subscribers.remove(request.sid)
+ if client_sid in subscribers:
+ subscribers.remove(client_sid)
if not subscribers:
socket_subscriptions.pop(research_id, None)
print(f"Removed empty subscription for research {research_id}")
except Exception as e:
print(f"Error handling disconnect: {e}")
- @socketio.on("subscribe_to_research")
- def handle_subscribe(data):
+ @socketio.on("join")
+ def handle_join(data):
from .routes.research_routes import get_globals
globals_dict = get_globals()
active_research = globals_dict['active_research']
@@ -205,67 +207,49 @@ def register_socket_events(socketio):
# Only allow subscription to valid research
if result:
status = result[0]
-
- # Initialize subscription set if needed
+ # Allow subscription to any research that exists
+ sid = request.sid
+
+ # Initialize this research key if needed
if research_id not in socket_subscriptions:
socket_subscriptions[research_id] = set()
-
+
# Add this client to the subscribers
- socket_subscriptions[research_id].add(request.sid)
- print(f"Client {request.sid} subscribed to research {research_id}")
-
- # Send current status immediately if available
+ socket_subscriptions[research_id].add(sid)
+ print(f"Client {sid} joined research {research_id}")
+
+ # Send initial data immediately (especially important for completed research)
if research_id in active_research:
- progress = active_research[research_id]["progress"]
- latest_log = (
- active_research[research_id]["log"][-1]
- if active_research[research_id]["log"]
- else None
- )
-
- if latest_log:
- socketio.emit(
- f"research_progress_{research_id}",
- {
- "progress": progress,
- "message": latest_log.get("message", "Processing..."),
- "status": "in_progress",
- "log_entry": latest_log,
- },
- room=request.sid
- )
- elif status in ["completed", "failed", "suspended"]:
- # Send final status for completed research
- socketio.emit(
- f"research_progress_{research_id}",
- {
- "progress": 100 if status == "completed" else 0,
- "message": (
- "Research completed successfully"
- if status == "completed"
- else (
- "Research failed"
- if status == "failed"
- else "Research was suspended"
- )
- ),
- "status": status,
- "log_entry": {
- "time": datetime.utcnow().isoformat(),
- "message": f"Research is {status}",
- "progress": 100 if status == "completed" else 0,
- "metadata": {
- "phase": (
- "complete" if status == "completed" else "error"
- )
- },
- },
- },
- room=request.sid
- )
+ # Send current progress
+ current_data = active_research[research_id].copy()
+ socketio.emit(f"progress_{research_id}", current_data, room=sid)
+ else:
+ # Get details from database for completed research
+ conn = get_db_connection()
+ conn.row_factory = lambda cursor, row: {
+ column[0]: row[idx] for idx, column in enumerate(cursor.description)
+ }
+ cursor = conn.cursor()
+ cursor.execute("SELECT * FROM research_history WHERE id = ?", (research_id,))
+ result = cursor.fetchone()
+ conn.close()
+
+ if result:
+ # Send completion data
+ socketio.emit(f"progress_{research_id}", {
+ "status": result["status"],
+ "progress": 100 if result["status"] == "completed" else 0,
+ "current_task": "Research completed" if result["status"] == "completed" else "Research not active"
+ }, room=sid)
else:
- # Research not found
- socketio.emit("error", {"message": f"Research ID {research_id} not found"}, room=request.sid)
+ print(f"Attempted to subscribe to invalid research: {research_id}")
+ else:
+ print("No research_id provided in subscribe event")
+
+ @socketio.on("subscribe_to_research")
+ def handle_subscribe(data):
+ """Legacy handler for backward compatibility"""
+ handle_join(data)
@socketio.on_error
def handle_socket_error(e):