fix: cross-channel send_message failures (Telegram proxy + WeChat event loop)#12370
Closed
calonye wants to merge 2 commits into
Closed
fix: cross-channel send_message failures (Telegram proxy + WeChat event loop)#12370calonye wants to merge 2 commits into
calonye wants to merge 2 commits into
Conversation
…nt loop)
Two fixes for cross-channel messaging via the send_message tool:
1. 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.
2. fix(weixin): prevent event-loop mismatch in send_weixin_direct()
- When called via _run_async from a different async context (e.g.
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 connector
loop doesn't match the current running loop, skip adapter reuse
and fall through to the fresh-session fallback path.
3. fix(telegram): add missing finalize kwarg to edit_message()
- edit_message() was called with finalize=... by the gateway but the
method signature didn't accept it, causing TypeError.
1. fix(gateway): always write .clean_shutdown marker on stop Previously, when drain timed out (e.g. agent mid-API-call during restart), the marker was skipped, causing all recently-active sessions to be suspended on next startup. This meant EVERY restart lost the active conversation. Now we always write the marker because: - The interrupt signal has been sent; agents handle it cleanly - 3-restart stuck-loop detection (#7536) already suspends truly stuck sessions as a safety net - Losing every conversation on restart is far worse than resuming an interrupted one 2. fix(proxy): detect SOCKS proxy in macOS system proxy fallback _detect_macos_system_proxy() now checks SOCKSEnable/SOCKSProxy/ SOCKSPort from scutil --proxy, returning socks5:// URLs. Added comment explaining why HTTP scheme is used for HTTPS proxies (HTTP CONNECT tunnelling). Env vars (ALL_PROXY, TELEGRAM_PROXY etc.) remain the recommended way to configure any proxy scheme.
This was referenced May 2, 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.
Problem
This PR addresses four issues:
Telegram send failed: Timed outTimeout context manager should be used inside a taskgateway restartloses the active conversationRoot Causes & Fixes
1.
_send_telegram()missing proxy support (send_message_tool.py)The
send_messagetool's_send_telegram()createdBot(token=token)without proxy configuration, bypassingTELEGRAM_PROXY. The gateway adapter correctly uses the proxy, but the tool's direct send path didn't.Fix: Added
resolve_proxy_url("TELEGRAM_PROXY")+HTTPXRequest(proxy=...), matching the gateway adapter's pattern.2.
send_weixin_direct()event loop mismatch (weixin.py)When
send_weixin_direct()is called via_run_async()from a different async context (e.g. cross-channel Telegram→WeChat),_run_asyncspawns a new thread withasyncio.run()creating a fresh event loop. The code tries to reuse alive_adapterfrom_LIVE_ADAPTERS, but that adapter's aiohttp session is bound to the gateway's main event loop. Using it from the new loop causes aiohttp's timeout context manager to fail.Fix: Added event loop compatibility check. If the live adapter's aiohttp session connector loop doesn't match the current running loop, skip adapter reuse and fall through to the existing fresh-session fallback path.
3.
edit_message()missingfinalizekwarg (telegram.py)The gateway calls
TelegramAdapter.edit_message(..., finalize=...)but the method signature didn't accept this keyword argument, causingTypeError.Fix: Added
finalize: bool = Falseparameter.4. Session continuity on restart (
gateway/run.py)When
gateway restartis called,_drain_active_agents()waits up to 60s for running agents to finish. The current agent can't self-terminate mid-response, so drain times out. The code then skips writing the.clean_shutdownmarker, causing the next startup to callsuspend_recently_active()— suspending all sessions and losing the conversation.Fix: Always write the
.clean_shutdownmarker, even on drain timeout. The interrupt signal has been sent and agents handle it cleanly on their next iteration. The existing 3-restart stuck-loop detection (#7536) already suspends truly stuck sessions as a safety net. Losing every conversation on restart is far worse than resuming an interrupted one.5. SOCKS proxy auto-detection (
gateway/platforms/base.py)_detect_macos_system_proxy()only checked HTTP/HTTPS proxy settings fromscutil --proxy, ignoring SOCKS proxies. Many users (especially behind GFW) use SOCKS5 proxies (V2rayU, Clash, Shadowrocket).Fix: Added SOCKS proxy detection (
SOCKSEnable/SOCKSProxy/SOCKSPort) returningsocks5://URLs. Added explanatory comment for whyhttp://scheme is used for HTTPS proxies (HTTP CONNECT tunnelling). Env vars (ALL_PROXY,TELEGRAM_PROXYetc.) remain the recommended way to configure any proxy scheme.Testing