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: Improve WebSocket handling and add OpenAI availability tracking

hashedvikingc812471e05d9
diff --git a/app.py b/app.py
index 55e99bba34..bb93b9d711 100644
--- a/app.py
+++ b/app.py
@@ -8,9 +8,13 @@ from flask import Flask, render_template, request, jsonify, send_from_directory,
from flask_socketio import SocketIO, emit
from search_system import AdvancedSearchSystem
from report_generator import IntegratedReportGenerator
+# Move this import up to ensure it's available globally
from dateutil import parser
import traceback
+# Set flag for tracking OpenAI availability - we'll check it only when needed
+OPENAI_AVAILABLE = False
+
# Initialize Flask app
app = Flask(__name__, static_folder=os.path.abspath('static'))
app.config['SECRET_KEY'] = 'deep-research-secret-key'
@@ -25,7 +29,9 @@ socketio = SocketIO(
async_mode='threading',
path='/research/socket.io',
logger=True,
- engineio_logger=True
+ engineio_logger=True,
+ ping_timeout=20,
+ ping_interval=5
)
# Active research processes and socket subscriptions
@@ -64,6 +70,18 @@ def add_security_headers(response):
return response
+# Add a middleware layer to handle abrupt disconnections
+def handle_websocket_requests():
+ if request.path.startswith('/research/socket.io'):
+ try:
+ if not request.environ.get('werkzeug.socket'):
+ return
+ except Exception as e:
+ print(f"WebSocket preprocessing error: {e}")
+ # Return empty response to prevent further processing
+ return '', 200
+
# Initialize the database
def init_db():
conn = sqlite3.connect(DB_PATH)
@@ -396,13 +414,16 @@ def handle_connect():
@socketio.on('disconnect')
def handle_disconnect():
- print(f"Client disconnected: {request.sid}")
- # 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 not subscribers:
- socket_subscriptions.pop(research_id, None)
+ try:
+ print(f"Client disconnected: {request.sid}")
+ # 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 not subscribers:
+ socket_subscriptions.pop(research_id, None)
+ except Exception as e:
+ print(f"Error handling disconnect: {e}")
@socketio.on('subscribe_to_research')
def handle_subscribe(data):
@@ -428,7 +449,15 @@ def handle_subscribe(data):
@socketio.on_error
def handle_socket_error(e):
- print(f"SocketIO error: {str(e)}")
+ print(f"Socket.IO error: {str(e)}")
+ # Don't propagate exceptions to avoid crashing the server
+ return False
+
[email protected]_error_default
+def handle_default_error(e):
+ print(f"Unhandled Socket.IO error: {str(e)}")
+ # Don't propagate exceptions to avoid crashing the server
+ return False
def run_research_process(research_id, query, mode):
try:
@@ -814,5 +843,23 @@ def favicon():
return send_from_directory(app.static_folder, 'favicon.ico', mimetype='image/x-icon')
if __name__ == '__main__':
- # Set server name explicitly to avoid DNS resolution issues
- socketio.run(app, debug=True, host='0.0.0.0', port=5000, allow_unsafe_werkzeug=True)
\ No newline at end of file
+ # Check for OpenAI availability but don't import it unless necessary
+ try:
+ import os
+ api_key = os.environ.get("OPENAI_API_KEY")
+ if api_key:
+ try:
+ # Only try to import if we have an API key
+ import openai
+ openai.api_key = api_key
+ OPENAI_AVAILABLE = True
+ print("OpenAI integration is available")
+ except ImportError:
+ print("OpenAI package not installed, integration disabled")
+ else:
+ print("OPENAI_API_KEY not found in environment variables, OpenAI integration disabled")
+ except Exception as e:
+ print(f"Error checking OpenAI availability: {e}")
+
+ # Run with threading (more stable than eventlet with complex dependencies)
+ socketio.run(app, debug=False, host='0.0.0.0', port=5000, allow_unsafe_werkzeug=False)
\ No newline at end of file