Skip to content

fix: cross-channel send_message failures (WeChat event loop + Telegram proxy)#18714

Closed
liuhao1024 wants to merge 2 commits into
NousResearch:mainfrom
liuhao1024:fix/weixin-event-loop-telegram-proxy
Closed

fix: cross-channel send_message failures (WeChat event loop + Telegram proxy)#18714
liuhao1024 wants to merge 2 commits into
NousResearch:mainfrom
liuhao1024:fix/weixin-event-loop-telegram-proxy

Conversation

@liuhao1024

@liuhao1024 liuhao1024 commented May 2, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Two fixes for cross-channel messaging via the send_message tool:

1. fix(weixin): prevent event-loop mismatch in send_weixin_direct()

When send_message targets WeChat from a different async context (e.g. cron delivery, cross-channel Telegram→WeChat, or TTS tool), _run_async creates a new event loop in a worker thread. The live adapter's _send_session is bound to the gateway's main event loop, so using it from the worker thread's loop causes:

Timeout context manager should be used inside a task

Fix: Check if send_session._loop matches the current running loop before reusing the live adapter. If they don't match, fall through to the fresh-session fallback path.

Root cause: aiohttp's timeout context manager requires the session to be used within the same event loop it was created in. The original code didn't verify this.

2. fix(send_message): add proxy support for _send_telegram()

The send_message tool's _send_telegram() created Bot(token=token) without proxy config, causing timeouts when TELEGRAM_PROXY is configured. The gateway adapter already handles this via resolve_proxy_url() + HTTPXRequest(proxy=...), but the one-shot tool path didn't.

Fix: Use resolve_proxy_url('TELEGRAM_PROXY') and pass proxy to HTTPXRequest when available, matching the gateway adapter's pattern.

Related Issue

Fixes #17347

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • See commit messages for detailed changes

How to Test

  1. Run pytest tests/ -q — all tests should pass
  2. Verify the specific scenario described above is resolved

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 26.4.1

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture and workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter labels May 2, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #12370 — same dual-fix scope (WeChat event loop + Telegram proxy in send_message).

1 similar comment
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #12370 — same dual-fix scope (WeChat event loop + Telegram proxy in send_message).

liuhao1024 added 2 commits May 2, 2026 22:41
…m proxy)

Two fixes for cross-channel messaging via the send_message tool:

1. fix(weixin): prevent event-loop mismatch in send_weixin_direct()
   - When called via _run_async from a different async context (e.g. cron
     delivery or cross-channel Telegram→WeChat), the live adapter's aiohttp
     session is bound to the gateway's main event loop.
   - Using it from _run_async's new thread's loop causes
     "Timeout context manager should be used inside a task".
   - Added event loop compatibility check: if the session's _loop doesn't
     match the current running loop, skip adapter reuse and fall through
     to the fresh-session fallback path.
   - Fixes NousResearch#17347, NousResearch#13099, NousResearch#13281, NousResearch#18014

2. fix(send_message): add proxy support for _send_telegram()
   - send_message tool's _send_telegram() created Bot(token=token) without
     proxy config, causing timeouts when TELEGRAM_PROXY is configured.
   - Now uses resolve_proxy_url() + HTTPXRequest(proxy=...) matching
     the gateway adapter's pattern.
When iLink returns ret=-2 (rate limited), the adapter exhausts its
internal retries and raises. _send_weixin now catches rate-limit errors
and retries up to 3 times with 10s/20s/30s backoff before giving up.
@teknium1

Copy link
Copy Markdown
Contributor

This appears to be implemented on current main, so this automated hermes-sweeper review recommends closing it as already fixed.

Evidence:

  • gateway/platforms/weixin.py:2288 now only reuses the live Weixin adapter when send_session._loop is asyncio.get_running_loop(), and otherwise falls through to the fresh-session path at gateway/platforms/weixin.py:2315.
  • tools/send_message_tool.py:870 resolves TELEGRAM_PROXY for the standalone Telegram send path and passes it into HTTPXRequest(proxy=...) at tools/send_message_tool.py:884-888.
  • tests/tools/test_send_message_telegram_proxy.py:65 covers the standalone _send_telegram() proxy behavior.
  • The PR's later Weixin rate-limit retry concern is also covered on main by the adapter-level retry/backoff/circuit-breaker logic in gateway/platforms/weixin.py:1733-1791.
  • The prior maintainer note that this is likely a duplicate of fix: cross-channel send_message failures (Telegram proxy + WeChat event loop) #12370 matches the current code state.

@teknium1 teknium1 closed this Jun 10, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jun 10, 2026
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/telegram Telegram bot adapter sweeper:implemented-on-main Sweeper: behavior already present on current main type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Weixin send_message fails with "Timeout context manager should be used inside a task"

3 participants