Skip to content

fix: avoid reusing Weixin sessions across event loops#14481

Open
timfaner wants to merge 1 commit into
NousResearch:mainfrom
timfaner:fix/weixin-cron-loop-reuse
Open

fix: avoid reusing Weixin sessions across event loops#14481
timfaner wants to merge 1 commit into
NousResearch:mainfrom
timfaner:fix/weixin-cron-loop-reuse

Conversation

@timfaner

Copy link
Copy Markdown

Summary

  • prevent send_weixin_direct() from reusing a live Weixin adapter unless its aiohttp session belongs to the current event loop
  • fall back to a fresh one-shot aiohttp.ClientSession when the cached live adapter is bound to a different loop
  • add a regression test covering the cross-loop fallback path

Root cause

When cron delivery hit the Weixin fallback path after an initial send failure, it could reuse a live adapter/session created by the long-running gateway loop. If that session belonged to a different asyncio event loop than the current cron delivery context, aiohttp could fail with:

Timeout context manager should be used inside a task

That turned one recoverable Weixin send failure into a second Hermes-side delivery failure.

Fix

Gate live-adapter reuse on loop affinity:

  • read the current running loop with asyncio.get_running_loop()
  • inspect the live session loop
  • reuse the live adapter only when the session is open and belongs to the current loop
  • otherwise, skip reuse and send through a fresh one-shot session

Verification

  • added regression test: test_send_weixin_direct_ignores_live_adapter_bound_to_different_loop
  • ran python -m pytest tests/gateway/test_weixin.py -q -o 'addopts='
  • result: 43 passed
  • manually re-ran the affected cron job and confirmed the delivery error was cleared in the cron job state

Notes

This fixes the Hermes fallback bug. It does not change the upstream iLink error behavior itself; it prevents the fallback path from failing for an unrelated cross-event-loop reason.

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 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