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: prevent memory exhaustion in PDF uploads (issue #1176)

LearningCircuitb83de7ec3cbb
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index 13c25ef318..f5663482a4 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -6,6 +6,7 @@ from importlib import resources as importlib_resources
from flask import (
Flask,
+ Request,
jsonify,
make_response,
request,
@@ -40,6 +41,21 @@ def _is_private_ip(ip_str: str) -> bool:
return False
+class DiskSpoolingRequest(Request):
+ """Custom Request class that spools large file uploads to disk.
+
+ This prevents memory exhaustion from large multipart uploads by writing
+ files larger than max_form_memory_size to temporary files on disk instead
+ of keeping them in memory.
+
+ Security fix for issue #1176: With 200 files × 50MB limit, the default
+ behavior could consume 10GB+ of memory per request.
+ """
+
+ # Files larger than 5MB are spooled to disk instead of memory
+ max_form_memory_size = 5 * 1024 * 1024 # 5MB threshold
+
+
def create_app():
"""
Create and configure the Flask application.
@@ -82,6 +98,10 @@ def create_app():
# Store static dir for custom handling
app.config["STATIC_DIR"] = str(Path("static").resolve())
+ # Use custom Request class that spools large uploads to disk
+ # This prevents memory exhaustion from large file uploads (issue #1176)
+ app.request_class = DiskSpoolingRequest
+
# Add proxy support for deployments behind load balancers/reverse proxies
# This ensures X-Forwarded-For and X-Forwarded-Proto headers are properly handled
# Important for rate limiting and security (gets real client IP, not proxy IP)
diff --git a/src/local_deep_research/web/routes/research_routes.py b/src/local_deep_research/web/routes/research_routes.py
index 85c07ef9d8..0c5548a95f 100644
--- a/src/local_deep_research/web/routes/research_routes.py
+++ b/src/local_deep_research/web/routes/research_routes.py
@@ -1575,13 +1575,17 @@ def upload_pdf():
processed_files = 0
errors = []
+ # Import gc for explicit memory cleanup after each file (issue #1176)
+ import gc
+
for file in files:
if not file or not file.filename:
errors.append("Unnamed file: Skipped")
continue
+ pdf_content = None
try:
- # Read file content
+ # Read file content (with disk spooling, large files are read from temp file)
pdf_content = file.read()
# Comprehensive validation
@@ -1593,6 +1597,10 @@ def upload_pdf():
if not is_valid:
errors.append(f"{file.filename}: {error_msg}")
+ # Release memory immediately on validation failure
+ del pdf_content
+ pdf_content = None
+ gc.collect()
continue
# Extract text and metadata in single pass (performance fix)
@@ -1600,6 +1608,10 @@ def upload_pdf():
pdf_content, file.filename
)
+ # Release PDF content memory immediately after extraction
+ del pdf_content
+ pdf_content = None
+
if result["success"]:
extracted_texts.append(
{
@@ -1616,6 +1628,17 @@ def upload_pdf():
except Exception:
logger.exception(f"Error processing {file.filename}")
errors.append(f"{file.filename}: Error processing file")
+ finally:
+ # Ensure memory is released after each file
+ if pdf_content is not None:
+ del pdf_content
+ # Close the file stream to release resources
+ try:
+ file.close()
+ except Exception:
+ pass
+ # Force garbage collection after each file to keep memory bounded
+ gc.collect()
# Prepare response
response_data = {