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: comprehensive file descriptor leak prevention (#1860)

LearningCircuit5e748e815546
diff --git a/.pre-commit-hooks/check-service-context-managers.py b/.pre-commit-hooks/check-service-context-managers.py
index f160489d46..3c633f80a7 100755
--- a/.pre-commit-hooks/check-service-context-managers.py
+++ b/.pre-commit-hooks/check-service-context-managers.py
@@ -1,12 +1,15 @@
#!/usr/bin/env python3
"""
-Pre-commit hook to detect services with close() methods that are instantiated
-without context managers.
+Pre-commit hook to detect resources (services, database sessions) that are
+instantiated without context managers.
Services like DownloadService, LibraryRAGService, and LocalEmbeddingManager
hold resources (file handles, connections, models) that need to be released.
They should be used with context managers (with statements) to ensure cleanup.
+Functions like get_auth_db_session() return raw sessions that require manual
+cleanup. Use the auth_db_session() context manager instead.
+
Exceptions:
- Factory functions that return services (caller is responsible for cleanup)
- Services passed to other objects (dependency injection)
@@ -18,13 +21,19 @@ import sys
from typing import List, Optional, Set, Tuple
-# Services that require context manager usage
+# Services (classes) that require context manager usage
SERVICES_REQUIRING_CONTEXT = {
"DownloadService",
"LibraryRAGService",
"LocalEmbeddingManager",
}
+# Functions returning resources that need cleanup
+# Maps function name -> suggested context manager replacement
+FUNCTIONS_REQUIRING_CONTEXT = {
+ "get_auth_db_session": "auth_db_session()",
+}
+
# Files/patterns where direct instantiation is allowed
ALLOWED_PATTERNS = {
"tests/", # Test files often mock or need special handling
@@ -34,6 +43,9 @@ ALLOWED_PATTERNS = {
"download_service.py",
"library_rag_service.py",
"search_engine_local.py",
+ # Database implementation files
+ "auth_db.py",
+ "encrypted_db.py",
}
@@ -45,20 +57,38 @@ def is_file_allowed(filename: str) -> bool:
return False
-def get_service_name(node: ast.expr) -> Optional[str]:
- """Check if an expression is a service instantiation."""
+def get_resource_name(node: ast.expr) -> Optional[Tuple[str, Optional[str]]]:
+ """
+ Check if an expression is a resource that requires cleanup.
+
+ Returns:
+ Tuple of (resource_name, suggested_replacement) or None if not a resource.
+ For services, suggested_replacement is None (use 'with ServiceName(...) as var:').
+ For functions, suggested_replacement is the context manager to use instead.
+ """
if not isinstance(node, ast.Call):
return None
- # Check for ServiceName() pattern
+ func_name = None
+
+ # Check for Name() pattern (e.g., ServiceName() or get_auth_db_session())
if isinstance(node.func, ast.Name):
- if node.func.id in SERVICES_REQUIRING_CONTEXT:
- return node.func.id
+ func_name = node.func.id
+
+ # Check for module.Name() pattern (e.g., module.ServiceName())
+ elif isinstance(node.func, ast.Attribute):
+ func_name = node.func.attr
- # Check for module.ServiceName() pattern
- if isinstance(node.func, ast.Attribute):
- if node.func.attr in SERVICES_REQUIRING_CONTEXT:
- return node.func.attr
+ if func_name is None:
+ return None
+
+ # Check if it's a service class
+ if func_name in SERVICES_REQUIRING_CONTEXT:
+ return (func_name, None)
+
+ # Check if it's a function returning a resource
+ if func_name in FUNCTIONS_REQUIRING_CONTEXT:
+ return (func_name, FUNCTIONS_REQUIRING_CONTEXT[func_name])
return None
@@ -77,32 +107,43 @@ class FunctionScopeAnalyzer(ast.NodeVisitor):
def visit_Try(self, node: ast.Try) -> None:
"""Find variables that are closed in finally blocks within this scope."""
if node.finalbody:
- for stmt in node.finalbody:
- if isinstance(stmt, ast.Expr) and isinstance(
- stmt.value, ast.Call
- ):
- call = stmt.value
- if (
- isinstance(call.func, ast.Attribute)
- and call.func.attr == "close"
- and isinstance(call.func.value, ast.Name)
- ):
- self.close_vars.add(call.func.value.id)
+ self._find_close_calls(node.finalbody)
self.generic_visit(node)
+ def _find_close_calls(self, stmts: List) -> None:
+ """Recursively find .close() calls in a list of statements."""
+ for stmt in stmts:
+ if isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Call):
+ call = stmt.value
+ if (
+ isinstance(call.func, ast.Attribute)
+ and call.func.attr == "close"
+ and isinstance(call.func.value, ast.Name)
+ ):
+ self.close_vars.add(call.func.value.id)
+ # Recursively check nested try blocks in finally
+ elif isinstance(stmt, ast.Try):
+ self._find_close_calls(stmt.body)
+ for handler in stmt.handlers:
+ self._find_close_calls(handler.body)
+ self._find_close_calls(stmt.finalbody)
+ elif isinstance(stmt, ast.If):
+ self._find_close_calls(stmt.body)
+ self._find_close_calls(stmt.orelse)
+
def visit_With(self, node: ast.With) -> None:
- """Mark with statements that use services as context managers."""
+ """Mark with statements that use resources as context managers."""
for item in node.items:
- service_name = get_service_name(item.context_expr)
- if service_name:
+ resource_info = get_resource_name(item.context_expr)
+ if resource_info:
self.safe_with_lines.add(item.context_expr.lineno)
self.generic_visit(node)
def visit_AsyncWith(self, node: ast.AsyncWith) -> None:
- """Mark async with statements that use services as context managers."""
+ """Mark async with statements that use resources as context managers."""
for item in node.items:
- service_name = get_service_name(item.context_expr)
- if service_name:
+ resource_info = get_resource_name(item.context_expr)
+ if resource_info:
self.safe_with_lines.add(item.context_expr.lineno)
self.generic_visit(node)
@@ -110,9 +151,9 @@ class FunctionScopeAnalyzer(ast.NodeVisitor):
"""Track variables that are returned (factory function pattern)."""
if node.value and isinstance(node.value, ast.Name):
self.returned_vars.add(node.value.id)
- # Also handle direct service returns
- if node.value and get_service_name(node.value):
- # This is a direct return of a service - mark the line as safe
+ # Also handle direct resource returns
+ if node.value and get_resource_name(node.value):
+ # This is a direct return of a resource - mark the line as safe
self.safe_with_lines.add(node.value.lineno)
self.generic_visit(node)
@@ -299,11 +340,13 @@ class ServiceContextChecker(ast.NodeVisitor):
returned_vars: Set[str],
passed_to_args_vars: Set[str],
) -> None:
- """Check an assignment for improper service usage."""
- service_name = get_service_name(node.value)
- if not service_name:
+ """Check an assignment for improper resource usage."""
+ resource_info = get_resource_name(node.value)
+ if not resource_info:
return
+ resource_name, suggested_replacement = resource_info
+
# Get the variable name being assigned
var_name = None
if node.targets and isinstance(node.targets[0], ast.Name):
@@ -327,13 +370,24 @@ class ServiceContextChecker(ast.NodeVisitor):
return
# Not safe - flag this
- self.errors.append(
- (
- node.lineno,
- f"{service_name} should be used with a context manager "
- f"('with {service_name}(...) as var:') to ensure proper cleanup.",
+ if suggested_replacement:
+ # Function with a known replacement context manager
+ self.errors.append(
+ (
+ node.lineno,
+ f"{resource_name}() returns a resource that needs cleanup. "
+ f"Use 'with {suggested_replacement} as var:' instead.",
+ )
+ )
+ else:
+ # Service class
+ self.errors.append(
+ (
+ node.lineno,
+ f"{resource_name} should be used with a context manager "
+ f"('with {resource_name}(...) as var:') to ensure proper cleanup.",
+ )
)
- )
def check_file(filename: str) -> bool:
@@ -389,9 +443,10 @@ def main():
if has_errors:
print("\n" + "=" * 70)
- print("Resource Leak Prevention: Services need context managers!")
+ print("Resource Leak Prevention: Use context managers for cleanup!")
print("=" * 70)
- print("\nServices like DownloadService, LibraryRAGService, and")
+ print("\n--- Services ---")
+ print("Services like DownloadService, LibraryRAGService, and")
print("LocalEmbeddingManager hold resources that need cleanup.")
print("\nTo fix, use context managers:")
print(" # Before (leaks resources):")
@@ -401,13 +456,25 @@ def main():
print(" # After (proper cleanup):")
print(" with DownloadService() as service:")
print(" result = service.download(...)")
- print("\nAlternatively, use try/finally with explicit close():")
+ print("\n--- Database Sessions ---")
+ print("get_auth_db_session() returns a raw session that needs cleanup.")
+ print("Use auth_db_session() context manager instead:")
+ print()
+ print(" # Before (may leak on exception):")
+ print(" session = get_auth_db_session()")
+ print(" user = session.query(User).first()")
+ print(" session.close()")
+ print()
+ print(" # After (proper cleanup):")
+ print(" with auth_db_session() as session:")
+ print(" user = session.query(User).first()")
+ print("\n--- Alternative: try/finally ---")
print(" service = DownloadService()")
print(" try:")
print(" result = service.download(...)")
print(" finally:")
print(" service.close()")
- print("\nNote: Factory functions that return services are allowed,")
+ print("\nNote: Factory functions that return resources are allowed,")
print("as the caller is responsible for cleanup.")
sys.exit(1)
else:
diff --git a/examples/api_usage/http/advanced/http_api_examples.py b/examples/api_usage/http/advanced/http_api_examples.py
index d65746bdec..b271050e81 100755
--- a/examples/api_usage/http/advanced/http_api_examples.py
+++ b/examples/api_usage/http/advanced/http_api_examples.py
@@ -44,7 +44,7 @@ sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "src"))
from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.models import User
-from local_deep_research.database.auth_db import get_auth_db_session
+from local_deep_research.database.auth_db import auth_db_session
# Configuration
BASE_URL = "http://localhost:5000"
@@ -59,11 +59,10 @@ def create_test_user():
try:
# Create user in auth database
- auth_db = get_auth_db_session()
- new_user = User(username=username)
- auth_db.add(new_user)
- auth_db.commit()
- auth_db.close()
+ with auth_db_session() as session:
+ new_user = User(username=username)
+ session.add(new_user)
+ session.commit()
# Create encrypted database for user
db_manager = DatabaseManager()
diff --git a/examples/api_usage/http/advanced/simple_http_example.py b/examples/api_usage/http/advanced/simple_http_example.py
index eaae331be0..38bd5ffbc5 100755
--- a/examples/api_usage/http/advanced/simple_http_example.py
+++ b/examples/api_usage/http/advanced/simple_http_example.py
@@ -36,7 +36,7 @@ sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "src"))
from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.models import User
-from local_deep_research.database.auth_db import get_auth_db_session
+from local_deep_research.database.auth_db import auth_db_session
# Configuration
API_URL = "http://localhost:5000"
@@ -51,11 +51,10 @@ def create_test_user():
try:
# Create user in auth database
- auth_db = get_auth_db_session()
- new_user = User(username=username)
- auth_db.add(new_user)
- auth_db.commit()
- auth_db.close()
+ with auth_db_session() as session:
+ new_user = User(username=username)
+ session.add(new_user)
+ session.commit()
# Create encrypted database for user
db_manager = DatabaseManager()
diff --git a/examples/api_usage/http/simple_working_example.py b/examples/api_usage/http/simple_working_example.py
index 43884d1974..fc2e56001d 100644
--- a/examples/api_usage/http/simple_working_example.py
+++ b/examples/api_usage/http/simple_working_example.py
@@ -46,7 +46,7 @@ sys.path.insert(0, str(Path(__file__).parent.parent.parent.parent / "src"))
from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.models import User
-from local_deep_research.database.auth_db import get_auth_db_session
+from local_deep_research.database.auth_db import auth_db_session
def create_test_user():
@@ -58,11 +58,10 @@ def create_test_user():
try:
# Create user in auth database
- auth_db = get_auth_db_session()
- new_user = User(username=username)
- auth_db.add(new_user)
- auth_db.commit()
- auth_db.close()
+ with auth_db_session() as session:
+ new_user = User(username=username)
+ session.add(new_user)
+ session.commit()
# Create encrypted database for user
db_manager = DatabaseManager()
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index ff3705b09e..f940c899b7 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -587,14 +587,12 @@ class DatabaseManager:
def user_exists(self, username: str) -> bool:
"""Check if a user exists in the auth database."""
- from .auth_db import get_auth_db_session
+ from .auth_db import auth_db_session
from .models.auth import User
- auth_db = get_auth_db_session()
- user = auth_db.query(User).filter_by(username=username).first()
- auth_db.close()
-
- return user is not None
+ with auth_db_session() as session:
+ user = session.query(User).filter_by(username=username).first()
+ return user is not None
def get_memory_usage(self) -> Dict[str, Any]:
"""Get memory usage statistics."""
diff --git a/src/local_deep_research/web/routes/api_routes.py b/src/local_deep_research/web/routes/api_routes.py
index 6234753f55..98b42526f8 100644
--- a/src/local_deep_research/web/routes/api_routes.py
+++ b/src/local_deep_research/web/routes/api_routes.py
@@ -37,27 +37,26 @@ def get_current_config():
try:
with get_user_db_session() as session:
settings_manager = SettingsManager(session)
-
- config = {
- "provider": settings_manager.get_setting(
- "llm.provider", "Not configured"
- ),
- "model": settings_manager.get_setting(
- "llm.model", "Not configured"
- ),
- "search_tool": settings_manager.get_setting(
- "search.tool", "searxng"
- ),
- "iterations": settings_manager.get_setting("search.iterations", 8),
- "questions_per_iteration": settings_manager.get_setting(
- "search.questions_per_iteration", 5
- ),
- "search_strategy": settings_manager.get_setting(
- "search.search_strategy", "focused_iteration"
- ),
- }
-
- session.close()
+ config = {
+ "provider": settings_manager.get_setting(
+ "llm.provider", "Not configured"
+ ),
+ "model": settings_manager.get_setting(
+ "llm.model", "Not configured"
+ ),
+ "search_tool": settings_manager.get_setting(
+ "search.tool", "searxng"
+ ),
+ "iterations": settings_manager.get_setting(
+ "search.iterations", 8
+ ),
+ "questions_per_iteration": settings_manager.get_setting(
+ "search.questions_per_iteration", 5
+ ),
+ "search_strategy": settings_manager.get_setting(
+ "search.search_strategy", "focused_iteration"
+ ),
+ }
return jsonify({"success": True, "config": config})
diff --git a/src/local_deep_research/web_search_engines/search_engine_base.py b/src/local_deep_research/web_search_engines/search_engine_base.py
index 3b7ecaaeba..650b5b86de 100644
--- a/src/local_deep_research/web_search_engines/search_engine_base.py
+++ b/src/local_deep_research/web_search_engines/search_engine_base.py
@@ -654,3 +654,25 @@ Respond with ONLY the JSON array, no other text."""
List of result dictionaries with full content
"""
pass
+
+ def close(self) -> None:
+ """
+ Close any resources held by this search engine.
+
+ Subclasses with HTTP sessions or other resources should override this.
+ The base implementation safely closes any 'session' attribute if present.
+ """
+ if hasattr(self, "session") and self.session is not None:
+ try:
+ self.session.close()
+ except Exception:
+ pass # Safe cleanup - don't raise on close
+
+ def __enter__(self):
+ """Support context manager usage."""
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ """Cleanup on context exit."""
+ self.close()
+ return False