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(resource-cleanup): expand Wave 7 with full audit ledger (#4054)

LearningCircuit6f18a711d2c0
diff --git a/docs/developing/resource-cleanup.md b/docs/developing/resource-cleanup.md
index fe4b4c5460..940bc64799 100644
--- a/docs/developing/resource-cleanup.md
+++ b/docs/developing/resource-cleanup.md
@@ -256,30 +256,478 @@ a corner the previous wave hadn't touched.
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.
+#### Audit ledger — what the broader sweep checked
+
+The PR included a wide audit (50+ parallel exploration agents across
+seven rounds plus direct `/proc` inspection) to catch any other latent
+FD leak. To save the next contributor from re-running the same checks,
+here is the full ledger:
+
+##### Checked and confirmed clean (no action needed)
+
+- **Non-Ollama LLM providers.** xAI, Google Gemini, OpenRouter, IONOS,
+ LM Studio, llama.cpp HTTP, DeepSeek, OpenAI-compatible endpoint, plus
+ OpenAI and Anthropic themselves. All extend `ChatOpenAI` or
+ `ChatAnthropic`, which use `@lru_cache`'d shared httpx clients.
+ `_close_base_llm`'s short-circuit on these classes is correct by
+ design — closing them would brick every other live LLM in the
+ process.
+- **HTTP session lifecycle.** Six instantiation sites checked
+ (`PricingFetcher` aiohttp, `LDRClient` SafeSession, `BaseDownloader`,
+ `SemanticScholarSearchEngine`, `MCPClient`, `CostCalculator`). All
+ context-managed via `with` or owned by a class with a paired
+ `close()` and `__exit__`.
+- **subprocess / pidfd.** Three call sites, all `subprocess.run()`
+ (blocking). No `subprocess.Popen` paths anywhere in `src/`. No
+ `ProcessPoolExecutor`. No FD leak surface beyond the healthcheck
+ child, already addressed by the Dockerfile `timeout=8` change.
+- **asyncio event loops.** Zero raw `asyncio.new_event_loop()`
+ outside safe `asyncio.run()` patterns. The historical leak in
+ `news_strategy.py` (#3018) is still fixed.
+- **File handles.** All 37 `open()` call sites are inside `with`.
+ Zero bare opens. `tempfile.NamedTemporaryFile` / `TemporaryDirectory`
+ all context-managed.
+- **SocketIO connect/disconnect.** Non-disconnect handlers
+ (`subscribe`, `unsubscribe`, `connect`) do not acquire DB sessions
+ (an early-round agent claim that they did was refuted on re-read).
+ The `__socket_subscriptions` dict is cleaned on disconnect. The
+ PID-1 FD breakdown showed only 3 sockets out of 1024 — socket
+ accumulation is not a contributor.
+
+##### Flagged by audit, then verified NOT a real FD leak
+
+- **OllamaEmbeddings httpx.** LDR imports the **deprecated**
+ `langchain_community.embeddings.OllamaEmbeddings`, which uses
+ `requests.post()` per call — no persistent httpx client, no
+ `_client` / `_async_client` attribute. Direct introspection:
+ `[a for a in dir(e) if 'client' in a.lower()]` returns `[]`. Zero
+ FDs per call. An audit agent confused this class with `ChatOllama`,
+ which is a different class. See the migration note in the next
+ subsection — this changes when langchain forces the move.
+- **`auth_db` and `journal_quality` engines escaping
+ `shutdown_databases()`.** `auth_db` uses
+ `QueuePool(pool_size=10, max_overflow=20)` and `journal_quality`
+ uses `StaticPool` with `immutable=1`. Both are **bounded** and do
+ not grow at runtime. Live `/proc` on the affected container showed
+ only 21 SQLite-related FDs total on PID 1 — well below the ~91-FD
+ ceiling these unmanaged engines could theoretically reach. The
+ kernel reclaims FDs at process exit regardless of `engine.dispose()`,
+ and SQLite WAL files auto-checkpoint on next open. Missing dispose
+ at exit is hygiene, not a leak.
+- **`LibraryRAGService` in three RAG SSE endpoints.**
+ `rag_routes.py:693, 1054, 1827` do construct the service outside
+ the generator and never close it, **but** `LibraryRAGService.close()`
+ only sets references to `None` — it releases no FDs. FAISS uses
+ `pickle.load()` (not mmap); OllamaEmbeddings holds no FDs per the
+ item above; the SentenceTransformer model+tokenizer mmaps are
+ process-wide singletons. What gets delayed is ~50–200 MB of
+ embedding-model RAM until GC. A memory-pressure question, not the
+ eventpoll FD class this Wave addressed.
+- **Residual `pidfd` accumulation via Playwright fallback** —
+ identified in a Round-8 follow-up after the eventpoll fix landed.
+ Live `/proc` on the prerelease container showed ~29 pidfds steady
+ state, growing ~3.6/hour, all targeting `Pid: -1` (children that
+ had exited). Rate was stable during active benchmark execution,
+ ruling out a per-task source. Eight parallel agents converged on
+ the same chain: `_check_subscription` → `quick_summary` →
+ `FullSearchResults.batch_fetch_and_extract` → `AutoHTMLDownloader`
+ fallback to `PlaywrightHTMLDownloader._fetch_with_playwright`. Each
+ `sync_playwright().start()` invokes
+ `asyncio.create_subprocess_exec()` for the Node.js driver (opens a
+ pidfd via Linux's `PidfdChildWatcher`); the driver then fails
+ because Chromium is not installed in the production `ldr` Dockerfile
+ stage (only `ldr-test` runs `playwright install --with-deps
+ chromium`), and the asyncio child watcher does not promptly close
+ the pidfd on the failed-child exit. CPython 3.14 was confirmed to
+ not use pidfd in `subprocess.py` at all (`subprocess.run`/`Popen`
+ use `waitpid(WNOHANG)` polling), so subprocess-based hypotheses
+ were ruled out. **Fixed by PR #3971** (default
+ `web.enable_javascript_rendering=false`): the fallback short-circuits
+ before any subprocess is spawned, so no pidfd is opened. The PR was
+ motivated by issue #3826 (confusing tracebacks); the FD-leak
+ finding is the second motivation, surfaced here.
+
+##### Minor findings (not steady-state leaks; worth knowing)
+
+- **Daemon threads without explicit shutdown.** `search_cache.py`
+ cache-cleanup thread, `journal_reputation_filter.py` background
+ fetcher, `log_utils.py` queue processor, `parallel_search_engine.py`
+ global executor. All daemonized — reaped by the OS at process exit.
+ Not steady-state leaks; no per-request growth.
+- **Abandoned-research thread on socket disconnect.** If a client
+ closes the tab mid-research, the socket subscription is removed but
+ the research thread keeps running until completion;
+ `_active_research[research_id]` is not cleared on disconnect. Not an
+ FD leak; potentially compute/memory waste if the user wanted the
+ research to stop. Out of scope for the FD-leak story.
+
+#### Future-proofing note — `langchain_ollama.OllamaEmbeddings` migration
+
+`langchain_community.embeddings.OllamaEmbeddings` is **deprecated**
+("will be removed in langchain 1.0.0", per the import warning). Its
+replacement, `langchain_ollama.OllamaEmbeddings`, **does** carry
+`_client` and `_async_client` attributes — same shape as
+`ChatOllama`. Verified by direct introspection:
+
+```
+langchain_ollama.OllamaEmbeddings client attrs:
+ ['_set_clients', 'async_client_kwargs', 'client_kwargs',
+ 'sync_client_kwargs']
+Has _client? True
+Has _async_client? True
+```
+
+So: today's LDR is FD-safe on the embeddings side **by accident**
+(using the deprecated class). The day the deprecated class is removed
+upstream and LDR migrates, the in-running-loop eventpoll FD leak
+class will reappear for embeddings unless `_close_base_llm` is
+generalized to introspect either a chat model or an embeddings
+instance with the same `_client` / `_async_client` shape. The
+extension is a small type-check broadening, not a redesign.
+
+---
+
+## Debugging FD leaks — playbook for the next one
+
+When the next FD leak shows up (and there will be one, eventually), this
+section is the shortcut. It captures the actual diagnostic flow that
+worked across Waves 6 and 7 so a future contributor doesn't have to
+re-derive it from the symptom.
+
+### 0. Symptoms that mean "investigate this as an FD leak"
+
+- Tracebacks like `OSError: [Errno 24] Too many open files`, typically
+ from `selectors.DefaultSelector()` in werkzeug or `send_from_directory`
+ in Flask. These are usually the *first* visible failure.
+- Browser-side MIME-type errors on static assets (`text/html` instead of
+ `text/css` / `application/javascript`). These are downstream of FD
+ exhaustion — Flask can't open the static file, returns an HTML 500,
+ and the browser refuses to apply it because of
+ `X-Content-Type-Options: nosniff`.
+- `High FD count (N) — approaching system limit` warnings from
+ `web/auth/connection_cleanup.py` (fires at FD > 800 every 5-minute
+ cleanup tick).
+- Container health turns `unhealthy` because the healthcheck `urlopen`
+ hangs on a process that no longer has FDs to accept connections.
+
+### 1. Capture diagnostic state BEFORE restarting
+
+The single most important rule: **the snapshot does not survive a
+container restart**. Every minute spent on the live broken container is
+worth an hour of after-the-fact agent guessing. Save the diagnostic
+output to a host-side file first.
+
+#### One-shot host-side snapshot (works even when the container is
+FD-starved enough that `docker exec` can't fork)
+
+```bash
+# Run on the Docker host. No docker exec required.
+P=$(docker inspect -f '{{.State.Pid}}' <container-name>)
+sudo bash -c "
+ echo '=== Total FDs ==='
+ ls /proc/$P/fd | wc -l
+ echo '=== FD-type histogram (digits collapsed) ==='
+ ls -l /proc/$P/fd | awk '{print \$NF}' \
+ | sed -E 's/\[[0-9]+\]/[N]/g; s/[0-9]{4,}/NUM/g' \
+ | sort | uniq -c | sort -rn | head -30
+ echo '=== Counts by category ==='
+ printf 'socket: %s\n' \$(find /proc/$P/fd -lname 'socket:*' | wc -l)
+ printf 'pipe: %s\n' \$(find /proc/$P/fd -lname 'pipe:*' | wc -l)
+ printf 'eventpoll: %s\n' \$(find /proc/$P/fd -lname '*eventpoll*' | wc -l)
+ printf 'pidfd: %s\n' \$(find /proc/$P/fd -lname '*pidfd*' | wc -l)
+ printf 'WAL files: %s\n' \$(find /proc/$P/fd -lname '*-wal' | wc -l)
+ printf 'SHM files: %s\n' \$(find /proc/$P/fd -lname '*-shm' | wc -l)
+ printf '.db files: %s\n' \$(find /proc/$P/fd -lname '*.db' | wc -l)
+" | tee /tmp/ldr-fd-snapshot.txt
+```
+
+Why host-side: reading the container's PID 1 FDs from inside the
+container requires the same UID that started PID 1. The Dockerfile
+entrypoint runs as root then `setpriv`s to `ldruser`, so the
+`docker exec` shell (ldruser) cannot `readlink` PID 1's FDs even though
+it can count them. Host root via `sudo` sidesteps the UID check.
+
+#### Inside-container alternative (if the host is locked down)
+
+```bash
+docker exec --user 0 <container-name> sh -c '...same body...'
+```
+
+`--user 0` runs the exec'd shell as root inside the container,
+sidestepping the same UID restriction.
+
+### 2. The lookup table — FD type → likely source
+
+| Dominant FD type | Likely source | Diagnostic deep-dive |
+|-----------------------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------|
+| `anon_inode:[eventpoll]` | `asyncio` event loop or `httpx.AsyncClient` selector. Each leaked async client = +1. | Grep `asyncio.create_subprocess`, `httpx.AsyncClient`, `_async_client`, `ainvoke`. See Wave 6, Wave 7. |
+| `anon_inode:[pidfd]` | `asyncio.create_subprocess_*` or `multiprocessing.Process` (uses `pidfd_open` on Linux). | Read `/proc/PID/fdinfo/N` for each pidfd; the `Pid:` line shows the target (`-1` = child already exited). |
+| `socket:*` (lots) | HTTP keep-alive, SSE streams, SocketIO connections. | Cross-reference with `/proc/PID/net/tcp` states; check Round 7 R7A8 patterns. |
+| `pipe:*` (lots) | `subprocess.run`/`Popen` with `stdout=PIPE`, multiprocessing IPC, loguru queue. | Check `subprocess.run` sites and APScheduler executor type. |
+| `REG` `*-wal` / `*-shm` | SQLCipher in WAL mode. Each pooled connection holds ~3 FDs. | See ADR-0004. If growing without bound, the periodic `engine.dispose()` is silently failing. |
+| `REG /data/*.db` (lots) | Plain SQLite connections from an engine without bounded pool. | Audit `create_engine` sites (R7A6 caught two unmanaged ones). |
+| `REG /home/...mmap...` | Memory-mapped model weights or FAISS indexes — usually process-wide singletons (not leaks). | Check whether the count grows per request. If yes → real leak. |
+
+### 3. Pinpointing the source for a specific FD type
+
+#### Eventpoll
+
+`anon_inode:[eventpoll]` always comes from `EpollSelector` — created
+by every asyncio loop and every `httpx.AsyncClient`. Grep:
+
+```
+grep -rn 'asyncio.create_subprocess\|httpx.AsyncClient\|_async_client' src/
+```
+
+Then check whether each site explicitly closes the client. The Wave 7
+fix to `_close_base_llm` is the reference pattern for "close async
+httpx even when called inside a running loop."
+
+#### Pidfd
+
+Pidfds expose their target PID via fdinfo:
+
+```bash
+# Run inside the container (or via docker exec --user 0):
+for fd in $(ls /proc/1/fd 2>/dev/null); do
+ link=$(readlink /proc/1/fd/$fd 2>/dev/null)
+ case "$link" in
+ *pidfd*)
+ tpid=$(awk '/^Pid:/ {print $2}' /proc/1/fdinfo/$fd 2>/dev/null)
+ if [ "$tpid" -gt 0 ] 2>/dev/null; then
+ cmd=$(tr '\0' ' ' < /proc/$tpid/cmdline 2>/dev/null | cut -c1-80)
+ echo "fd=$fd alive pid=$tpid : $cmd"
+ else
+ echo "fd=$fd ORPHAN (child exited; pidfd not closed)"
+ fi
+ ;;
+ esac
+done
+```
+
+A high "ORPHAN" count = something called `asyncio.create_subprocess_*`
+or `multiprocessing.Process`, the child exited, but the pidfd in the
+parent was never closed. Common in Round-8: Playwright's Node.js
+driver subprocess failing because Chromium isn't installed in the
+production image.
+
+**Note:** CPython 3.14's `subprocess.py` does not use pidfd at all
+(`waitpid(WNOHANG)` polling instead). So pidfds in a 3.14 process
+necessarily come from asyncio or multiprocessing, not from
+`subprocess.run` / `Popen`.
+
+#### Syscall-level pinpointing with bpftrace (mysterious cases)
+
+When the source isn't obvious from the FD type, `bpftrace` can record
+the Python stack of every relevant syscall on the live process. This
+would have caught the Playwright leak in seconds instead of two rounds
+of agent exploration. Requires kernel headers and `bpftrace` installed
+on the host (NOT the container — bpftrace runs in host kernel space
+and can target a host PID by number):
+
+```bash
+# Find host-side PID of container's PID 1
+P=$(docker inspect -f '{{.State.Pid}}' <container>)
+
+# Trace every pidfd_open syscall, grouped by user-stack:
+sudo bpftrace -e "tracepoint:syscalls:sys_enter_pidfd_open
+ /pid == $P/ { @[ustack(perf)] = count(); }"
+
+# Same idea for epoll_create / epoll_create1 (eventpoll FDs):
+sudo bpftrace -e "tracepoint:syscalls:sys_enter_epoll_create1
+ /pid == $P/ { @[ustack(perf)] = count(); }"
+```
+
+Let it run for a minute, then Ctrl-C; you get a histogram of every
+unique stack that triggered the syscall, ranked by frequency. The hot
+stacks are your culprits. Works for any syscall — useful future
+candidates: `socket`, `inotify_init1`, `timerfd_create`,
+`memfd_create`.
+
+#### WAL/SHM
+
+`engine.dispose()` is expected to release these. If the count climbs
+across the periodic 30-minute dispose cycles, the dispose is silently
+failing. The observability commit (f86c3f7af) elevates dispose
+failures to WARNING — check the logs for `Error disposing engine for
+<user>`.
+
+### 4. Existing instrumentation already in the codebase
+
+- **`_count_open_fds()`** at
+ `src/local_deep_research/web/auth/connection_cleanup.py:50` —
+ fast `/proc/self/fd`-based counter with macOS fallback. Reusable.
+- **`Resource monitor: open_fds=…`** debug log line at
+ `connection_cleanup.py:184`, fires every 5-minute cleanup tick.
+- **`High FD count (N)` WARNING** at `connection_cleanup.py:190`
+ when FDs exceed 800. The single most useful production signal.
+- **`fd_monitor.py`** (PR #3036) — cross-platform helper used by
+ diagnostic endpoints.
+- **`tests/manual_smoke/test_fd_smoke.py`** (PR #3930, the
+ `test/manual-fd-smoke-suite` branch) — opt-in pytest harness that
+ runs the close cycle N times and asserts the FD count stays flat.
+ Gated by `RUN_MANUAL_SMOKE=1`; not part of the default CI run
+ because it needs a live Ollama and produces noise. Extend this
+ suite when you ship a leak fix.
+- **In-CI FD-growth canaries** in
+ `tests/utilities/test_close_base_llm.py`. These run on every PR:
+ - `TestCloseBaseLLMRealHttpxAsync::test_no_fd_growth_across_repeated_close_cycles`
+ — guards the eventpoll FD class against Wave-6-shaped regressions.
+ - `TestCloseBaseLLMRealHttpxAsync::test_no_fd_growth_when_closed_inside_running_loop`
+ — guards the Wave-7-shaped in-running-loop skip regression.
+ - `TestAsyncioSubprocessFDBaseline::test_no_fd_growth_across_asyncio_subprocess_cycles`
+ — guards the pidfd FD class against the child-watcher leak shape.
+ - `TestAsyncioSubprocessFDBaseline::test_no_fd_growth_when_subprocess_fails_to_exec`
+ — pins the *exact* Wave-7-pidfd shape (failed exec, child watcher
+ must still clean up). Catches platform-level regressions in
+ Python's asyncio child watcher.
+
+ All four use `_open_fd_count()` (also in that file) which reads
+ `/proc/self/fd` on Linux with an `RLIMIT_NOFILE` fallback on macOS.
+ Slack is +2 FDs across 5–10 iterations. A real per-cycle leak would
+ blow past that.
+
+### 4a. Development-time detection (catch leaks at test time)
+
+Production /proc inspection catches leaks **after** they ship. The
+cheapest catch is to make Python itself complain at test time. Three
+Python features cooperate to surface unclosed resources during a
+normal test run — none of them were on by default during Waves 6 and
+7, which is part of why those leaks made it to production.
+
+**`PYTHONASYNCIODEBUG=1` plus `-W default::ResourceWarning`.** When
+asyncio debug mode is on, unclosed transports/coroutines emit a
+`ResourceWarning` at GC time. The `-W` filter makes Python actually
+display them. Together they would have caught the Wave 7 in-running-loop
+skip: every leaked `httpx.AsyncClient` produces a visible warning the
+first time the GC sweeps after the test fixture exits. From
+[the asyncio dev docs](https://docs.python.org/3/library/asyncio-dev.html):
+
+> When a transport is no longer needed, call its `close()` method to
+> release resources. ... If a transport or an event loop is not closed
+> explicitly, a `ResourceWarning` warning will be emitted in its
+> destructor.
+
+To enable in `pyproject.toml` `[tool.pytest.ini_options]`:
+
+```toml
+filterwarnings = [
+ "default::ResourceWarning",
+]
+env = [
+ "PYTHONASYNCIODEBUG=1",
+]
+```
+
+Or in CI for a one-off check:
+
+```bash
+PYTHONASYNCIODEBUG=1 python -W default::ResourceWarning -m pytest tests/
+```
+
+For a CI gate that **fails** on any leak (more aggressive — use only
+on a targeted subset of tests, not the whole suite, because
+third-party libraries also emit ResourceWarning):
+
+```toml
+filterwarnings = [
+ "error::ResourceWarning",
+]
+```
+
+**`python -X dev`.** Enables Python's dev mode, which turns on a
+bundle of safety checks including ResourceWarning display, asyncio
+debug mode, and warnings as default. Cheap one-flag alternative for
+local development; not recommended in production (overhead).
+
+```bash
+python -X dev -m pytest tests/
+```
+
+**`psutil` for portable FD counting in tests.** Our in-codebase
+`_count_open_fds` uses `/proc/self/fd` (Linux-fast path, macOS
+fallback). `psutil` is the cross-platform alternative many other
+projects use:
+
+- `psutil.Process().num_fds()` — Linux/BSD only; same number as our
+ helper.
+- `psutil.Process().open_files()` — list of named files; gives the
+ paths for `REG`-type FDs (e.g., `/data/*.db-wal`).
+- `psutil.Process().connections(kind='all')` — sockets visible to the
+ process, with state and remote address.
+
+These are useful in unit tests when you want to assert "no new file
+of pattern X is open after the close path runs," and they work on the
+macOS dev environments without `/proc`.
+
+**For tracking which Python object holds a leaked FD: `tracemalloc`
++ `objgraph`.** Not FD tools per se, but when a leak is reproducible,
+take a `tracemalloc` snapshot before and after the suspect operation
+and diff — the new allocation is usually the wrapper holding the FD.
+`objgraph.show_backrefs([leaked_obj])` then renders the reference
+chain keeping it alive. Both are pure-Python and zero-dependency.
+
+### 5. Why we don't have an automated FD-growth test in CI
+
+Several reasons, weighed during Wave 6 and Wave 7:
+
+- **Per-request FD growth is hard to assert.** Many legitimate
+ request paths transiently open and close FDs; a noisy delta is the
+ norm. Distinguishing "leak" from "in-flight" requires a stable
+ quiescent state, which a CI test doesn't naturally provide.
+- **The CI environment spawns its own subprocesses.** pytest,
+ coverage, gunicorn workers (for some test variants), gh-runner
+ cleanups — all add their own FDs that pollute the count.
+- **PID-namespace differences between CI and prod.** Counts you
+ observe in a CI container's /proc are not directly comparable to a
+ production container's /proc; the subprocess sources differ.
+- **The actual leaks have been "slow drip" patterns** that need
+ hours of uptime to surface. Wave 6's eventpoll leak took multiple
+ hours of `ainvoke` calls to reach the 1024 cap. CI can't run for
+ hours per PR.
+
+What works instead:
+1. **Per-leak unit-level regression tests.** Each fix in Waves 1-7
+ landed with a targeted test that exercises the specific close path
+ (e.g. `tests/utilities/test_close_base_llm.py::test_no_fd_growth_when_closed_inside_running_loop`).
+ These are fast, deterministic, and run on every PR.
+2. **Opt-in manual smoke suite** (`RUN_MANUAL_SMOKE=1`) for the
+ end-to-end "run-the-cycle-N-times-and-count" pattern, used during
+ investigation but not on every CI run.
+3. **Production /proc inspection** when a leak is suspected — the
+ playbook above. Faster than CI for the long-drip patterns.
+
+If you want to add a long-run CI job, the right shape would be a
+**nightly** workflow (not per-PR) that:
+
+1. Builds the production Docker image.
+2. Starts it with a synthetic user account and ~5 news subscriptions.
+3. Lets it idle for 20-30 minutes.
+4. Runs the host-side snapshot script above.
+5. Asserts `total FDs < N` and `eventpoll < M` and `pidfd < K`,
+ where the thresholds are tuned for the steady-state ceilings the
+ codebase intentionally permits (auth_db pool, etc.).
+
+That would have caught Waves 6, 7 in a single nightly cycle instead
+of through a user crash report. The reason it doesn't exist yet is
+cost (a half-hour idle job per night per platform) and the lack of a
+clear baseline; the Round-8 finding is the moment to consider adding
+one if you want to invest the maintenance time.
+
+### 6. Lookup: which Wave fixed which leak class
+
+| FD class | Wave / PR | Root mechanism |
+|------------------------|------------------------|---------------------------------------------------------------------------|
+| `eventpoll` | Wave 6 #3855 + Wave 7 #4047 | ChatOllama `_async_client` not closed (Wave 6) → also not closed when called inside a running loop (Wave 7). |
+| `pidfd` from healthcheck | Wave 7 #4047 | `urlopen` no `timeout=` → child hangs → reparented to PID 1 with pidfd held. |
+| `pidfd` from Playwright fallback | Round 8 / #3971 | Production image lacks Chromium binary; Playwright invocation opens pidfd then fails. |
+| WAL/SHM accumulation | Wave 5 / ADR-0004 | SQLCipher+WAL leaks handles on out-of-order close; periodic `engine.dispose()` resets the pool. |
+| Per-thread engine FDs | Wave 5 #3441 | Removed per-thread `NullPool` engines entirely; shared per-user `QueuePool`. |
+| HTTP session sockets | Wave 1 / Wave 3 | `SafeSession` / `BaseDownloader` close-in-`finally` discipline. |
+| `asyncio.new_event_loop` | Wave 4 #3018 | Replaced manual loop creation with `asyncio.run()` in `news_strategy.py`. |
+
+Use this table to skip the rediscovery step the next time a specific
+FD type dominates a snapshot.
---
diff --git a/tests/utilities/test_close_base_llm.py b/tests/utilities/test_close_base_llm.py
index 1d4188a9ee..33c9169ea5 100644
--- a/tests/utilities/test_close_base_llm.py
+++ b/tests/utilities/test_close_base_llm.py
@@ -442,6 +442,113 @@ class TestCloseBaseLLMRealHttpxAsync:
)
+class TestAsyncioSubprocessFDBaseline:
+ """Canary for the *other* FD class that production has leaked from:
+ ``anon_inode:[pidfd]`` opened by ``asyncio.create_subprocess_exec``.
+
+ Background (Wave 7 / Round 8): production observed ~3.6 pidfds/hour
+ in steady state, all targeting ``Pid: -1`` (exited children).
+ Traced to Playwright's Node.js driver being spawned via
+ ``asyncio.create_subprocess_exec`` and failing because Chromium
+ isn't installed in the production image — but the pidfd opened by
+ the child watcher wasn't promptly closed on the failed-child exit.
+ Fixed by PR #3971 (disable JS rendering by default) which
+ short-circuits before any subprocess is spawned.
+
+ These tests don't exercise LDR's own code directly — LDR itself
+ doesn't call ``asyncio.create_subprocess_*`` (R8C1 audit confirmed).
+ Instead they pin the *platform baseline*: on this Python version,
+ repeated ``asyncio.create_subprocess_exec`` cycles of a fast-exiting
+ child must not leak FDs. If a future Python upgrade, child-watcher
+ change, or dependency hook breaks the close semantics, CI fails
+ here on the next PR — which is the canary signal we want.
+
+ Skip on non-Linux: ``/bin/true`` isn't universally available on
+ Windows; pidfd_open is Linux-only. macOS has ``/bin/true`` but uses
+ a different child watcher (no pidfd), so the test still passes there
+ by virtue of nothing to leak. Restricting to Linux keeps the signal
+ sharp.
+ """
+
+ @pytest.mark.skipif(
+ sys.platform != "linux",
+ reason="pidfd_open is Linux-only; asyncio child watcher behavior differs elsewhere",
+ )
+ def test_no_fd_growth_across_asyncio_subprocess_cycles(self):
+ """Repeated ``asyncio.create_subprocess_exec`` of ``/bin/true``
+ must not leak FDs. A real regression of this class would push
+ the delta well past the +2 slack (this is the same slack we
+ use for the eventpoll tests above; a 1-FD-per-iter leak across
+ 10 iterations would land at delta=10).
+ """
+
+ async def _spawn_and_wait():
+ proc = await asyncio.create_subprocess_exec(
+ "/bin/true",
+ stdout=asyncio.subprocess.DEVNULL,
+ stderr=asyncio.subprocess.DEVNULL,
+ )
+ await proc.wait()
+
+ gc.collect()
+ before = _open_fd_count()
+
+ for _ in range(10):
+ asyncio.run(_spawn_and_wait())
+ gc.collect()
+
+ gc.collect()
+ after = _open_fd_count()
+
+ assert after - before <= 2, (
+ f"FD count climbed across asyncio.create_subprocess_exec "
+ f"cycles — likely a pidfd or eventpoll leak in the child "
+ f"watcher: before={before}, after={after}"
+ )
+
+ @pytest.mark.skipif(
+ sys.platform != "linux",
+ reason="pidfd_open is Linux-only",
+ )
+ def test_no_fd_growth_when_subprocess_fails_to_exec(self):
+ """The Wave-7 production shape was a child that exec'd a binary
+ that **didn't exist** (Chromium missing). The pidfd was opened
+ before exec; if the exec failed, the child watcher still has to
+ close the pidfd cleanly. This pins that path.
+ """
+
+ async def _spawn_missing_binary():
+ # An intentionally-not-installed path. The kernel returns
+ # ENOENT on the exec; the child watcher must still clean up.
+ try:
+ proc = await asyncio.create_subprocess_exec(
+ "/nonexistent/binary/for/ldr/fd/test",
+ stdout=asyncio.subprocess.DEVNULL,
+ stderr=asyncio.subprocess.DEVNULL,
+ )
+ await proc.wait()
+ except (FileNotFoundError, OSError):
+ # Expected: exec failed.
+ pass
+
+ gc.collect()
+ before = _open_fd_count()
+
+ for _ in range(10):
+ asyncio.run(_spawn_missing_binary())
+ gc.collect()
+
+ gc.collect()
+ after = _open_fd_count()
+
+ assert after - before <= 2, (
+ f"FD count climbed across failed-exec asyncio.create_subprocess "
+ f"cycles — exact Wave-7 shape (Playwright Node.js driver "
+ f"failing because Chromium isn't installed). "
+ f"before={before}, after={after}"
+ )
+
+
class TestCloseBaseLLMRealLangchain:
"""End-to-end regression coverage against the real langchain LLM classes
users actually instantiate. Guards against: