fix(weixin): send_weixin_direct cross-loop session causes "Timeout context manager should be used inside a task" - #18890
Closed
MottledShadow wants to merge 1 commit into
Conversation
When send_message tool is called from inside a running gateway, the _run_async bridge spawns a worker thread with a separate event loop. send_weixin_direct then reuses the live adapter's aiohttp session which was created on the gateway's main loop. aiohttp's TimerContext checks asyncio.current_task(loop=session._loop) and sees None because we're executing on the worker thread's loop → raises 'Timeout context manager should be used inside a task'. Fix: skip the live-adapter shortcut when the session belongs to a different event loop, falling through to the fresh-session path.
Collaborator
Collaborator
|
Likely duplicate of #13350 |
9 tasks
Contributor
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
When
send_messagetool is invoked from inside a running gateway (e.g., CLI → send_message to weixin), thesend_messagetool handler is async and_run_async()spawns a worker thread with a separate event loop.send_weixin_direct()then prefers reusing the live adapter's aiohttp session — but that session was created on the gateway's main loop.aiohttp 3.13.5's
TimerContext.__enter__()calls:Since we are executing on the worker thread loop,
current_task(loop=gateway_loop)returnsNone, and every send attempt fails (5 retries with backoff, then error).This affects:
send_messagetext delivery to weixinsend_messageMEDIA: file delivery to weixinNormal inbound→reply works fine because it runs entirely on the gateway's main loop.
Fix
Before using the live adapter path, verify the session's event loop matches the current execution loop:
When loops differ, fall through to the existing fresh-session path (new
aiohttp.ClientSession()), which already works correctly.The feishu adapter avoids this issue entirely by always creating a new adapter instance in
_send_feishu()— it never touches the live adapter.Testing
send_messagetext to weixinsend_messageMEDIA: file attachment to weixinNotes
Uses
session._loop(aiohttp private attribute) for loop comparison. An alternative would be to fix this at the_run_asynclevel so gateway-session-bound tools stay on the main loop, but that would be a wider change and we judged this targeted fix to be the right tradeoff.