Skip to content

fix(weixin): handle cross-event-loop session reuse in send_weixin_direct - #19481

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

fix(weixin): handle cross-event-loop session reuse in send_weixin_direct#19481
eipiem1 wants to merge 1 commit into
NousResearch:mainfrom
eipiem1:fix/weixin-cron-cross-loop-session

Conversation

@eipiem1

@eipiem1 eipiem1 commented May 4, 2026

Copy link
Copy Markdown

Problem

When cron jobs deliver results to Weixin, send_weixin_direct() attempts to reuse the cached live adapter's aiohttp session (created on the gateway's event loop). However, standalone cron delivery runs via asyncio.run() on a different event loop.

aiohttp's TimerContext.__enter__() calls asyncio.current_task(loop=gateway_loop), which returns None on the standalone loop, raising:

RuntimeError: Timeout context manager should be used inside a task

This causes all cron deliveries to the Weixin (WeChat) platform to fail silently.

Fix

Wrap the live-adapter reuse path in try/except RuntimeError so it falls through to creating a fresh aiohttp.ClientSession on the current event loop. The fresh session path was already implemented — it just wasn't reachable when a cached live adapter was present.

Impact

  • ✅ Cron → Weixin deliveries now work correctly
  • ✅ Gateway → Weixin deliveries (same event loop) continue to work unchanged
  • ✅ No performance regression — live adapter is still preferred when on the same loop
  • ✅ Minimal change: ~12 lines added

When cron jobs deliver results to Weixin, send_weixin_direct() attempts to
reuse the cached live adapter's aiohttp session (created on the gateway's
event loop). However, standalone cron delivery runs via asyncio.run() on a
different event loop. aiohttp's TimerContext.__enter__() calls
asyncio.current_task(loop=gateway_loop) which returns None on the standalone
loop, raising RuntimeError('Timeout context manager should be used inside a
task').

Fix: wrap the live-adapter reuse path in try/except RuntimeError so it falls
through to creating a fresh aiohttp session on the current event loop.

Fixes cron delivery failures for Weixin platform.
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery comp/cron Cron scheduler and job management platform/wecom WeCom / WeChat Work adapter P2 Medium — degraded but workaround exists duplicate This issue or pull request already exists labels May 4, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #14481, #12810, #15911 — same cross-event-loop aiohttp session reuse in send_weixin_direct causing RuntimeError on cron delivery. Multiple open fix PRs already exist.

@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #14481, #12810, #15911 — same cross-event-loop aiohttp session reuse issue.

@eipiem1

eipiem1 commented May 4, 2026

Copy link
Copy Markdown
Author

Thanks for the heads up @alt-glitch! Closing in favor of the existing PRs (#14481, #12810, #15911). The fix was already known — appreciate the pointer.

@eipiem1 eipiem1 closed this May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cron Cron scheduler and job management comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists 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.

2 participants