hermes - ✅(Solved) Fix Bug: Cron job delivery to WeChat fails with "Timeout context manager should be used inside a task" [1 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
NousResearch/hermes-agent#18014Fetched 2026-05-01 05:54:22
View on GitHub
Comments
2
Participants
2
Timeline
10
Reactions
0
Timeline (top)
labeled ×5commented ×2cross-referenced ×1subscribed ×1

Error Message

When a cron job attempts to deliver a message to WeChat (Weixin), it fails with the following error: 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. 4. Observe the error in logs

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.

Fix Action

Fixed

PR fix notes

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

Description (problem / solution / changelog)

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

Changed files

  • gateway/platforms/weixin.py (modified, +14/-1)
  • tests/gateway/test_weixin_session_loop.py (added, +118/-0)

Code Example

Timeout context manager should be used inside a task

---

# 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...
RAW_BUFFERClick to expand / collapse

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.

extent analysis

TL;DR

Create a new aiohttp.ClientSession instance when the current event loop doesn't match the session's loop to resolve the timeout context manager error.

Guidance

  • Verify that the send_weixin_direct() function is indeed creating an aiohttp.ClientSession that gets reused across different event loops, causing the error.
  • Implement the proposed fix by adding an event loop consistency check in send_weixin_direct() to create a new session when the current event loop doesn't match the session's loop.
  • Ensure that the new session is properly closed when it's no longer needed to avoid resource leaks.
  • Test the fix by reproducing the error using the provided steps and verifying that the cron job executes successfully with WeChat delivery.

Example

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

Notes

This fix assumes that the aiohttp library is being used correctly and that the event loop consistency check is sufficient to resolve the issue. Additional debugging may be necessary if the problem persists.

Recommendation

Apply the proposed workaround by implementing the event loop consistency check in send_weixin_direct() to create a new session when necessary, as this directly addresses the identified root cause of the issue.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

hermes - ✅(Solved) Fix Bug: Cron job delivery to WeChat fails with "Timeout context manager should be used inside a task" [1 pull requests, 2 comments, 2 participants]