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.

docs: thread lifecycle, FD budget, and resource exhaustion (#2605)

LearningCircuit0b23d58e85db
diff --git a/docs/architecture.md b/docs/architecture.md
index d8f3f7516e..2318a983a5 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -461,6 +461,79 @@ flowchart TB
| **Progress Streaming** | SocketIO for real-time UI updates |
| **Cross-Engine Filtering** | LLM-powered relevance scoring and deduplication |
+#### Thread & Resource Lifecycle
+
+Unlike typical web apps that share a single database connection pool, this application maintains **separate database engines per user** because each user has their own encrypted SQLite file with a unique SQLCipher key ([SQLCipher](https://www.zetetic.net/sqlcipher/) is an encrypted extension of SQLite). This creates a threading challenge: every background thread needs its own engine for the user it serves.
+
+Two SQLAlchemy pooling strategies coexist:
+
+- **QueuePool** — one per user, shared across Flask request-handler threads (`pool_size=10`, `max_overflow=30`). Maintains a fixed-size pool of reusable connections that persist between requests.
+- **NullPool** — per-thread engines for background work (research workers, scheduler jobs). Creates a new connection per checkout and closes it on return (no pooling). The SQLAlchemy `Session` using this engine is held for the background thread's lifetime.
+
+##### Resource Cleanup Layers
+
+Cleanup is defense-in-depth with multiple layers:
+
+| Layer | Trigger | What it cleans |
+|-------|---------|----------------|
+| `@thread_cleanup` decorator | Thread function exit (normal or exception) | DB session, thread engine, settings context, search context |
+| `finally` blocks | Per-research in `run_research_process()` | Search engine HTTP sessions, strategy thread pool executors |
+| `teardown_appcontext` | After each HTTP request | QueuePool session, thread-local session, triggers dead-thread engine sweep (rate-limited to 60s) and credential sweep (every request) |
+| Dead-thread engine sweep | Every ~60s (rate-limited) | Disposes NullPool engines for threads no longer in `threading.enumerate()` |
+| Logout cascade | User logout | Scheduler unregister (removes password) → DB close (`engine.dispose()`) → session destroy |
+| Stale session cleanup | `before_request` (~1% of requests, sampled) | Clears Flask sessions for users whose DB connection is gone |
+
+##### Research Thread Lifecycle
+
+```mermaid
+flowchart TD
+ A["POST /api/start_research"] --> B{"Slots available?"}
+ B -- "Yes (direct)" --> C["start_research_process()"]
+ B -- "No (queued)" --> D["Database queue"]
+ D --> E["_process_queue_loop<br/>(daemon thread, 10s poll)"]
+ E --> C
+
+ C --> F["Research thread<br/>(daemon, semaphore-gated)"]
+ F --> G["@thread_cleanup wraps<br/>run_research_process()"]
+
+ G --> H["Strategy.find_relevant_information()"]
+ H --> I["ThreadPoolExecutor sub-tasks<br/>(within strategies only)"]
+ I --> J["Worker @thread_cleanup:<br/>close worker DB session"]
+
+ J --> K["finally: search_engine.close()<br/>system.close() → strategy.close()"]
+ K --> L["Main @thread_cleanup:<br/>close DB session, dispose engine"]
+
+ subgraph sweep ["Defense-in-depth (independent 60s timer)"]
+ M["Dead-thread sweep"] -.-> N["Dispose engines for<br/>dead thread IDs"]
+ end
+
+ style sweep fill:#f0f0f0,stroke:#999
+```
+
+##### FD Budget
+
+Each SQLCipher connection in WAL (Write-Ahead Logging) mode uses **2 file descriptors** (main db + WAL file). All connections to the same database within a process share **1 SHM** (shared-memory) file descriptor. The formula per user database is: `connections × 2 + 1`.
+
+| Component | FD Formula | With defaults |
+|-----------|-----------|---------------|
+| QueuePool (steady state) | `logged_in_users × (pool_size × 2 + 1)` | `users × 21` FDs |
+| QueuePool (peak) | `logged_in_users × ((pool_size + max_overflow) × 2 + 1)` | `users × 81` FDs |
+| NullPool (background) | Transient — held for session lifetime only | Varies |
+
+Default Linux ulimit is 1024 soft (bare metal), which is tight for multi-user deployments. Docker's daemon default (typically 1M+) is adequate. QueuePool engines are created at login and disposed at logout, so only active users consume FDs.
+
+For more on diagnosing FD exhaustion, see [Troubleshooting - Resource Exhaustion](./troubleshooting.md#resource-exhaustion).
+
+##### Key Files
+
+| File | Role |
+|------|------|
+| `database/encrypted_db.py` | `DatabaseManager`, engine lifecycle, dead-thread sweep |
+| `database/thread_local_session.py` | `@thread_cleanup` decorator, thread-local sessions, credential cleanup |
+| `web/app_factory.py` | `teardown_appcontext` handler, cleanup orchestration |
+| `web/services/research_service.py` | Research thread creation, `run_research_process()` |
+| `web/queue/processor_v2.py` | Queue processing, dead-thread sweep trigger |
+
### Areas for Improvement
While the project scores highly overall, these areas have room for growth:
@@ -468,7 +541,7 @@ While the project scores highly overall, these areas have room for growth:
1. **Integration Testing** - More end-to-end tests for full research workflows
2. **API Documentation** - OpenAPI/Swagger spec for REST endpoints
3. **Metrics Dashboard** - Prometheus/Grafana integration for monitoring
-4. **Container Optimization** - Multi-stage Docker builds for smaller images
+4. **Resource Observability** - Expose FD count, thread count, and connection pool stats in /api/v1/health; add periodic sweep logging
5. **Async Architecture** - Migration to async/await for I/O-bound operations
### Key Source Files
@@ -481,6 +554,8 @@ While the project scores highly overall, these areas have room for growth:
| Report Generation | `src/local_deep_research/report_generator.py` | `IntegratedReportGenerator` |
| Web API | `src/local_deep_research/web/routes/` | Flask routes and WebSocket handlers |
| Database | `src/local_deep_research/web/database/` | SQLCipher models and migrations |
+| Encrypted DB | `src/local_deep_research/database/encrypted_db.py` | Per-user SQLCipher engine lifecycle |
+| Thread Sessions | `src/local_deep_research/database/thread_local_session.py` | Thread-safe session management and cleanup |
| Settings | `src/local_deep_research/config/` | Configuration and LLM setup |
### Contributing to Architecture
diff --git a/docs/docker-compose-guide.md b/docs/docker-compose-guide.md
index cba909c810..3f3f3933b8 100644
--- a/docs/docker-compose-guide.md
+++ b/docs/docker-compose-guide.md
@@ -82,6 +82,13 @@ ports:
| `ollama_data` | Downloaded models |
| `searxng_data` | Search engine config |
+### Resource Limits
+
+> **Warning:** Resource limits in the base `docker-compose.yml` are intentionally minimal:
+> - **`nofile` (file descriptors):** Not set. Docker's daemon default (typically 1M+) is appropriate. Setting a lower value can cause `unable to open database file` errors under load.
+> - **`memlock`:** Set to unlimited so SQLCipher's `mlock()` (a system call that prevents memory from being swapped to disk) can lock encryption keys in RAM when `cipher_memory_security` is enabled (opt-in, off by default).
+> - To customize, use a `docker-compose.override.yml` for your deployment.
+
### Local Document Collections
Use the **Collections** system in the Web UI to manage your local documents.
@@ -185,3 +192,4 @@ docker compose down -v
- [Full Configuration Reference](CONFIGURATION.md)
- [SearXNG Setup](SearXNG-Setup.md)
- [Unraid Deployment](deployment/unraid.md)
+- [Architecture - Resource Lifecycle](architecture.md#thread--resource-lifecycle)
diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md
index eb1fbf9e06..940049a127 100644
--- a/docs/troubleshooting.md
+++ b/docs/troubleshooting.md
@@ -12,6 +12,7 @@ This guide covers common issues and their solutions.
- [Docker Issues](#docker-issues)
- [API Issues](#api-issues)
- [Performance Issues](#performance-issues)
+- [Resource Exhaustion](#resource-exhaustion)
---
@@ -566,6 +567,47 @@ sudo lsof -i :5000 # May need sudo for system services
---
+## Resource Exhaustion
+
+### File Descriptor Exhaustion
+
+**Symptoms:**
+- `sqlite3.OperationalError: unable to open database file`
+- `OSError: [Errno 24] Too many open files`
+- Cascading failures across unrelated operations (logging, HTTP requests, WebSocket connections fail simultaneously)
+
+**Why it happens:**
+
+Each SQLCipher WAL-mode connection uses 2 file descriptors (main db + WAL), plus 1 shared SHM fd per database. With per-user encrypted databases, the QueuePool alone uses `users × (pool_size × 2 + 1)` FDs at steady state (21 per user with defaults), up to `users × ((10 + 30) × 2 + 1) = users × 81` under load. Background research threads add transient FDs. The default Linux soft ulimit of 1024 is tight for multi-user deployments.
+
+**Diagnosis:**
+
+```bash
+# Inside Docker (PID 1 is the app due to exec in entrypoint)
+ls /proc/1/fd | wc -l
+cat /proc/1/limits | grep "open files"
+
+# Bare-metal Linux
+ls /proc/$(pgrep -fo ldr-web)/fd | wc -l
+
+# Detailed view — show database-related FDs
+lsof -p <PID> | grep -E '\.db|\.wal|\.shm'
+```
+
+**Solutions:**
+
+1. The app includes automatic dead-thread engine sweeps every ~60 seconds — this normally handles cleanup transparently
+2. **Docker:** The daemon default FD limit (typically 1M+) is appropriate. Do not set a lower `nofile` ulimit — this was intentionally removed from `docker-compose.yml`
+3. **Bare-metal Linux:** The default soft limit of 1024 may be too low. Increase it:
+ ```bash
+ ulimit -n 65536
+ ```
+4. Restart the application to release all file descriptors
+
+For the technical details of the cleanup architecture, see [Architecture - Thread & Resource Lifecycle](./architecture.md#thread--resource-lifecycle).
+
+---
+
## Debug Logging
> **Security note:** Log files are unencrypted and may contain sensitive information such as research queries. Ensure appropriate file permissions.
@@ -643,3 +685,4 @@ If you're still experiencing issues:
- [Architecture Overview](./architecture/OVERVIEW.md) - System architecture
- [FAQ](./faq.md) - Frequently asked questions
- [Search Engines Guide](./search-engines.md) - Detailed engine documentation
+- [Architecture - Thread & Resource Lifecycle](./architecture.md#thread--resource-lifecycle) - Resource cleanup layers and FD budget
diff --git a/src/local_deep_research/web/database/README.md b/src/local_deep_research/web/database/README.md
index e87dbbce6c..f29a7df09e 100644
--- a/src/local_deep_research/web/database/README.md
+++ b/src/local_deep_research/web/database/README.md
@@ -53,6 +53,17 @@ The unified database contains:
* `research` - Research data (from deep_research.db)
* `research_report` - Generated research reports (from deep_research.db)
+## Thread Safety & Connection Model
+
+The application uses per-user encrypted SQLite databases (via [SQLCipher](https://www.zetetic.net/sqlcipher/)) with two [SQLAlchemy pool](https://docs.sqlalchemy.org/en/20/core/pooling.html) strategies:
+
+- **QueuePool** (shared per-user engine in `DatabaseManager.connections`): Serves Flask request handlers. `pool_size=10`, `max_overflow=30`. Connections persist in the pool.
+- **NullPool** (per-thread engines in `DatabaseManager._thread_engines`): Used by background threads (research workers, scheduler jobs). Each checkout creates a fresh SQLCipher connection with the user's encryption key. The SQLAlchemy `Session` using this engine is held for the thread's lifetime.
+
+Cleanup is handled by the `@thread_cleanup` decorator (primary) and periodic dead-thread sweeps (fallback for daemon threads that exit without cleanup).
+
+Note: The core database module is at `src/local_deep_research/database/`, separate from this `web/database/` directory. See [Architecture - Thread & Resource Lifecycle](../../../../docs/architecture.md#thread--resource-lifecycle) for the full cleanup architecture.
+
## Rollback
If you need to roll back to the previous database architecture: