Skip to content

fix(weixin): replace aiohttp ClientTimeout with asyncio.wait_for (salvage #19050) - #21196

Merged
teknium1 merged 3 commits into
mainfrom
salvage/pr-19050
May 7, 2026
Merged

fix(weixin): replace aiohttp ClientTimeout with asyncio.wait_for (salvage #19050)#21196
teknium1 merged 3 commits into
mainfrom
salvage/pr-19050

Conversation

@teknium1

@teknium1 teknium1 commented May 7, 2026

Copy link
Copy Markdown
Contributor

Closes #19050 via salvage.

Summary

Cron-delivered WeChat messages fail with "Timeout context manager should be used inside a task" because aiohttp.ClientTimeout uses loop.call_later() which isn't bound to a Task when invoked via asyncio.run_coroutine_threadsafe(). Standard fix: replace with asyncio.wait_for().

Changes

  • gateway/platforms/weixin.py: swap aiohttp.ClientTimeoutasyncio.wait_for() at 3 call sites (_upload_ciphertext, _download_bytes, _download_remote_media). Set total=None on _send_session's ClientTimeout so aiohttp's internal timer doesn't fire outside a Task context either.

Improvements during salvage

  • Dropped the original PR's trust_env=Truetrust_env=False changes. That was unrelated to the timeout bug and would silently break users who proxy WeChat through HTTP_PROXY (common in China).
  • Updated test_send_file_uses_post_for_upload_full_url_and_hex_encoded_aes_key — timeout no longer appears as a session.post() kwarg since it's enforced externally now.

Validation

scripts/run_tests.sh tests/gateway/test_weixin.py → 51 passed.

Original author: @noOne-list.

chenlinfeng and others added 3 commits May 7, 2026 05:07
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.
@teknium1
teknium1 merged commit d856f45 into main May 7, 2026
10 of 11 checks passed
@teknium1
teknium1 deleted the salvage/pr-19050 branch May 7, 2026 12:10
@github-actions

github-actions Bot commented May 7, 2026

Copy link
Copy Markdown
Contributor

🔎 Lint report: salvage/pr-19050 vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 7482 on HEAD, 7483 on base (✅ -1)

🆕 New issues (1):

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant