Skip to content

fix(codex): app-server 探针的 stdin 写失败不再抛成进程级异常 - #2129

Merged
deepcoldy merged 1 commit into
masterfrom
fix/codex-probe-stdin-epipe
Oct 21, 2026
Merged

deepcoldy merged 1 commit into
masterfrom
fix/codex-probe-stdin-epipe

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

改了什么

src/services/codex-app-threads.ts 的 CodexAppServerProbe 给 app-server 子进程的 stdin 加上 error 监听:写入失败时,以 Codex app-server stdin error: … 结束所有挂起的请求。做法与同仓库的 codex-app-runner 一致(它早已有这条监听)。子进程照旧由各调用方在 finally 里 close() 回收(标题生成、线程列表、改名、读元数据、归档五处都有)。

测试 fixture 新增 close-stdin-after-initialize 行为:收到 initialize 后先关掉自己的 0 号 fd,再回响应并保持存活,于是 client 之后的每次写入都是 EPIPE。新增用例「rejects instead of crashing when the app-server stops reading its stdin」。

为什么(根因:生产代码问题)

#2124 排查 codex-app-threads 偶发时发现的,与那条偶发本身无关,属于 #2079 的「子进程流写入 EPIPE」一类。

CodexAppServerProbe 跑在 daemon 里(/resume 线程列表、原生标题元数据读取)和 worker 里(原生标题生成与设置、归档)。app-server 一旦停止读取 stdin(崩溃、启动即退、关掉 stdin),probe 的下一次写入就会异步报 EPIPE;stdin 上没有监听时,这个 stream error 被当作进程级异常抛出:

  • daemon 没有 uncaughtException 兜底(见 src/index-daemon.ts 顶部注释),整个 daemon 进程退出;
  • worker 的 uncaughtException 兜底会忽略 EPIPE,进程不崩,但被写坏的请求要一直挂到超时才失败。

用例怎么做到确定性复现

  • libuv 不会替 process.stdin.destroy() 关闭 0–2 号 fd,所以 fixture 里显式 closeSync(0),并且先关读端再回 initialize,client 收到响应时管道已经无人读取,不依赖时序。
  • 实测 Bun 1.4 往读端已关的管道写小数据会静默成功、不报错,只有写不进管道缓冲区的数据才报 EPIPE(Node 两种都报)。所以用例让 thread/name/set 的请求体为 256KB,两种运行时都会走到 EPIPE。

影响面

  • 只影响「app-server 不再读 stdin」这条失败路径:修复前 daemon 会被进程级异常带崩、worker 里请求挂到超时;修复后立即以 stdin 错误失败,调用方按原有的失败处理继续(标题生成返回空、列表报错等)。正常路径不变。
  • 编译态二进制同样受益(同一份源码)。不涉及其他 CLI。

验证

修复前(src 还原):vitest → Unhandled Errors: write EPIPE + 请求挂到 20s 超时;bun → (fail)
修复后:vitest test/codex-app-threads.test.ts 24 passed;bun test 24 pass

# 变异(两处全红,两种运行时都红)
去掉 stdin error 监听 / 监听但吞掉错误、不结束挂起请求

# 高负载压测:taskset -c 0-1,16 路并发,每组 48 次
vitest 48/48   bun test 48/48

bun run build → rc=0
npx vitest run --project unit test/codex-app-threads.test.ts test/test-hygiene.test.ts → 50 passed

Refs #2079

🤖 Generated with Claude Code

CodexAppServerProbe(/resume 列表、原生标题读取与设置、归档)往 app-server 的
stdin 写请求时没有监听 stream error。app-server 停止读取(崩溃、关掉 stdin)后,
下一次写入异步报 EPIPE;没有监听时作为进程级异常抛出,而 daemon 没有
uncaughtException 兜底,整个 daemon 退出。同仓库的 codex-app-runner 早已监听。

改为与 codex-app-runner 一致:stdin 报错时以该错误结束所有挂起请求;各调用方
退出时照旧 close() 回收子进程。补确定性用例:fake app-server 在回 initialize
之前关掉 0 号 fd 并保持存活;修复前 vitest 报 Unhandled EPIPE、请求挂到超时,
bun 同样失败。

Refs #2079

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

Copy link
Copy Markdown
Owner Author

已合并 PR #2129 → 8652489(squash)

门禁:首审与 Pure(pi) 复审都在 head 9d4b077 上给出可合(0 P1 / 0 P2 / 0 P3)/ 必需 CI(build、test)与 bun-test、bun-binary*、Sandbox network boundary 共 15 项首跑全绿 / 老用户无 breakchange:只改「app-server 不再读 stdin」这条失败路径,正常路径不变 / 落地树与审过的合并树一致

follow-up(复审指出,范围外、既有代码):同一形状的「daemon 里子进程 stdin 未监听 error」还有两处——src/services/external-durable-coordination-store.ts:223(BOTMUX_COORDINATION_MODE 非 disabled 时可达,写回调挡不住 unhandled 'error')与 src/adapters/backend/remote-runner-backend.ts:1116。worker 侧的 dsh-runner / minimax-acp / constrained-invocation 已由 worker 的 uncaughtException 兜底覆盖。这两处在 #2079 单独跟进。

未做:发版、部署(需另行指令)

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.49.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.

1 participant