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(fetcher): add context manager to ContentFetcher for cleanup (#3199)

LearningCircuit977a3305524e
diff --git a/src/local_deep_research/advanced_search_system/strategies/mcp_strategy.py b/src/local_deep_research/advanced_search_system/strategies/mcp_strategy.py
index 300624b319..b9abfaff3a 100644
--- a/src/local_deep_research/advanced_search_system/strategies/mcp_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/mcp_strategy.py
@@ -1146,49 +1146,51 @@ class MCPSearchStrategy(BaseSearchStrategy):
try:
from local_deep_research.content_fetcher import ContentFetcher
- fetcher = ContentFetcher(timeout=self.CONTENT_FETCHER_TIMEOUT)
- result = fetcher.fetch(url, max_length=max_length)
-
- if result.get("status") == "success":
- content = result.get("content", "")
- source_type = result.get("source_type", "unknown")
- title = result.get("title", "")
-
- # Format output
- output_parts = []
- if title:
- output_parts.append(f"Title: {title}")
- output_parts.append(f"Source: {source_type}")
- output_parts.append(f"URL: {url}")
- output_parts.append("")
- output_parts.append(content)
-
- formatted_content = "\n".join(output_parts)
-
- # Track as a finding
- self._findings.append(
- {
- "type": "content_download",
- "url": url,
- "source_type": source_type,
- "title": title,
- "content_length": len(content),
- }
- )
+ with ContentFetcher(
+ timeout=self.CONTENT_FETCHER_TIMEOUT
+ ) as fetcher:
+ result = fetcher.fetch(url, max_length=max_length)
+
+ if result.get("status") == "success":
+ content = result.get("content", "")
+ source_type = result.get("source_type", "unknown")
+ title = result.get("title", "")
+
+ # Format output
+ output_parts = []
+ if title:
+ output_parts.append(f"Title: {title}")
+ output_parts.append(f"Source: {source_type}")
+ output_parts.append(f"URL: {url}")
+ output_parts.append("")
+ output_parts.append(content)
+
+ formatted_content = "\n".join(output_parts)
+
+ # Track as a finding
+ self._findings.append(
+ {
+ "type": "content_download",
+ "url": url,
+ "source_type": source_type,
+ "title": title,
+ "content_length": len(content),
+ }
+ )
- # Track source
- self._sources.append(url)
+ # Track source
+ self._sources.append(url)
- logger.info(
- f"Downloaded {len(content)} chars from {source_type}: {url}"
- )
+ logger.info(
+ f"Downloaded {len(content)} chars from {source_type}: {url}"
+ )
- return {"status": "success", "content": formatted_content}
+ return {"status": "success", "content": formatted_content}
- else:
- error_msg = result.get("error", "Download failed")
- logger.warning(f"Content download failed: {error_msg}")
- return {"status": "error", "content": error_msg}
+ else:
+ error_msg = result.get("error", "Download failed")
+ logger.warning(f"Content download failed: {error_msg}")
+ return {"status": "error", "content": error_msg}
except ImportError:
logger.warning("ContentFetcher not available")
diff --git a/src/local_deep_research/content_fetcher/fetcher.py b/src/local_deep_research/content_fetcher/fetcher.py
index 6391cd1c4b..eac24f1f7a 100644
--- a/src/local_deep_research/content_fetcher/fetcher.py
+++ b/src/local_deep_research/content_fetcher/fetcher.py
@@ -13,6 +13,7 @@ from loguru import logger
from .url_classifier import URLClassifier, URLType
from ..research_library.downloaders.base import ContentType
from ..security.ssrf_validator import validate_url
+from ..utilities.resource_utils import safe_close
# Default maximum content length (500KB of text)
DEFAULT_MAX_CONTENT_LENGTH = 500_000
@@ -301,3 +302,16 @@ class ContentFetcher:
"source_name": URLClassifier.get_source_name(url_type),
"extracted_id": URLClassifier.extract_id(url, url_type),
}
+
+ def close(self):
+ """Close all cached downloaders and their HTTP sessions."""
+ for url_type, downloader in self._downloaders.items():
+ safe_close(downloader, f"downloader-{url_type.value}")
+ self._downloaders.clear()
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self.close()
+ return False