Skip to content

fix(weixin): guard against event-loop mismatch in send_weixin_direct - #18040

Closed
luyao618 wants to merge 1 commit into
NousResearch:mainfrom
luyao618:fix/weixin-cron-event-loop-session-mismatch
Closed

fix(weixin): guard against event-loop mismatch in send_weixin_direct#18040
luyao618 wants to merge 1 commit into
NousResearch:mainfrom
luyao618:fix/weixin-cron-event-loop-session-mismatch

Conversation

@luyao618

Copy link
Copy Markdown
Contributor

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's aiohttp.ClientSession. Reusing that session across loops causes:

Timeout context manager should be used inside a task

Root Cause

send_weixin_direct() checks for a live adapter and reuses its _send_session if 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 from session._loop, the fast path is skipped and the function falls through to the existing one-shot async with aiohttp.ClientSession(...) path, which always creates a session on the correct loop.

Changes

  • gateway/platforms/weixin.py: Added _session_loop_ok guard in send_weixin_direct()
  • tests/gateway/test_weixin_session_loop.py: Added 2 tests covering both the stale-loop (skip) and same-loop (reuse) paths

Testing

pytest tests/gateway/test_weixin_session_loop.py -v  # 2 passed

…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
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists labels Apr 30, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #12810 — same root cause: cross-event-loop aiohttp ClientSession reuse in send_weixin_direct(). At least 5 open PRs already address this (#12810, #14873, #15911, #16790).

1 similar comment
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #12810 — same root cause: cross-event-loop aiohttp ClientSession reuse in send_weixin_direct(). At least 5 open PRs already address this (#12810, #14873, #15911, #16790).

@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #12810.

1 similar comment
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #12810.

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 P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Cron job delivery to WeChat fails with "Timeout context manager should be used inside a task"

2 participants