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: Address AI code review feedback on thread pool management

LearningCircuitef696151c178
diff --git a/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py b/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py
index e42b3b6eb1..a5bf8da090 100644
--- a/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py
+++ b/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py
@@ -17,6 +17,16 @@ class ParallelSearchEngine(BaseSearchEngine):
"""
Parallel search engine that executes multiple search engines simultaneously.
Uses LLM to select appropriate engines based on query, then runs them all in parallel.
+
+ Usage:
+ Recommended: Use as context manager to ensure proper cleanup:
+ with ParallelSearchEngine(llm) as engine:
+ results = engine.run("query")
+
+ Alternative: Manually call shutdown() when done:
+ engine = ParallelSearchEngine(llm)
+ results = engine.run("query")
+ engine.shutdown() # Important: cleanup thread pool
"""
def __init__(
@@ -31,11 +41,22 @@ class ParallelSearchEngine(BaseSearchEngine):
max_filtered_results: Optional[int] = None,
settings_snapshot: Optional[Dict[str, Any]] = None,
programmatic_mode: bool = False,
+ max_workers: Optional[
+ int
+ ] = None, # Thread pool size (None = auto-detect)
**kwargs,
):
"""
Initialize the parallel search engine.
+ Important: This class should be used as a context manager or have shutdown()
+ called explicitly to ensure proper thread pool cleanup.
+
+ Example:
+ with ParallelSearchEngine(llm) as engine:
+ results = engine.run("query")
+ # Thread pool automatically cleaned up
+
Args:
llm: Language model instance for query classification
max_results: Maximum number of search results to return per engine
@@ -47,6 +68,8 @@ class ParallelSearchEngine(BaseSearchEngine):
max_filtered_results: Maximum number of results to keep after filtering
settings_snapshot: Settings snapshot for thread context
programmatic_mode: If True, disables database operations and metrics tracking
+ max_workers: Thread pool size. If None, uses Python's recommended formula:
+ min(32, (os.cpu_count() or 1) + 4) for I/O-bound operations
**kwargs: Additional parameters (ignored but accepted for compatibility)
"""
# Override max_filtered_results to be much higher for parallel search
@@ -85,13 +108,27 @@ class ParallelSearchEngine(BaseSearchEngine):
# Persistent thread pool for parallel searches (performance optimization)
# Reusing threads avoids repeated thread creation/destruction overhead
- self.max_workers = kwargs.get("max_workers", 20)
- self._executor = concurrent.futures.ThreadPoolExecutor(
- max_workers=self.max_workers, thread_name_prefix="parallel_search_"
- )
- logger.debug(
- f"Initialized persistent ThreadPool with {self.max_workers} workers"
- )
+ # Use Python's recommended formula for I/O-bound operations if not specified
+ import os
+
+ if max_workers is None:
+ self.max_workers = min(32, (os.cpu_count() or 1) + 4)
+ else:
+ self.max_workers = max_workers
+
+ try:
+ self._executor = concurrent.futures.ThreadPoolExecutor(
+ max_workers=self.max_workers,
+ thread_name_prefix="parallel_search_",
+ )
+ logger.debug(
+ f"Initialized persistent ThreadPool with {self.max_workers} workers"
+ )
+ except Exception:
+ logger.exception(
+ "Failed to create ThreadPoolExecutor, parallel search will not work"
+ )
+ self._executor = None
# Get available engines (excluding 'meta', 'auto', and 'parallel')
self.available_engines = self._get_available_engines()
@@ -510,6 +547,13 @@ Select up to {self.max_engines_to_select} engines that would best answer this qu
all_results = []
engine_results = {}
+ # Check if thread pool is available
+ if self._executor is None:
+ logger.error(
+ "Thread pool not available, cannot execute parallel search"
+ )
+ return []
+
# Submit all tasks to the persistent executor
future_to_engine = {
self._executor.submit(
@@ -677,14 +721,6 @@ Select up to {self.max_engines_to_select} engines that would best answer this qu
except Exception:
logger.exception("Error during ThreadPool shutdown")
- def __del__(self):
- """Ensure thread pool is shutdown on deletion."""
- try:
- self.shutdown(wait=False)
- except Exception:
- # Silently ignore errors during cleanup
- pass
-
def __enter__(self):
"""Support using as context manager."""
return self