fix(weixin): resolve "Timeout context manager should be used inside a task" errors - #19050
Closed
rj-chenlinfeng wants to merge 2 commits into
Closed
Conversation
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.
Collaborator
Collaborator
9 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Cron jobs that deliver messages via WeChat (微信) iLink adapter intermittently fail with:
This causes message delivery failures for all scheduled tasks (HN news digest, oil price reports, stock updates, etc.).
Root Cause
aiohttp.ClientTimeoutinternally usesBaseTimerContextwhich callsloop.call_later(). Whensend()is invoked viaasyncio.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.ClientTimeoutusage inweixin.pywithasyncio.wait_for(), which is a pure-asyncio timeout mechanism that does not depend on Task-bound timer contexts.Changes
_upload_ciphertextsession.post(..., timeout=ClientTimeout(total=120))asyncio.wait_for(coro, timeout=120)_download_bytessession.get(..., timeout=ClientTimeout(...))asyncio.wait_for(coro, timeout=...)_download_remote_mediaself._send_session.get(..., timeout=ClientTimeout(total=30))asyncio.wait_for(coro, timeout=30)_send_sessioncreationClientTimeoutClientTimeout(total=None, ...)to disabletrust_env=Truetrust_env=False(bypass proxy for WeChat CDN)Note:
_api_postand_api_getwere already fixed in a previous commit.Testing