fix(agent): prevent AsyncOpenAI/httpx cross-loop deadlock in gateway mode#2701
Merged
teknium1 merged 1 commit intoNousResearch:mainfrom Mar 26, 2026
Merged
Conversation
…mode In gateway mode, async tools (vision_analyze, web_extract, session_search) deadlock because _run_async() spawns a thread with asyncio.run(), creating a new event loop, but _get_cached_client() returns an AsyncOpenAI client bound to a different loop. httpx.AsyncClient cannot work across event loop boundaries, causing await client.chat.completions.create() to hang forever. Fix: include the event loop identity in the async client cache key so each loop gets its own AsyncOpenAI instance. Also fix session_search_tool.py which had its own broken asyncio.run()-in-thread pattern — now uses the centralized _run_async() bridge.
outsourc-e
pushed a commit
to outsourc-e/hermes-agent
that referenced
this pull request
Mar 26, 2026
…mode (NousResearch#2701) In gateway mode, async tools (vision_analyze, web_extract, session_search) deadlock because _run_async() spawns a thread with asyncio.run(), creating a new event loop, but _get_cached_client() returns an AsyncOpenAI client bound to a different loop. httpx.AsyncClient cannot work across event loop boundaries, causing await client.chat.completions.create() to hang forever. Fix: include the event loop identity in the async client cache key so each loop gets its own AsyncOpenAI instance. Also fix session_search_tool.py which had its own broken asyncio.run()-in-thread pattern — now uses the centralized _run_async() bridge.
StreamOfRon
pushed a commit
to StreamOfRon/hermes-agent
that referenced
this pull request
Mar 29, 2026
…mode (NousResearch#2701) In gateway mode, async tools (vision_analyze, web_extract, session_search) deadlock because _run_async() spawns a thread with asyncio.run(), creating a new event loop, but _get_cached_client() returns an AsyncOpenAI client bound to a different loop. httpx.AsyncClient cannot work across event loop boundaries, causing await client.chat.completions.create() to hang forever. Fix: include the event loop identity in the async client cache key so each loop gets its own AsyncOpenAI instance. Also fix session_search_tool.py which had its own broken asyncio.run()-in-thread pattern — now uses the centralized _run_async() bridge.
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.
Summary
vision_analyze,web_extract, andsession_searchhang forever because cached AsyncOpenAI clients are reused across different event loopssession_search_tool.py's manualasyncio.run()in ThreadPoolExecutor with the centralized_run_async()bridgeFixes #2681. Related to #2338.
Relationship to #2682
PR #2682 fixes the same issue but only for
vision_analyzeby switching to synccall_llm. This PR fixes the root cause in the client cache layer, so all async tools are fixed without modifying each tool individually:async_call_llmvision_analyzeweb_extractsession_searchmixture_of_agentsBoth approaches are compatible — #2682's sync switch is a reasonable defense-in-depth for vision specifically, while this PR prevents the class of bug from affecting any current or future async tool.
Root Cause
In gateway mode,
_run_async()spawns a new thread withasyncio.run()which creates a fresh event loop. But_get_cached_client()returns an AsyncOpenAI client that was created on (and bound to) a different loop. Sincehttpx.AsyncClientcannot operate across event loop boundaries,await client.chat.completions.create()hangs indefinitely.session_search_tool.pyhad the same bug independently — its ownasyncio.run()in aThreadPoolExecutorcreated the same cross-loop conflict.Changes
agent/auxiliary_client.py— Addid(current_loop)to the async client cache key so each event loop gets its own AsyncOpenAI instance. Sync clients (no loop binding) are unaffected.tools/session_search_tool.py— Replace manualasyncio.run()in ThreadPoolExecutor with_run_async()which properly handles loop lifecycle across CLI, gateway, and worker-thread contexts.tests/test_crossloop_client_cache.py— 5 new tests:asyncio.runin thread gets fresh client)How to Test
vision_analyze)session_search)Tested On