fix(wecom): dispatch WS SDK calls to ws_loop to fix asyncio event loop conflict#3300
Open
octo-patch wants to merge 1 commit intoagentscope-ai:mainfrom
Open
Conversation
|
Hi @octo-patch, this is your 18th Pull Request. 📋 About PR TemplateTo help maintainers review your PR faster, please make sure to include:
Complete PR information helps speed up the review process. You can edit the PR description to add these details. 🙌 Join Developer CommunityThanks so much for your contribution! We'd love to invite you to join the official QwenPaw developer group! You can find the Discord and DingTalk group links under the "Developer Community" section on our docs page: We truly appreciate your enthusiasm—and look forward to your future contributions! 😊 We'll review your PR soon. |
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 #3296
Problem
When using the WeCom channel with
send_file_to_useror text replies, the SDK internally usesasyncio.ensure_future()which attaches tasks to whatever loop is currently running. Since the WebSocket loop (_ws_loop) runs in a dedicated thread separate from the main agent loop (_loop), this causes:This prevents files/photos from being sent and causes text reply failures.
Root Cause
Two event loops are running:
self._loop— main agent/logic loopself._ws_loop— WebSocket loop in a dedicated threadIn
_send_ws_cmd(),asyncio.get_event_loop()may return the wrong loop forFuturecreation. More critically,self._client._ws_manager.send()andself._client.reply_stream()internally callasyncio.ensure_future(), attaching tasks to the main loop — but the WS operations need to run onself._ws_loop.Solution
_send_ws_cmd(): Useself._loop or asyncio.get_running_loop()forFuturecreation. Dispatch_ws_manager.send()toself._ws_loopviaasyncio.run_coroutine_threadsafe()+asyncio.wrap_future()when the WS loop is running._send_text_via_frame(): Similarly dispatchreply_stream()toself._ws_loopwhen available.Both methods fall back to direct
awaitwhen_ws_loopis not running (e.g. during tests or single-loop setups).Testing
Verified by the bug reporter: after applying the fix,
send_file_to_usercorrectly sends photos and files to users via WeCom.