Skip to content

fix(weixin): resolve "Timeout context manager should be used inside a task" errors - #19050

Closed
rj-chenlinfeng wants to merge 2 commits into
NousResearch:mainfrom
rj-chenlinfeng:fix/weixin-aiohttp-timeout-context-manager
Closed

fix(weixin): resolve "Timeout context manager should be used inside a task" errors#19050
rj-chenlinfeng wants to merge 2 commits into
NousResearch:mainfrom
rj-chenlinfeng:fix/weixin-aiohttp-timeout-context-manager

Conversation

@rj-chenlinfeng

Copy link
Copy Markdown

Problem

Cron jobs that deliver messages via WeChat (微信) iLink adapter intermittently fail with:

"Timeout context manager should be used inside a task"

This causes message delivery failures for all scheduled tasks (HN news digest, oil price reports, stock updates, etc.).

Root Cause

aiohttp.ClientTimeout internally uses BaseTimerContext which calls loop.call_later(). When send() is invoked via asyncio.run_coroutine_threadsafe() from cron threads, the timer context is not bound to a proper asyncio Task, triggering the error.

Fix

Replace all direct aiohttp.ClientTimeout usage in weixin.py with asyncio.wait_for(), which is a pure-asyncio timeout mechanism that does not depend on Task-bound timer contexts.

Changes

Function Before After
_upload_ciphertext session.post(..., timeout=ClientTimeout(total=120)) asyncio.wait_for(coro, timeout=120)
_download_bytes session.get(..., timeout=ClientTimeout(...)) asyncio.wait_for(coro, timeout=...)
_download_remote_media self._send_session.get(..., timeout=ClientTimeout(total=30)) asyncio.wait_for(coro, timeout=30)
_send_session creation Default ClientTimeout ClientTimeout(total=None, ...) to disable
All sessions trust_env=True trust_env=False (bypass proxy for WeChat CDN)

Note: _api_post and _api_get were already fixed in a previous commit.

Testing

  • All 6 cron jobs (HN news, oil prices, stock daily, stock earnings, quantum weekly, Cisco blog) have been running with this fix
  • Message delivery confirmed working via WeChat iLink gateway

chenlinfeng added 2 commits April 30, 2026 18:42
aiohttp ClientTimeout uses BaseTimerContext which calls
loop.call_later() internally. When invoked via
asyncio.run_coroutine_threadsafe() from cron jobs, this
triggers "Timeout context manager should be used inside a task"
errors, causing message delivery failures.

Replace all direct ClientTimeout usage with asyncio.wait_for():
- _upload_ciphertext: CDN upload (120s timeout)
- _download_bytes: CDN download (configurable timeout)
- _download_remote_media: remote media fetch (30s timeout)

Also set total=None on _send_session to disable aiohttp built-in
timeout, and change trust_env=True to False to bypass proxy for
WeChat CDN connections.
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery labels May 3, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to #18014, #18417, #12810 — same aiohttp timeout context manager issue in WeChat gateway. This PR uses asyncio.wait_for() approach which differs from the other open PRs.

@alt-glitch alt-glitch added the platform/wecom WeCom / WeChat Work adapter label May 3, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to #18014, #18417, #12810 — same aiohttp timeout context manager issue in WeChat gateway.

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