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: close leaked file descriptors causing 'Too many open files' (#2708)

LearningCircuit91f7be96fb87
diff --git a/src/local_deep_research/api/research_functions.py b/src/local_deep_research/api/research_functions.py
index 1c3e2b3601..d305098a82 100644
--- a/src/local_deep_research/api/research_functions.py
+++ b/src/local_deep_research/api/research_functions.py
@@ -20,6 +20,47 @@ from ..utilities.search_utilities import remove_think_tags
from .settings_utils import create_settings_snapshot
+def _close_llm(llm):
+ """Close underlying HTTP clients held by LLM instances.
+
+ Only ChatOllama creates per-instance httpx.Client objects that need
+ explicit close(). ChatAnthropic uses @lru_cache'd shared httpx
+ clients that must NOT be closed.
+ """
+ try:
+ # Unwrap ProcessingLLMWrapper
+ base = getattr(llm, "base_llm", llm)
+ # ChatOllama path: base._client is ollama.Client,
+ # base._client._client is httpx.Client
+ ollama_client = getattr(base, "_client", None)
+ if ollama_client is not None:
+ # Only close Ollama clients — Anthropic's are @lru_cache'd and shared.
+ # This relies on ollama's module structure; if the ollama package
+ # reorganises its internals this guard may need updating.
+ if not type(ollama_client).__module__.startswith("ollama"):
+ return
+ httpx_client = getattr(ollama_client, "_client", None)
+ if httpx_client is not None and hasattr(httpx_client, "close"):
+ httpx_client.close()
+ except Exception:
+ pass
+
+
+def _close_system(system):
+ """Close an AdvancedSearchSystem and its associated resources."""
+ try:
+ system.close()
+ except Exception:
+ pass
+ if hasattr(system, "search") and system.search is not None:
+ try:
+ system.search.close()
+ except Exception:
+ pass
+ if hasattr(system, "model"):
+ _close_llm(system.model)
+
+
def _init_search_system(
model_name: str | None = None,
temperature: float = 0.7,
@@ -102,38 +143,42 @@ def _init_search_system(
# Set the search engine if specified or get from settings
search_engine = None
- # If no search_tool provided, get from settings_snapshot
- if not search_tool and settings_snapshot:
- search_tool = get_setting_from_snapshot(
- "search.tool", settings_snapshot=settings_snapshot
- )
+ try:
+ # If no search_tool provided, get from settings_snapshot
+ if not search_tool and settings_snapshot:
+ search_tool = get_setting_from_snapshot(
+ "search.tool", settings_snapshot=settings_snapshot
+ )
- if search_tool:
- search_engine = get_search(
- search_tool,
- llm_instance=llm,
+ if search_tool:
+ search_engine = get_search(
+ search_tool,
+ llm_instance=llm,
+ username=username,
+ settings_snapshot=settings_snapshot,
+ programmatic_mode=programmatic_mode,
+ )
+ if search_engine is None:
+ logger.warning(
+ f"Could not create search engine '{search_tool}', using default."
+ )
+
+ # Create search system with custom parameters
+ logger.info("Search strategy: {}", search_strategy)
+ system = AdvancedSearchSystem(
+ llm=llm,
+ search=search_engine,
+ strategy_name=search_strategy,
username=username,
+ research_id=research_id,
+ research_context=research_context,
settings_snapshot=settings_snapshot,
programmatic_mode=programmatic_mode,
+ search_original_query=search_original_query,
)
- if search_engine is None:
- logger.warning(
- f"Could not create search engine '{search_tool}', using default."
- )
-
- # Create search system with custom parameters
- logger.info("Search strategy: {}", search_strategy)
- system = AdvancedSearchSystem(
- llm=llm,
- search=search_engine,
- strategy_name=search_strategy,
- username=username,
- research_id=research_id,
- research_context=research_context,
- settings_snapshot=settings_snapshot,
- programmatic_mode=programmatic_mode,
- search_original_query=search_original_query,
- )
+ except Exception:
+ _close_llm(llm)
+ raise
# Override default settings with user-provided values
system.max_iterations = iterations
@@ -274,6 +319,7 @@ def quick_summary(
}
set_search_context(search_context)
+ system = None
try:
# Remove research_mode from kwargs before passing to _init_search_system
init_kwargs = {k: v for k, v in kwargs.items() if k != "research_mode"}
@@ -306,6 +352,8 @@ def quick_summary(
"sources": results.get("all_links_of_system", []),
}
finally:
+ if system is not None:
+ _close_system(system)
clear_search_context()
@@ -410,38 +458,42 @@ def generate_report(
register_llm(name, llm_instance)
logger.info(f"Registered {len(llms)} LLMs: {list(llms.keys())}")
- system = _init_search_system(retrievers=retrievers, llms=llms, **kwargs)
-
- # Set progress callback if provided
- if progress_callback:
- system.set_progress_callback(progress_callback)
-
- # Perform the initial research
- initial_findings = system.analyze_topic(query)
-
- # Generate the structured report
- report_generator = IntegratedReportGenerator(
- search_system=system,
- llm=system.model,
- searches_per_section=searches_per_section,
- settings_snapshot=kwargs.get("settings_snapshot"),
- )
- report = report_generator.generate_report(initial_findings, query)
-
- # Save report to file if path is provided
- if output_file and report and "content" in report:
- from ..security.file_write_verifier import write_file_verified
-
- write_file_verified(
- output_file,
- report["content"],
- "api.allow_file_output",
- context="API research report",
+ system = None
+ try:
+ system = _init_search_system(retrievers=retrievers, llms=llms, **kwargs)
+ # Set progress callback if provided
+ if progress_callback:
+ system.set_progress_callback(progress_callback)
+
+ # Perform the initial research
+ initial_findings = system.analyze_topic(query)
+
+ # Generate the structured report
+ report_generator = IntegratedReportGenerator(
+ search_system=system,
+ llm=system.model,
+ searches_per_section=searches_per_section,
settings_snapshot=kwargs.get("settings_snapshot"),
)
- logger.info(f"Report saved to {output_file}")
- report["file_path"] = output_file
- return report
+ report = report_generator.generate_report(initial_findings, query)
+
+ # Save report to file if path is provided
+ if output_file and report and "content" in report:
+ from ..security.file_write_verifier import write_file_verified
+
+ write_file_verified(
+ output_file,
+ report["content"],
+ "api.allow_file_output",
+ context="API research report",
+ settings_snapshot=kwargs.get("settings_snapshot"),
+ )
+ logger.info(f"Report saved to {output_file}")
+ report["file_path"] = output_file
+ return report
+ finally:
+ if system is not None:
+ _close_system(system)
@no_db_settings
@@ -503,6 +555,7 @@ def detailed_research(
}
set_search_context(search_context)
+ system = None
try:
# Initialize system
system = _init_search_system(retrievers=retrievers, llms=llms, **kwargs)
@@ -530,6 +583,8 @@ def detailed_research(
},
}
finally:
+ if system is not None:
+ _close_system(system)
clear_search_context()
@@ -562,97 +617,109 @@ def analyze_documents(
f"Analyzing documents in collection '{collection_name}' for query: {query}"
)
- # Get language model with custom temperature
- llm = get_llm(temperature=temperature)
-
- # Get search engine for the specified collection
- search = get_search(collection_name, llm_instance=llm)
-
- if not search:
- return {
- "summary": f"Error: Collection '{collection_name}' not found or not properly configured.",
- "documents": [],
- }
+ llm = None
+ search = None
+ try:
+ # Get language model with custom temperature
+ llm = get_llm(temperature=temperature)
- # Set max results
- search.max_results = max_results
+ # Get search engine for the specified collection
+ search = get_search(collection_name, llm_instance=llm)
- # Perform the search
- results = search.run(query)
+ if not search:
+ _close_llm(llm)
+ llm = None
+ return {
+ "summary": f"Error: Collection '{collection_name}' not found or not properly configured.",
+ "documents": [],
+ }
- if not results:
- return {
- "summary": f"No documents found in collection '{collection_name}' for query: '{query}'",
- "documents": [],
- }
+ # Set max results
+ search.max_results = max_results
+ # Perform the search
+ results = search.run(query)
- # Get LLM to generate a summary of the results
+ if not results:
+ return {
+ "summary": f"No documents found in collection '{collection_name}' for query: '{query}'",
+ "documents": [],
+ }
- docs_text = "\n\n".join(
- [
- f"Document {i + 1}:"
- f" {doc.get('content', doc.get('snippet', ''))[:1000]}"
- for i, doc in enumerate(results[:5])
- ]
- ) # Limit to first 5 docs and 1000 chars each
+ # Get LLM to generate a summary of the results
- summary_prompt = f"""Analyze these document excerpts related to the query: "{query}"
+ docs_text = "\n\n".join(
+ [
+ f"Document {i + 1}:"
+ f" {doc.get('content', doc.get('snippet', ''))[:1000]}"
+ for i, doc in enumerate(results[:5])
+ ]
+ ) # Limit to first 5 docs and 1000 chars each
- {docs_text}
+ summary_prompt = f"""Analyze these document excerpts related to the query: "{query}"
- Provide a concise summary of the key information found in these documents related to the query.
- """
+ {docs_text}
- import time
+ Provide a concise summary of the key information found in these documents related to the query.
+ """
- llm_start_time = time.time()
- logger.info(
- f"Starting LLM summary generation (prompt length: {len(summary_prompt)} chars)..."
- )
+ import time
- summary_response = llm.invoke(summary_prompt)
+ llm_start_time = time.time()
+ logger.info(
+ f"Starting LLM summary generation (prompt length: {len(summary_prompt)} chars)..."
+ )
- llm_elapsed = time.time() - llm_start_time
- logger.info(f"LLM summary generation completed in {llm_elapsed:.2f}s")
+ summary_response = llm.invoke(summary_prompt)
- if hasattr(summary_response, "content"):
- summary = remove_think_tags(summary_response.content)
- else:
- summary = str(summary_response)
-
- # Create result dictionary
- analysis_result = {
- "summary": summary,
- "documents": results,
- "collection": collection_name,
- "document_count": len(results),
- }
+ llm_elapsed = time.time() - llm_start_time
+ logger.info(f"LLM summary generation completed in {llm_elapsed:.2f}s")
- # Save to file if requested
- if output_file:
- from ..security.file_write_verifier import write_file_verified
+ if hasattr(summary_response, "content"):
+ summary = remove_think_tags(summary_response.content)
+ else:
+ summary = str(summary_response)
- content = f"# Document Analysis: {query}\n\n"
- content += f"## Summary\n\n{summary}\n\n"
- content += f"## Documents Found: {len(results)}\n\n"
+ # Create result dictionary
+ analysis_result = {
+ "summary": summary,
+ "documents": results,
+ "collection": collection_name,
+ "document_count": len(results),
+ }
- for i, doc in enumerate(results):
- content += (
- f"### Document {i + 1}: {doc.get('title', 'Untitled')}\n\n"
+ # Save to file if requested
+ if output_file:
+ from ..security.file_write_verifier import write_file_verified
+
+ content = f"# Document Analysis: {query}\n\n"
+ content += f"## Summary\n\n{summary}\n\n"
+ content += f"## Documents Found: {len(results)}\n\n"
+
+ for i, doc in enumerate(results):
+ content += (
+ f"### Document {i + 1}: {doc.get('title', 'Untitled')}\n\n"
+ )
+ content += f"**Source:** {doc.get('link', 'Unknown')}\n\n"
+ content += f"**Content:**\n\n{doc.get('content', doc.get('snippet', 'No content available'))[:1000]}...\n\n"
+ content += "---\n\n"
+
+ write_file_verified(
+ output_file,
+ content,
+ "api.allow_file_output",
+ context="API document analysis",
+ settings_snapshot=None, # analyze_documents doesn't support programmatic mode yet
)
- content += f"**Source:** {doc.get('link', 'Unknown')}\n\n"
- content += f"**Content:**\n\n{doc.get('content', doc.get('snippet', 'No content available'))[:1000]}...\n\n"
- content += "---\n\n"
-
- write_file_verified(
- output_file,
- content,
- "api.allow_file_output",
- context="API document analysis",
- settings_snapshot=None, # analyze_documents doesn't support programmatic mode yet
- )
- analysis_result["file_path"] = output_file
- logger.info(f"Analysis saved to {output_file}")
+ analysis_result["file_path"] = output_file
+ logger.info(f"Analysis saved to {output_file}")
- return analysis_result
+ return analysis_result
+ finally:
+ if search is not None:
+ try:
+ search.close()
+ except Exception:
+ pass
+ if llm is not None:
+ _close_llm(llm)
diff --git a/src/local_deep_research/web_search_engines/engines/meta_search_engine.py b/src/local_deep_research/web_search_engines/engines/meta_search_engine.py
index a03743695f..668c580a9d 100644
--- a/src/local_deep_research/web_search_engines/engines/meta_search_engine.py
+++ b/src/local_deep_research/web_search_engines/engines/meta_search_engine.py
@@ -517,6 +517,24 @@ Example output: searxng,wikipedia,brave"""
return engine
+ def close(self):
+ """Close all cached child search engines, fallback engine, and own resources."""
+ for engine in self.engine_cache.values():
+ try:
+ engine.close()
+ except Exception:
+ pass
+ self.engine_cache.clear()
+ if (
+ hasattr(self, "fallback_engine")
+ and self.fallback_engine is not None
+ ):
+ try:
+ self.fallback_engine.close()
+ except Exception:
+ pass
+ super().close()
+
def invoke(self, query: str) -> List[Dict[str, Any]]:
"""Compatibility method for LangChain tools"""
return self.run(query)
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 e5d7cf44a0..b6bcf00e16 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
@@ -740,6 +740,16 @@ Select up to {self.max_engines_to_select} engines that would best answer this qu
return all_full_content
+ def close(self):
+ """Close all cached child search engines and own resources."""
+ for engine in self.engine_cache.values():
+ try:
+ engine.close()
+ except Exception:
+ pass
+ self.engine_cache.clear()
+ super().close()
+
def invoke(self, query: str) -> List[Dict[str, Any]]:
"""Compatibility method for LangChain tools"""
return self.run(query)