Skip to content

fix(weixin): avoid cross-event-loop ClientSession reuse in send_weixin_direct#15911

Open
lihua881215 wants to merge 1 commit into
NousResearch:mainfrom
lihua881215:fix/weixin-cross-event-loop
Open

fix(weixin): avoid cross-event-loop ClientSession reuse in send_weixin_direct#15911
lihua881215 wants to merge 1 commit into
NousResearch:mainfrom
lihua881215:fix/weixin-cross-event-loop

Conversation

@lihua881215

Copy link
Copy Markdown

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's
aiohttp.ClientSession that was bound to the gateway's main loop.
aiohttp sessions are not safe across event loops, producing:

Timeout context manager should be used inside a task

Fix

Compare the cached session's _loop with the currently running loop.
On mismatch, skip the live-adapter fast path and create a fresh
ClientSession on the current loop. Same-loop behavior is unchanged.

Test plan

  • New 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 used
  • New TestWeixinSendDirectCrossEventLoop::test_same_loop_reuses_live_adapter — guards the existing fast path
  • Full tests/gateway/test_weixin.py (44 tests) passes

…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>
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery platform/wecom WeCom / WeChat Work adapter labels Apr 26, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #13520 — same cross-event-loop aiohttp session reuse fix for Weixin adapter (send_weixin_direct). Also related to #14481, #14873 which address the same root cause from #13099.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/wecom WeCom / WeChat Work adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: WeChat send_message causes "Timeout context manager should be used inside a task" error

2 participants