fix(weixin): avoid cross-event-loop ClientSession reuse in send_weixin_direct#15911
Open
lihua881215 wants to merge 1 commit into
Open
fix(weixin): avoid cross-event-loop ClientSession reuse in send_weixin_direct#15911lihua881215 wants to merge 1 commit into
lihua881215 wants to merge 1 commit into
Conversation
…n_direct When a scheduled cron job (running on its own asyncio loop) triggers send_weixin_direct, the helper would reuse the cached live adapter's aiohttp.ClientSession that was bound to the gateway's main loop. aiohttp sessions are not safe to share across event loops, producing: Timeout context manager should be used inside a task Detect the mismatch by comparing the cached session's bound loop with the currently running loop. When they differ, fall through to creating a fresh ClientSession on the current loop instead of reusing the live adapter. Fixes NousResearch#13099 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Collaborator
This was referenced Apr 28, 2026
Open
2 tasks
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
Fixes #13099.
When a scheduled cron job runs on its own asyncio loop and triggers
send_weixin_direct, the helper reused the cached live adapter'saiohttp.ClientSessionthat was bound to the gateway's main loop.aiohttp sessions are not safe across event loops, producing:
Fix
Compare the cached session's
_loopwith the currently running loop.On mismatch, skip the live-adapter fast path and create a fresh
ClientSessionon the current loop. Same-loop behavior is unchanged.Test plan
TestWeixinSendDirectCrossEventLoop::test_cross_loop_falls_through_to_fresh_session— fakes a live adapter on loop A, calls send on loop B, asserts fresh session is usedTestWeixinSendDirectCrossEventLoop::test_same_loop_reuses_live_adapter— guards the existing fast pathtests/gateway/test_weixin.py(44 tests) passes