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: thread-safe rate limiting and silent exception logging (#2946)

LearningCircuit4200855dba30
diff --git a/src/local_deep_research/database/session_context.py b/src/local_deep_research/database/session_context.py
index 582a34c72f..dcebb5253f 100644
--- a/src/local_deep_research/database/session_context.py
+++ b/src/local_deep_research/database/session_context.py
@@ -11,6 +11,7 @@ from flask import (
g,
has_app_context,
has_request_context,
+ jsonify,
session as flask_session,
)
from loguru import logger
@@ -213,6 +214,7 @@ def ensure_db_session(view_func: Callable) -> Callable:
logger.exception(
f"Failed to ensure database session for {username}"
)
+ return jsonify({"error": "Database session unavailable"}), 500
return view_func(*args, **kwargs)
diff --git a/src/local_deep_research/llm/providers/implementations/custom_openai_endpoint.py b/src/local_deep_research/llm/providers/implementations/custom_openai_endpoint.py
index c6a249b8c5..b0afbbb76b 100644
--- a/src/local_deep_research/llm/providers/implementations/custom_openai_endpoint.py
+++ b/src/local_deep_research/llm/providers/implementations/custom_openai_endpoint.py
@@ -54,7 +54,10 @@ class CustomOpenAIEndpointProvider(OpenAICompatibleProvider):
if api_key and str(api_key).strip():
return True
except Exception:
- pass
+ logger.debug(
+ f"Error reading API key setting '{cls.api_key_setting}'",
+ exc_info=True,
+ )
try:
custom_url = get_setting_from_snapshot(
@@ -67,7 +70,10 @@ class CustomOpenAIEndpointProvider(OpenAICompatibleProvider):
if normalized.rstrip("/") != cls.default_base_url.rstrip("/"):
return True
except Exception:
- pass
+ logger.debug(
+ f"Error reading URL setting '{cls.url_setting}'",
+ exc_info=True,
+ )
return False
diff --git a/src/local_deep_research/web/api.py b/src/local_deep_research/web/api.py
index 89ef2d4c47..b24acd636e 100644
--- a/src/local_deep_research/web/api.py
+++ b/src/local_deep_research/web/api.py
@@ -3,6 +3,7 @@ REST API for Local Deep Research.
Provides HTTP access to programmatic search and research capabilities.
"""
+import threading
import time
from functools import wraps
from typing import Dict, Any
@@ -21,6 +22,7 @@ api_blueprint = Blueprint("api_v1", __name__, url_prefix="/api/v1")
# Rate limiting data store: {ip_address: [timestamp1, timestamp2, ...]}
rate_limit_data = {}
+_rate_limit_lock = threading.Lock()
def api_access_control(f):
@@ -70,30 +72,31 @@ def api_access_control(f):
client_ip = request.remote_addr
current_time = time.time()
- # Initialize or clean up old requests for this IP
- if client_ip not in rate_limit_data:
- rate_limit_data[client_ip] = []
-
- # Remove timestamps older than the rate limit window
- rate_limit_data[client_ip] = [
- ts
- for ts in rate_limit_data[client_ip]
- if current_time - ts < RATE_LIMIT_WINDOW_SECONDS
- ]
-
- # Check if rate limit is exceeded
- if len(rate_limit_data[client_ip]) >= rate_limit:
- return (
- jsonify(
- {
- "error": f"Rate limit exceeded. Maximum {rate_limit} requests per {RATE_LIMIT_WINDOW_SECONDS} seconds allowed."
- }
- ),
- 429,
- )
+ with _rate_limit_lock:
+ # Initialize or clean up old requests for this IP
+ if client_ip not in rate_limit_data:
+ rate_limit_data[client_ip] = []
+
+ # Remove timestamps older than the rate limit window
+ rate_limit_data[client_ip] = [
+ ts
+ for ts in rate_limit_data[client_ip]
+ if current_time - ts < RATE_LIMIT_WINDOW_SECONDS
+ ]
+
+ # Check if rate limit is exceeded
+ if len(rate_limit_data[client_ip]) >= rate_limit:
+ return (
+ jsonify(
+ {
+ "error": f"Rate limit exceeded. Maximum {rate_limit} requests per {RATE_LIMIT_WINDOW_SECONDS} seconds allowed."
+ }
+ ),
+ 429,
+ )
- # Add current timestamp to the list
- rate_limit_data[client_ip].append(current_time)
+ # Add current timestamp to the list
+ rate_limit_data[client_ip].append(current_time)
return f(*args, **kwargs)
diff --git a/src/local_deep_research/web/queue/processor_v2.py b/src/local_deep_research/web/queue/processor_v2.py
index 7ad236db6b..14c726789f 100644
--- a/src/local_deep_research/web/queue/processor_v2.py
+++ b/src/local_deep_research/web/queue/processor_v2.py
@@ -303,7 +303,9 @@ class QueueProcessorV2:
db_session.delete(active_record)
db_session.commit()
except Exception:
- pass
+ logger.exception(
+ f"Failed to clean up active research record for {research_id}"
+ )
def notify_research_completed(
self, username: str, research_id: str, user_password: str = None