Skip to content

fix(weixin): skip live adapter session when event loop mismatches in send_weixin_direct - #28835

Closed
handouwenjin wants to merge 1 commit into
NousResearch:mainfrom
handouwenjin:fix/weixin-cross-loop-session
Closed

fix(weixin): skip live adapter session when event loop mismatches in send_weixin_direct#28835
handouwenjin wants to merge 1 commit into
NousResearch:mainfrom
handouwenjin:fix/weixin-cross-loop-session

Conversation

@handouwenjin

Copy link
Copy Markdown

Problem

Cron job delivery to WeChat/Weixin fails with:

delivery error: Weixin send failed: Timeout context manager should be used inside a task

Root Cause

When send_weixin_direct is called under asyncio.run() (e.g. cron standalone delivery), it picks up the live adapter from _LIVE_ADAPTERS and reuses its _send_session. However, that session was created on the gateway's main event loop, not the asyncio.run() loop.

aiohttp's TimerContext.__enter__ checks asyncio.current_task(loop=session._loop) — since the session is bound to the old loop and no task is running on it, this returns None and raises the RuntimeError.

Fix

Added an event-loop identity check (asyncio.get_running_loop() is send_session._loop) before using the live adapter's session. When the loops don't match, the function falls through to creating a fresh ClientSession on the current loop.

Changes

  • gateway/platforms/weixin.py: Guard the live-adapter fast path with loop identity check
  • tests/gateway/test_weixin.py: Added TestSendWeixinDirectCrossLoop class with two tests:
    • test_cross_loop_falls_through_to_fresh_session — verifies cross-loop delivery works without RuntimeError
    • test_same_loop_uses_live_adapter — verifies same-loop delivery still uses the live adapter

Verification

All 44 existing tests in test_weixin.py continue to pass (zero regressions).

…send_weixin_direct

When send_weixin_direct is called under asyncio.run() (e.g. cron
standalone delivery), the live adapter's _send_session was created on
the gateway's main event loop.  Reusing it on the new loop triggers
aiohttp's 'Timeout context manager should be used inside a task'
RuntimeError because asyncio.current_task(loop=stale_loop) returns None.

Guard the live-adapter fast path with an event-loop identity check so
cross-loop callers fall through to creating a fresh ClientSession on
the current loop.

Fixes: Cron WeChat delivery failures with 'Weixin send failed: Timeout
context manager should be used inside a task'
@handouwenjin

Copy link
Copy Markdown
Author

hi @Teknium, this PR fixes a recurring cron delivery bug where WeChat messages fail with "Timeout context manager should be used inside a task". Root cause: cross-event-loop ClientSession reuse in send_weixin_direct. The fix adds a loop identity check + tests (44/44 pass). Would you please review and merge? Thanks!

@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have platform/wecom WeCom / WeChat Work adapter comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists labels May 19, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #12810. The authoritative fix was merged as #19141. Multiple other PRs (#14873, #15911, #16790, #17557, #18040) already addressed this same cross-event-loop ClientSession reuse in send_weixin_direct().

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the fix. An automated hermes-sweeper review found this behavior is already implemented on current main, so this PR is safe to close as superseded.

Evidence:

  • gateway/platforms/weixin.py:2288-2290 now gates the live-adapter fast path on send_session._loop is asyncio.get_running_loop(), so cross-loop callers fall through to the fresh ClientSession path.
  • Commit a22465e07ab4b71019f711e7a6463f6590c50742 added that exact Weixin send_weixin_direct cross-loop session check.
  • The fix came through merged PR fix(weixin): send_weixin_direct cross-loop session check #19141 (9b5b88b5e028f8c799053aae624be40e616b5d8d), and the PR discussion here already noted fix(weixin): send_weixin_direct cross-loop session check #19141 as the authoritative duplicate fix.
  • The fix is contained in release tag v2026.5.7 and later tags.

This is an automated hermes-sweeper review.

@teknium1 teknium1 closed this Jun 15, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jun 15, 2026
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 duplicate This issue or pull request already exists P3 Low — cosmetic, nice to have platform/wecom WeCom / WeChat Work adapter sweeper:implemented-on-main Sweeper: behavior already present on current main type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants