Open
fix(gateway): 可选渠道启动失败后关停仍以退出码 0 结束 #490
fix(gateway): 可选渠道启动失败后关停仍以退出码 0 结束 #490
Open
yechenzhenAdd on 1 day ago
yechenzhen
yechenzhenCollaborators
1 day ago

Paired: GitHub #707 ↔ GitCode !490

/kind bug


What does this PR do / why do we need it:

可选渠道的 start() 因凭据无效或平台不可达抛错之后,Gateway 仍在运行,但之后每一次关停或重启都会以崩溃码 70 退出:services.stop()、遥测关停和 restart_gateway() 被跳过。完整分析见 #265,根因简述如下。

OptionalChannels.stop_channels() 对每个渠道任务都是 cancel() 后 await,并且只兜 CancelledError。任务早已因 start() 抛错而结束时,cancel() 是空操作,await 会把存着的异常重新抛出。这个调用位于 run_single 的 finally 中,异常一旦抛出,后面的关停步骤全部被跳过,最终被 crash handler 记成未处理异常。

本 PR 新增 _await_channel_task_stopped(),把 stop_channels() 中 11 处相同的等待收敛到它,并采取与重配置路径 _stop_channel() 相同的策略:启动失败的异常在发生时已经记录,关停时只打一条 warning 并继续。

保持不变的部分:停止顺序、各渠道 stop() 的调用,以及 CancelledError 的处理(#131 所说的"吞掉本协程的取消"是另一个议题,本 PR 没有扩大范围)。

修复前后对比(macOS,默认配置启用 telegram 且 token 无效;修复前为 develop 273533f91,修复后为本 PR):

场景 修复前 修复后
进程收到 SIGTERM 退出码 70;Cron、派发循环未停止,无 [App] Gateway stopped 退出码 0,各步骤恢复
受 jiuwenswarm.app 监督,直接停止 Gateway 子进程 判为崩溃并自动拉起 正常收尾,退出码 0
自请求重启,运行约 20 秒 退出码 70,整个服务栈收尾退出 退出码 75,被拉起
自请求重启,运行约 45 秒 退出码 70,被拉起但记为崩溃 退出码 75,被拉起
独立运行(无 supervisor),自请求重启 进程退出,无人拉起 os.execv 原地重启

对外可见的行为变化只有两处:

  • 关停时现在会调用启动失败渠道的 stop()。此前它被异常跳过;重配置路径本来就会调用它。
  • 日志多一条 warning:[App] ignored exception while waiting for <渠道> channel task: ...。

Which issue(s) this PR fixes:
Fixes #265


Test Plan and Test result:What scenarios were tested, and what were the verification results(Function, performance, reliability, etc.):

  • 单元测试:develop 273533f91 上全量 UT 7039 通过、6 跳过;本 PR 上全量 UT 7051 通过、6 跳过,新增 12 项,无失败。新增的 12 项在未修复的代码上全部失败。
    • 覆盖 telegram / discord / slack / whatsapp / wecom / wechat 单实例,飞书与小艺多应用,企业飞书,失败渠道之后的渠道,SSH 密钥签发器的清理,以及 run_single 级别的关停顺序(services.stop() 与遥测关停仍然执行)。
  • 真实进程实测:使用真实 Gateway 进程,受监督场景使用 jiuwenswarm.app 的 process_supervision.supervise(),结果见上表。启动失败的形态覆盖了 token 被拒、连接超时和连接错误。
  • 与最新 develop 合并:与 8fc3c8774 合并无冲突,合并结果上生命周期、装配与启动相关测试 107 项通过。
  • 未覆盖:只在 macOS 上验证。真实进程实测只用了 telegram 和 discord(slack 用假凭据未能让 start() 抛错),其余渠道由单测中的假渠道覆盖。
  • 远端 CI 以本 PR 对应提交的流水线为准,本机验证不代替远端 CI。

Self-checklist:(请自检,在[ ]内打上x,我们将检视你的完成情况,否则会导致pr无法合入)

Linked Closing Issues:

likedislike
Merge blocked
yechenzhenyechenzhenCollaborators
1 day ago Associated issue: 【Bug】writer 收尾吞掉调用方的取消信号(except (CancelledError, Exception): pass),[Bug]: 可选渠道启动失败后,Gateway 关停或重启会以崩溃码 70 退出
atomgit-bot
atomgit-bot
1 day ago Comments:

🤖 AI Code Review

⚠️ 审查未能完成

本次代码审查未能完成,可能是临时性故障。

你可以在评论区输入 /ai summary 重新发起审查。若多次失败,请联系管理员并附上下方追踪 ID。

追踪 ID: task-567164

likedislike
Incorrect?
atomgit-bot
atomgit-bot
1 day ago Comments:

🤖 AI Code Review

⚠️ 审查未能完成

本次代码审查未能完成,可能是临时性故障。

你可以在评论区输入 /ai review 重新发起审查。若多次失败,请联系管理员并附上下方追踪 ID。

追踪 ID: task-567165

likedislike
Incorrect?
atomgit-bot
atomgit-bot
1 day ago Comments:

AtomGit AI 助手使用指南

在 PR 评论中使用命令即可触发。支持在普通评论和代码行评论中使用。

功能 命令 说明
代码审查 /ai review 检查代码质量、潜在问题、安全风险
PR 摘要 /ai summary 生成 PR 变更内容的结构化摘要
代码解释 /ai explain 解释代码变更的逻辑和意图
自由提问 @atomgit-bot <问题> 询问关于本 PR 的任何问题
帮助 /ai help 显示此帮助信息

免责声明

AI 助手可能存在误判,请结合自身判断。可以对评论点 👍 或 👎 帮助我们改进。

likedislike
Incorrect?
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: sig/sig-agent-gateway
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:

欢迎来到 openJiuwen 社区

Hey @yechenzhen , 感谢你对社区的贡献.

机器人使用手册

有关指令的使用,可以点击 此处 查看详情。开发人员可以在每个PR或Issue下方评论特定指令来触发机器人任务。

联系指引

有疑问可以联系 SIG: sig-agent-gateway ,
维护者是: @YiyangShao, @mingo007 ,
审核者是: @d-0771, @gcw_hx9prznZ, @yechenzhen, @yukuanj .

likedislike
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: openJiuwen-cla/yes
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:

CLA 签署成功

yechenzhen, 感谢您的代码贡献。 所有的代码贡献者都已完成了 CLA 签署。 👍

likedislike
openjiuwen-syncopenjiuwen-syncCollaborators
1 day ago Modify description of pull request
openjiuwen-syncopenjiuwen-syncCollaborators
1 day ago Modify description of pull request
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:

The pipeline(pipeline number:490) is running. Please wait a moment...

likedislike
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: ci-running
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Removed label: ci-running
openJiuwen-bot
openJiuwen-botCollaborators
1 day ago Comments:
任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 ✅SUCCESS 点此跳转
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 ✅SUCCESS N/A
pr size检查 ✅SUCCESS N/A
likedislike
openJiuwen-botopenJiuwen-botCollaborators
1 day ago Added label: ci-successful