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.

review: hoist asyncio+threading imports to module level + Wave 7 doc (#4048)

LearningCircuit3d0b7bb5f931
diff --git a/docs/developing/resource-cleanup.md b/docs/developing/resource-cleanup.md
index 35f48b4ea8..fe4b4c5460 100644
--- a/docs/developing/resource-cleanup.md
+++ b/docs/developing/resource-cleanup.md
@@ -232,6 +232,55 @@ a corner the previous wave hadn't touched.
per-research `finally` block. Idempotency via `_ldr_closed` sentinels
on the inner httpx clients.
+### Wave 7 — async close inside a running loop (May 2026)
+
+- **#4047** — `_close_base_llm`'s async branch had a documented "skip if
+ a loop is running; loop owner closes" path. **No loop-owner cleanup
+ code existed anywhere in the project**, so when the close was called
+ inside an active asyncio loop the inner `httpx.AsyncClient` (and its
+ `epoll_create` FD) was silently abandoned. Reproduced in production:
+ a v1.6.10 single-host Ollama container reached 1024 FDs with the
+ /proc histogram showing **929 `anon_inode:[eventpoll]` (91%)** — the
+ same FD class as #3816 but in a code path #3855's fix didn't cover.
+ The fix runs the async close in a brief daemon thread that owns its
+ own loop, so `asyncio.run(aclose())` works regardless of the caller's
+ loop state. A bounded 5-second `join` keeps the cleanup from blocking
+ shutdown when the Ollama server is unresponsive; on timeout
+ `_ldr_closed` is left unset so a later call retries, and a WARNING
+ surfaces so the situation is observable instead of silent.
+- **Healthcheck pidfd leak (same PR).** Dockerfile's
+ `HEALTHCHECK CMD python -c "... urllib.request.urlopen(...)"` had no
+ `timeout=` argument; Docker's 10s timeout SIGKILL'd the `sh -c`
+ parent but the python child was reparented to PID 1 and hung
+ forever, each surviving child holding a `pidfd` + TCP socket against
+ the app. Same /proc dump showed **64 `anon_inode:[pidfd]` (6%)** from
+ this. Adding `timeout=8` lets the child return/raise inside Docker's
+ budget so it exits cleanly and gets reaped.
+- **Broader audit during the PR found no other latent eventpoll leaks
+ in steady state.** Non-Ollama providers all share `lru_cache`'d
+ httpx via langchain — safe. All HTTP-client sites are
+ context-managed. `subprocess` usage is `subprocess.run`-only
+ (blocking, no pidfd leak surface). No raw `asyncio.new_event_loop()`
+ outside safe `asyncio.run()` patterns. All 37 `open()` calls inside
+ `with`. The audit did surface three related gaps that are scoped to
+ a follow-up PR rather than the immediate fix:
+ - **OllamaEmbeddings httpx.** Same FD class as ChatOllama, but
+ `_close_base_llm` only knows about `ChatOllama` (LLM). The
+ embeddings provider creates its own per-instance httpx client and
+ has no close path in either langchain or our wrappers. Currently
+ bounded by per-research GC, but a heavy-indexing workload would
+ leak the same way the LLM did. Pending follow-up.
+ - **Unmanaged engines at shutdown.** `auth_db._auth_engine` and
+ `journal_quality._engine` are module-global singletons that escape
+ `shutdown_databases()` — the former has a `dispose_auth_engine()`
+ that is never reached at process exit; the latter has no cleanup
+ hook at all. Both pools are bounded so they don't grow, but
+ graceful exit-time disposal is missing.
+ - **`LibraryRAGService` in three RAG SSE endpoints.** `rag_routes.py`
+ lines 693, 1054, 1827 construct the service **before** the
+ generator and never close it. Compounds with the OllamaEmbeddings
+ leak above when those endpoints index with Ollama embeddings.
+
---
## Intentionally not done (deferred)
diff --git a/src/local_deep_research/utilities/llm_utils.py b/src/local_deep_research/utilities/llm_utils.py
index 5de13f9217..324ed5b9ac 100644
--- a/src/local_deep_research/utilities/llm_utils.py
+++ b/src/local_deep_research/utilities/llm_utils.py
@@ -6,9 +6,12 @@ This module provides utility functions for working with language models
when the user's llm_config.py is missing or incomplete.
"""
-from loguru import logger
+import asyncio
+import threading
from typing import Any, Optional, Dict
+from loguru import logger
+
from ..config.constants import DEFAULT_OLLAMA_URL
from ..config.thread_settings import get_setting_from_snapshot
@@ -117,8 +120,6 @@ def _close_base_llm(llm):
and getattr(async_httpx, "_ldr_closed", None) is not True
and hasattr(async_httpx, "aclose")
):
- import asyncio
-
try:
asyncio.get_running_loop()
except RuntimeError:
@@ -140,7 +141,6 @@ def _close_base_llm(llm):
# off to a brief daemon thread whose own loop is
# independent of ours; cap with a bounded ``join`` so a
# stuck server can't hold up shutdown.
- import threading
def _close_in_thread() -> None:
try: