fix: 修复了代理切换或者弱网环境下的tg平台假死情况 - #9473
Open
Baneik wants to merge 2 commits into
Open
Conversation
📄 Knowledge reviewDosu skipped reviewing this PR because your organization has used its |
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider making the polling watchdog thresholds and connect timeout (currently hardcoded to 15s and fixed counters) configurable via the Telegram adapter config so different deployment environments can tune sensitivity without code changes.
- The outbound client lifecycle now depends on several flags (
_outbound_application,_outbound_client_initialized,_replace_outbound_client_on_recovery); it would be helpful to centralize and document the state transitions to reduce the chance of subtle bugs when future changes touch polling recovery or termination.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider making the polling watchdog thresholds and connect timeout (currently hardcoded to 15s and fixed counters) configurable via the Telegram adapter config so different deployment environments can tune sensitivity without code changes.
- The outbound client lifecycle now depends on several flags (`_outbound_application`, `_outbound_client_initialized`, `_replace_outbound_client_on_recovery`); it would be helpful to centralize and document the state transitions to reduce the chance of subtle bugs when future changes touch polling recovery or termination.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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.
问题背景
Telegram polling 偶尔会在 updater 仍显示运行中的情况下停止接收更新。该问题通常发生在网络或代理切换后:
getUpdates长轮询连接进入失效状态,但没有产生足够的异常来触发现有恢复逻辑。在用户看来,就是消息发出后无响应,或者偶尔会展示bot输入中,但是并不会有响应,需要手动重启适配器后才能暂时修复 #8314
在最新构建中,实测并没有修复该问题。
提交的修改已在我本地实测运行多日,并没有再出现假死现象。
发生问题时通常表现为:
updater.running仍为True;此外,直接重建整个 Telegram Application 会关闭旧的 Bot 客户端。已经创建的
TelegramPlatformEvent仍然持有该客户端,因此恢复 polling 时可能中断正在生成或发送的回复。问题原因
现有恢复逻辑主要依赖
getUpdates抛出的NetworkError。对于失效但没有及时产生异常的长轮询连接,仅依赖错误回调无法发现故障。同时,polling 和消息发送共享同一个 Application 生命周期。重建 polling 时关闭 Application,也会关闭仍被现有事件使用的消息发送客户端。
另外,仅根据
pending_update_count是否非零判断 polling 卡死,可能会把临时消息积压误判为故障,导致正常运行的机器人被不必要地重启。修改内容
getWebhookInfo检查状态。该请求使用普通 Bot API 请求池,与getUpdates使用的长轮询请求池相互独立。pending_update_count正在下降。getUpdates请求池设置明确的 15 秒连接超时。Updater.stop()增加超时限制,避免失效的 polling 请求无限阻塞恢复流程。getUpdates错误或 pending update 停滞时,只重建 polling 客户端;恢复策略
getUpdates连续发生网络错误测试覆盖
新增了以下测试:
run()主循环触发 watchdog 恢复;Updater.stop()卡住时能够在超时后继续清理;getUpdates连续错误恢复逻辑保持正常。验证方法