fix(weixin): skip live adapter session when event loop mismatches in send_weixin_direct - #28835
Closed
handouwenjin wants to merge 1 commit into
Closed
fix(weixin): skip live adapter session when event loop mismatches in send_weixin_direct#28835handouwenjin wants to merge 1 commit into
handouwenjin wants to merge 1 commit into
Conversation
…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'
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 |
Collaborator
19 tasks
Contributor
|
Thanks for the fix. An automated hermes-sweeper review found this behavior is already implemented on current Evidence:
This is an automated hermes-sweeper review. |
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
Cron job delivery to WeChat/Weixin fails with:
Root Cause
When
send_weixin_directis called underasyncio.run()(e.g. cron standalone delivery), it picks up the live adapter from_LIVE_ADAPTERSand reuses its_send_session. However, that session was created on the gateway's main event loop, not theasyncio.run()loop.aiohttp's
TimerContext.__enter__checksasyncio.current_task(loop=session._loop)— since the session is bound to the old loop and no task is running on it, this returnsNoneand 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 freshClientSessionon the current loop.Changes
gateway/platforms/weixin.py: Guard the live-adapter fast path with loop identity checktests/gateway/test_weixin.py: AddedTestSendWeixinDirectCrossLoopclass with two tests:test_cross_loop_falls_through_to_fresh_session— verifies cross-loop delivery works without RuntimeErrortest_same_loop_uses_live_adapter— verifies same-loop delivery still uses the live adapterVerification
All 44 existing tests in
test_weixin.pycontinue to pass (zero regressions).