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: add cleanup_current_thread() to prevent file descriptor leaks (#2495)

LearningCircuitfa62daf1ffab
diff --git a/src/local_deep_research/news/subscription_manager/scheduler.py b/src/local_deep_research/news/subscription_manager/scheduler.py
index 7585a0ccc7..7896c28a35 100644
--- a/src/local_deep_research/news/subscription_manager/scheduler.py
+++ b/src/local_deep_research/news/subscription_manager/scheduler.py
@@ -1368,6 +1368,17 @@ class NewsScheduler:
)
finally:
clear_settings_context()
+ # Defensive: clean up thread-local database session.
+ # Caller (_check_subscription) also has cleanup, but this ensures
+ # the function is self-contained if called from other contexts.
+ try:
+ from ...database.thread_local_session import (
+ cleanup_current_thread,
+ )
+
+ cleanup_current_thread()
+ except Exception:
+ pass
def _store_research_result(
self,
diff --git a/src/local_deep_research/research_library/routes/rag_routes.py b/src/local_deep_research/research_library/routes/rag_routes.py
index e2f3f4661e..0e9538937a 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -2069,6 +2069,15 @@ def index_collection(collection_id):
result_queue.put(r)
except Exception as ex:
error_queue.put(ex)
+ finally:
+ try:
+ from ...database.thread_local_session import (
+ cleanup_current_thread,
+ )
+
+ cleanup_current_thread()
+ except Exception:
+ pass
thread = threading.Thread(target=index_in_thread)
thread.start()
@@ -2548,6 +2557,15 @@ def _background_index_worker(
status="failed",
error_message=str(e),
)
+ finally:
+ # Clean up thread-local database session and NullPool engine.
+ # Background indexing threads create DB sessions that leak ~3 FDs without this.
+ try:
+ from ...database.thread_local_session import cleanup_current_thread
+
+ cleanup_current_thread()
+ except Exception:
+ pass
def _update_task_status(
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index 4f99794ac3..28819bf691 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -537,6 +537,17 @@ def apply_middleware(app):
except Exception:
logger.warning("Error closing request session during cleanup")
+ # Also clean up thread-local database session created by database_middleware.
+ # The g.db_session above is a separate QueuePool session; the middleware creates
+ # a NullPool engine per thread via get_metrics_session(). Without this cleanup,
+ # _thread_engines grows unboundedly as Werkzeug spawns new threads per request.
+ try:
+ from ..database.thread_local_session import cleanup_current_thread
+
+ cleanup_current_thread()
+ except Exception:
+ pass
+
# Add a middleware layer to handle abrupt disconnections
@app.before_request
def handle_websocket_requests():
diff --git a/src/local_deep_research/web/auth/routes.py b/src/local_deep_research/web/auth/routes.py
index 50b20aa86d..a3c2f26036 100644
--- a/src/local_deep_research/web/auth/routes.py
+++ b/src/local_deep_research/web/auth/routes.py
@@ -249,6 +249,15 @@ def _perform_post_login_tasks(username: str, password: str) -> None:
logger.info(f"Post-login tasks completed for user {username}")
+ # Clean up thread-local database session created during post-login DB operations.
+ # This daemon thread opens NullPool engines that leak ~3 FDs without cleanup.
+ try:
+ from ...database.thread_local_session import cleanup_current_thread
+
+ cleanup_current_thread()
+ except Exception:
+ pass
+
@auth_bp.route("/register", methods=["GET"])
def register_page():
diff --git a/src/local_deep_research/web/queue/processor_v2.py b/src/local_deep_research/web/queue/processor_v2.py
index 716f1b2caa..53fe6325bf 100644
--- a/src/local_deep_research/web/queue/processor_v2.py
+++ b/src/local_deep_research/web/queue/processor_v2.py
@@ -425,6 +425,18 @@ class QueueProcessorV2:
except Exception:
logger.exception("Error in queue processor loop")
+ finally:
+ # Clean up thread-local database session after each iteration.
+ # This long-running daemon thread repeatedly opens NullPool engines
+ # via get_user_db_session(); without cleanup, FDs accumulate.
+ try:
+ from ...database.thread_local_session import (
+ cleanup_current_thread,
+ )
+
+ cleanup_current_thread()
+ except Exception:
+ pass
time.sleep(self.check_interval)
diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.py
index 6b9fcc37f0..c6579d46f3 100644
--- a/src/local_deep_research/web/services/research_service.py
+++ b/src/local_deep_research/web/services/research_service.py
@@ -1643,6 +1643,15 @@ def run_research_process(
clear_search_context()
clear_settings_context()
+ # Clean up thread-local database session and NullPool engine to prevent
+ # FD leaks (~3 FDs per research run from WAL-mode SQLCipher).
+ try:
+ from ...database.thread_local_session import cleanup_current_thread
+
+ cleanup_current_thread()
+ except Exception:
+ pass
+
def cleanup_research_resources(
research_id, active_research, termination_flags, username=None