Skip to content

fix(model-proxy): stdin EPIPE 不再抢在退出码前定性,缺失可执行文件稳定报 native_inference_failed - #2327

Merged
deepcoldy merged 2 commits into
masterfrom
fix/native-print-epipe-exit-status
Oct 25, 2026
Merged

deepcoldy merged 2 commits into
masterfrom
fix/native-print-epipe-exit-status

Conversation

@deepcoldy

@deepcoldy deepcoldy commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Closes #2325

改了什么

collectNativePrint(services/constrained-invocation/print-process.ts)收到 stdin EPIPE 时,不再立即把失败记成 native_transport_closed,只记下「输入没送完」并照旧回收进程组;等进程退出后再按退出码统一分类:

  • 非 0 退出 → native_inference_failed(例如可执行文件不存在,退出码 127);
  • 0 退出但输入没送完 → native_transport_closed,和原来一致;
  • 中止、超出输出上限、spawn 失败等其它失败,仍然最先抛出,不变。

为什么

PR #2318 的 CI(test 3/3,job 121113171048)在 test/minimax-long-entry.test.ts「unavailable executable」这条上偶发失败:期望 native_inference_failed,实际得到 native_transport_closed。

  • 根因:CLI 是经 spawnOwnedModelProcess 的 owner-guard sh 启动的。这个 shell 在本地进程运行期间一直持有 stdin,只有它退出后才会出现 EPIPE。可执行文件不存在时,shell 以 127 退出。我们写 stdin 的时机如果晚于它退出,EPIPE 就先到;原实现收到 EPIPE 立刻定性为 native_transport_closed,退出码不再参与分类。两件事谁先谁后取决于时序,所以是偶发。
  • 确定性复现:同一个不存在的可执行文件,输入 10 字节时得到 native_inference_failed;输入 1MiB(超过管道缓冲)时,修复前必然得到 native_transport_closed。
  • 影响不止测试:InvocationService 落盘的错误分类、mcode 通道展示的错误,在进程本身失败时会被记成「传输关闭」。HTTP 层(model-proxy/server.ts)不受影响:它的已知错误码名单里没有这两个码,本来就统一回 502 native_inference_failed。
  • EPIPE 时为什么仍要回收进程组(复审 P2):第一版把 EPIPE 时的 kill 一起去掉了,理由是「shell 已经退出,kill 是空操作」,这个判断不对。CLI 退出后如果留下一个仍占着 stdout 的后代,close 要等全部 stdio 关闭才会触发;没有 kill 就会一直挂着。mcode exec 默认不限时长,没有调用方 deadline 兜底。现在 EPIPE 时照旧 SIGTERM(1 秒后 SIGKILL)整个进程组。EPIPE 只会在 owner-guard sh 退出后出现,那时退出码已经确定,回收进程组只会收掉残留的后代,不影响按退出码分类。

影响面

  • 只改 print-process.ts 一个函数。调用方是 mcode exec,以及 pi / gemini / opencode 的受限调用 runtime。
  • claude / codex runtime 不按退出码分类:进程提前退出时,它们本来就报 native_process_exited 或 native_transport_closed,不受影响,本 PR 也没有改它们。
  • 对老用户:只有「本地 CLI 非 0 退出、恰好又让 stdin 写入失败」这种情况,InvocationService 记录的错误码从 native_transport_closed 改成了 native_inference_failed。这是修正错误分类,不是行为变化。HTTP 客户端看到的错误码不变,其余路径的返回值和错误码也都没变。

验证

  • bun run build:通过。
  • 新增 test/native-print-process.test.ts:全部用真实进程,都经函数自己启动、进程组由函数自己收尾,测试里不杀任何进程。覆盖四种情况:
    • 不存在的可执行文件,1MiB 输入(必然 EPIPE)和小输入两种情况都报 native_inference_failed;
    • 关掉 stdin 后非 0 退出 → native_inference_failed;
    • 关掉 stdin 后 0 退出 → native_transport_closed;
    • 读完输入的正常进程照常返回输出;
    • CLI 关掉 stdin 退出、但留下一个占着 stdout 30 秒的后代:按退出码报错并在 10 秒内返回,不会挂住。
  • vitest 跑 native-print-process / minimax-long-entry / constrained-invocation 共 22 条,全过;bun test 跑新文件 5 条,全过。
  • 变异(每枪限时,从内存还原,只杀自己 spawn 的进程组),5 枪全红:
编号 改动 结果
F1 恢复「EPIPE 立即定性为传输关闭」 红(2 例)
F2 去掉「0 退出但输入没送完」的判定 红
F3 先看 EPIPE 再看退出码 红
K1 EPIPE 时不回收进程组 红(后代占着 stdout 的用例挂住超时)
F1b EPIPE 时立即定性为传输关闭(保留回收) 红

合入后,#2318 同步 master 让 CI 跑在带修复的树上再合。

🤖 Generated with Claude Code

…failed

collectNativePrint 经 owner-guard sh 启动 CLI,该 shell 在本地进程运行期间一直持有 stdin,
EPIPE 只会在它退出后出现;原实现收到 EPIPE 立即记 native_transport_closed,
和退出码 127 的先后取决于时序(CI 偶发)。改为退出后统一按退出码分类:非 0 → native_inference_failed,
0 退出但输入没送完 → native_transport_closed。

Closes #2325

Co-Authored-By: Claude Opus 5.5 <[email protected]>
复审 P2:上一提交去掉了 EPIPE 时的 kill,当本地 CLI 退出但留下仍持有 stdout 的后代、调用方又没有 deadline(mcode exec 默认不限时长)时,
close 永远不来、调用一直挂着。EPIPE 只会在 owner-guard sh 退出后出现,此时退出码已定,回收进程组只收掉残留后代,不影响按退出码定性。

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@deepcoldy

Copy link
Copy Markdown
Owner Author

已 squash 合入 master(193e6008b),落地树与评审树一致,master CI 已开跑。

评审中确认两处改前改后同形、master 本来就有的健壮性问题,本 PR 未引入也未覆盖,已另开 #2347 跟进,不影响本次合入:

  1. stdin error 事件若漏判,exit 0 且输入未送达时会落到 return output 误报成功;建议退出后结合流终态(!child.stdin.writableFinished || child.stdin.errored !== null)兜底。
  2. 后台后代同时继承 stdin 和 stdout 时 child close 永不到(Node 下挂起,Bun 不挂);本 PR 只覆盖了 EPIPE 可达的那一半,建议定性改用 exit、stdout 收尾做有界 drain,两种形态一起消掉。
  3. EPIPE handler 上方注释一处英文语法破句,顺手改即可。

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.52.0

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.

test(minimax): 可执行文件缺失时 stdin EPIPE 抢在退出码之前,失败被误报为 native_transport_closed(CI 偶发)

1 participant