fix(weixin): guard against event-loop mismatch in send_weixin_direct - #18040
Closed
luyao618 wants to merge 1 commit into
Closed
fix(weixin): guard against event-loop mismatch in send_weixin_direct#18040luyao618 wants to merge 1 commit into
luyao618 wants to merge 1 commit into
Conversation
…ousResearch#18014) When a cron job runs in a different event loop than the gateway adapter, reusing the adapter's aiohttp.ClientSession raises 'Timeout context manager should be used inside a task'. Add a loop identity check before reusing the live adapter's session — on mismatch, fall through to the one-shot ClientSession path that already works correctly. Closes NousResearch#18014
Collaborator
1 similar comment
Collaborator
Collaborator
|
Likely duplicate of #12810. |
1 similar comment
Collaborator
|
Likely duplicate of #12810. |
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 #18014.
When a cron job delivers to WeChat via
send_weixin_direct(), it may run in a different event loop than the one that created the gateway adapter'saiohttp.ClientSession. Reusing that session across loops causes:Root Cause
send_weixin_direct()checks for a live adapter and reuses its_send_sessionif available and not closed. However, it does not verify that the session's event loop matches the currently running loop. In cron execution, the scheduler creates a new event loop per run, making the cached session invalid.Fix
Added a loop identity check before reusing the live adapter's session. When
asyncio.get_running_loop()differs fromsession._loop, the fast path is skipped and the function falls through to the existing one-shotasync with aiohttp.ClientSession(...)path, which always creates a session on the correct loop.Changes
gateway/platforms/weixin.py: Added_session_loop_okguard insend_weixin_direct()tests/gateway/test_weixin_session_loop.py: Added 2 tests covering both the stale-loop (skip) and same-loop (reuse) pathsTesting