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 remaining thread leak sources (#2496)

LearningCircuitd1bf0a372325
diff --git a/src/local_deep_research/benchmarks/web_api/benchmark_service.py b/src/local_deep_research/benchmarks/web_api/benchmark_service.py
index 1b1bce8026..bd8c916471 100644
--- a/src/local_deep_research/benchmarks/web_api/benchmark_service.py
+++ b/src/local_deep_research/benchmarks/web_api/benchmark_service.py
@@ -612,6 +612,18 @@ class BenchmarkService:
# Clear thread-local settings context to prevent leaks
clear_settings_context()
+ # Clean up thread-local database session and NullPool engine.
+ # Benchmark threads may create DB sessions during task processing;
+ # without cleanup, engines accumulate leaking ~3 FDs each.
+ try:
+ from ...database.thread_local_session import (
+ cleanup_current_thread,
+ )
+
+ cleanup_current_thread()
+ except Exception:
+ pass
+
# Clean up active run tracking
if benchmark_run_id in self.active_runs:
# Mark that thread is done but keep data for database update
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 0e9538937a..5d0a45e6fc 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -2400,6 +2400,16 @@ def _auto_index_documents_worker(
except Exception:
logger.exception("Auto-indexing worker failed")
+ finally:
+ # Clean up thread-local database session and NullPool engine.
+ # This worker runs in a ThreadPoolExecutor; without cleanup,
+ # engines accumulate in _thread_engines leaking ~3 FDs each.
+ try:
+ from ...database.thread_local_session import cleanup_current_thread
+
+ cleanup_current_thread()
+ except Exception:
+ pass
def _background_index_worker(