fix: weixin send crosses event loops when called from tool handlers - #17319
Closed
zdev0x wants to merge 1 commit into
Closed
fix: weixin send crosses event loops when called from tool handlers#17319zdev0x wants to merge 1 commit into
zdev0x wants to merge 1 commit into
Conversation
send_weixin_direct() reused the live adapter's aiohttp session, but _run_async() spawns a fresh thread+loop for async tool handlers in the gateway context. aiohttp sessions cannot be used across event loops, causing 'Timeout context manager should be used inside a task'. Detect the loop mismatch via poll_task.get_loop() and fall back to a standalone session when the loops differ.
Collaborator
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.
Problem
send_weixin_direct()reused the live adapter'saiohttp.ClientSession, but_run_async()spawns a fresh thread + event loop for async tool handlers in the gateway context. aiohttp sessions cannot be used across event loops, causing:This makes the
send_messagetool fail every time it tries to send via Weixin, while the gateway is running.Root Cause
When the gateway is running,
send_weixin_direct()takes Path 1 (reuse live adapter session) because_LIVE_ADAPTERShas the adapter registered. But_run_async()runs the coroutine in a new thread withasyncio.run(), creating a new event loop. The aiohttp session from the gateway's loop can't be used there.Fix
Detect the event loop mismatch via
poll_task.get_loop()and fall back to a standalone session (Path 2) when the loops differ.``\python
Before
if live_adapter is not None and send_session is not None and not send_session.closed:
After
can_reuse = (
live_adapter is not None
and send_session is not None
and not send_session.closed
)
if can_reuse and live_adapter._poll_task is not None:
try:
task_loop = live_adapter._poll_task.get_loop()
except AttributeError:
task_loop = None # Python <3.9 — fall through to safe path
if task_loop is not asyncio.get_running_loop():
can_reuse = False
if can_reuse: