Skip to content

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

@fengfeng-yang

Description

@fengfeng-yang

Bug Description

When a cron job attempts to deliver a message to WeChat (Weixin), it fails with the following error:

Timeout context manager should be used inside a task

Root Cause

In weixin.py, the send_weixin_direct() function creates an aiohttp.ClientSession that gets reused across different event loops. When a cron job runs in a new session with a different event loop, the existing session (tied to the old loop) becomes invalid, causing the timeout context manager error.

Proposed Fix

Add an event loop consistency check in send_weixin_direct(). When the current event loop doesn't match the session's loop, create a new session instead of reusing the existing one.

# Before (pseudocode)
if session is None:
    session = aiohttp.ClientSession()
# use session...

# After (pseudocode)
current_loop = asyncio.get_event_loop()
if session is None or session._loop is not current_loop:
    if session and not session.closed:
        await session.close()
    session = aiohttp.ClientSession()
# use session...

Steps to Reproduce

  1. Configure WeChat as a delivery target in config.yaml
  2. Create a cron job with deliver: weixin
  3. Wait for the cron job to execute
  4. Observe the error in logs

Environment

  • Hermes Agent version: latest (as of 2026-04)
  • Delivery target: WeChat (Weixin)
  • Trigger: Cron job scheduled delivery

Impact

All cron jobs with WeChat delivery fail silently. Other delivery targets (Feishu, Telegram, etc.) are not affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/cronCron scheduler and job managementcomp/gatewayGateway runner, session dispatch, deliveryduplicateThis issue or pull request already existssweeper:implemented-on-mainSweeper: behavior already present on current maintype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions