fix(weixin): replace aiohttp ClientTimeout with asyncio.wait_for (salvage #19050) - #21196
Merged
Conversation
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.
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
unresolved-attribute |
1 |
First entries
gateway/platforms/weixin.py:1842: [unresolved-attribute] unresolved-attribute: Attribute `get` is not defined on `None` in union `Unknown | None`
✅ Fixed issues: none
Unchanged: 3930 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
This was referenced May 20, 2026
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.
Closes #19050 via salvage.
Summary
Cron-delivered WeChat messages fail with "Timeout context manager should be used inside a task" because
aiohttp.ClientTimeoutusesloop.call_later()which isn't bound to a Task when invoked viaasyncio.run_coroutine_threadsafe(). Standard fix: replace withasyncio.wait_for().Changes
gateway/platforms/weixin.py: swapaiohttp.ClientTimeout→asyncio.wait_for()at 3 call sites (_upload_ciphertext,_download_bytes,_download_remote_media). Settotal=Noneon_send_session's ClientTimeout so aiohttp's internal timer doesn't fire outside a Task context either.Improvements during salvage
trust_env=True→trust_env=Falsechanges. That was unrelated to the timeout bug and would silently break users who proxy WeChat throughHTTP_PROXY(common in China).test_send_file_uses_post_for_upload_full_url_and_hex_encoded_aes_key— timeout no longer appears as asession.post()kwarg since it's enforced externally now.Validation
scripts/run_tests.sh tests/gateway/test_weixin.py→ 51 passed.Original author: @noOne-list.