~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test(llm_utils): pin daemon-thread contract for in-loop async close (#4078)
LearningCircuitba0912056c5b
diff --git a/tests/utilities/test_close_base_llm.py b/tests/utilities/test_close_base_llm.pyindex 33c9169ea5..7e56824abf 100644--- a/tests/utilities/test_close_base_llm.py+++ b/tests/utilities/test_close_base_llm.py@@ -664,3 +664,96 @@ class TestCloseBaseLLMRealLangchain:# introspection closes both inner httpx clients.assert sync_httpx.is_closed is Trueassert async_httpx.is_closed is True+++class TestInLoopCleanupThreadContract:+ """The in-running-loop branch of ``_close_base_llm`` hands the+ ``aclose()`` off to a daemon thread (see the+ ``else: # A loop is running in this thread`` block that spawns+ a ``ldr-async-llm-close`` thread). Two properties of that thread+ are load-bearing but not pinned by the other tests in this file:++ 1. ``daemon=True`` — without this, a cleanup thread blocked on a+ stuck server would prevent Python interpreter shutdown.+ 2. The thread is given a recognizable name so operators chasing FD+ leaks can correlate ``threading.enumerate()`` / ``top`` output+ against the leak source.++ Plus the third gap: when the cleanup thread completes BUT its inner+ ``asyncio.run(aclose())`` raised, the main thread still sets+ ``_ldr_closed = True`` in the ``else`` branch reached when+ ``t.is_alive()`` is False. The pre-existing+ ``test_swallows_async_close_exception`` covers this for the no-loop+ branch; this class covers the same invariant for the in-loop branch.+ """++ def test_cleanup_thread_is_daemon_so_shutdown_is_not_blocked(self):+ """Capture the thread object created by the running-loop branch+ and assert ``daemon`` is True. A non-daemon cleanup thread+ blocked on a slow server would hang interpreter shutdown until+ the OS kills the process — exactly the failure mode the+ docstring of ``_close_base_llm`` warns about when motivating+ the brief daemon thread.+ """+ import threading+ from unittest.mock import patch++ async def fake_aclose():+ return None++ llm = _make_fake_chat_ollama(async_aclose=fake_aclose)++ captured = {}+ original_thread = threading.Thread++ class _CapturingThread(original_thread):+ def __init__(self, *args, **kwargs):+ captured["daemon"] = kwargs.get("daemon")+ captured["name"] = kwargs.get("name")+ super().__init__(*args, **kwargs)++ async def driver():+ with patch("threading.Thread", _CapturingThread):+ _close_base_llm(llm)++ asyncio.run(driver())++ assert captured["daemon"] is True, (+ "cleanup thread must be a daemon — otherwise a stuck "+ "aclose() would block Python interpreter shutdown"+ )+ # Name is operator-facing for FD-leak triage; pin a stable+ # prefix rather than the exact string so future tweaks can+ # extend without breaking the test.+ assert captured["name"] is not None+ assert "ldr" in captured["name"].lower()++ def test_in_loop_close_marks_closed_even_when_inner_aclose_raises(+ self,+ ):+ """The cleanup thread's ``_close_in_thread`` runs+ ``asyncio.run(aclose())`` inside a ``try/except Exception`` — if+ ``aclose`` raises, the thread logs a warning and exits cleanly.+ The main thread sees ``t.is_alive() == False`` and falls into+ the ``else`` branch that sets ``_ldr_closed = True``. Pin that+ invariant: the sentinel is the signal to never retry a+ known-broken close, regardless of the branch (no-loop or+ in-loop) that performed it.+ """++ async def boom_aclose():+ raise RuntimeError("simulated upstream failure")++ llm = _make_fake_chat_ollama(async_aclose=boom_aclose)++ async def driver():+ _close_base_llm(llm)++ asyncio.run(driver())++ async_httpx = llm._async_client._client+ assert async_httpx._ldr_closed is True, (+ "Even when aclose raises inside the cleanup thread, the "+ "main thread must mark the client closed to prevent "+ "indefinite retries on a known-broken close"+ )