fix(llm): retarget the deadline on the existing client instead of rebuilding it - #520
Open
FacaHD wants to merge 1 commit into
Open
fix(llm): retarget the deadline on the existing client instead of rebuilding it#520FacaHD wants to merge 1 commit into
FacaHD wants to merge 1 commit into
Conversation
FacaHD
force-pushed
the
fix/async-client-cleanup
branch
2 times, most recently
from
September 10, 2026 10:44
9a4258a to
56eab3f
Compare
…uilding it A workflow deadline makes every batch carry a slightly different timeout, and `_model_for_call` expressed that by building a fresh chat model per call. LangChain caches the underlying httpx client in an `lru_cache` keyed on `(base_url, timeout, socket_options)`, so a continuously shrinking deadline is a new cache key every call: a long scan opened one connection pool per LLM request and started evicting the oldest once it passed 128. Eviction is what crashed. Each analyzer node is synchronous and reaches `run_async()` -> `asyncio.run()`, so it owns its event loop. A pool evicted while a later analyzer is running belongs to an earlier, already-closed loop, and the OpenAI SDK's finaliser reacts to collection with `asyncio.get_running_loop().create_task(self.aclose())`. Nothing awaits that task, so it surfaced only as "Task exception was never retrieved: RuntimeError: Event loop is closed", once per eviction. Apply the deadline to the client that already exists instead. Both SDKs read `client.timeout` when building each request, which is the same in-place mutation `_uses_native_connection_retries` already relies on for `max_retries`, and `with_structured_output` wraps that same model instance. Transports without a mutable deadline still fall back to constructing a replacement, so behaviour is unchanged for them. Dynamic timeouts, the explicit retry loop, and the global concurrency limiter are untouched, as is the one-loop-per-analyzer execution model. A 200-call reproduction against the openai 3.11 / httpx2 combination that reported the failure goes from 75 unretrieved `Event loop is closed` tasks and 220 httpx clients to 0 and 20 (one per analyzer). Signed-off-by: FacaHD <49478213+FacaHD@users.noreply.github.com>
FacaHD
force-pushed
the
fix/async-client-cleanup
branch
from
September 10, 2026 10:48
56eab3f to
a352111
Compare
FacaHD
marked this pull request as ready for review
September 10, 2026 11:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #519
Problem
Under a workflow deadline (
SKILLSPECTOR_MAX_WORKFLOW_SECONDS), large scans emitted a stream of:Root cause
Every batch has to be sent with whatever time is left, and
_model_for_call()expressed that by building a fresh chat model per call. LangChain caches the underlyinghttpxclient in anlru_cachekeyed on(base_url, timeout, socket_options), so a deadline shrinking by a few milliseconds per call is a new cache key every call. A long scan opened one connection pool per LLM request and began evicting the oldest once it passed 128.Eviction is what crashed. Each analyzer node is synchronous and reaches
run_async()→asyncio.run(), so it owns its event loop. A pool evicted while a later analyzer is running belongs to an earlier, already-closed loop, and the OpenAI SDK's finaliser reacts to garbage collection withasyncio.get_running_loop().create_task(self.aclose())— closing the earlier analyzer's sockets from the current analyzer's loop. Nothing awaits that task, so it surfaced only as "Task exception was never retrieved", once per eviction.Fix
Apply the deadline to the client that already exists instead of baking it into a new one.
_retarget_request_timeout()inllm_analyzer_base.pymutatesclient.timeouton the live client forChatOpenAI,ChatAnthropic, andAgentCLIChatModel. Both SDKs readclient.timeoutwhen building each request — the same in-place mutation_uses_native_connection_retriesalready relies on formax_retries._model_for_call()retargets first and only falls back to constructing a replacement model for transports that keep no mutable deadline, so behaviour is unchanged for them._StructuredAgentCLIModelnow reads transport settings through its owner rather than copying them at construction, so a structured wrapper no longer pins the deadline it happened to be created with.with_structured_outputwraps that same model instance.Dynamic timeouts, the explicit retry loop, the global concurrency limiter, and the one-loop-per-analyzer execution model are all untouched.
Result: one pool per analyzer, one cache key, nothing to evict, and no cleanup that can outlive its loop.
Validation
openai3.11 /httpx2 combination that reported the failure goes from 75 unretrievedEvent loop is closedtasks and 220 httpx clients → 0 and 20 (one per analyzer).tests/nodes/test_llm_analyzer_base.pyandtests/unit/test_llm_utils.py.make test(unit): 4015 passed, 14 skipped, 4 xfailed.make lint: clean.make format/ruff format --check: no changes (198 files already formatted).Event loop is closed/Task exception was never retrievedmessages.Note:
tests/integration/test_graph.py::test_graph_surfaces_degraded_llm_stagefails in my local environment, but it fails identically on unmodifiedmain(verified by reverting only the two source files) — it is pre-existing and unrelated to this change.