Skip to content

test(codex): 修复 codex-app-threads 读到半写文件与冷启动截止时间导致的偶发失败 - #2124

Merged
deepcoldy merged 2 commits into
masterfrom
test/codex-app-threads-partial-file-reads
Oct 21, 2026
Merged

deepcoldy merged 2 commits into
masterfrom
test/codex-app-threads-partial-file-reads

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

改了什么

只改测试与测试 fixture,生产代码未动。

  1. pid 文件写完才算数:test/fixtures/fake-codex-app-server.mjs 写 PID 时末尾加换行;test/codex-app-threads.test.ts 的三个回收用例改用现成的 test/helpers/pid-file.ts 的 waitForPidFile(要求 ^[1-9][0-9]*\n$ 才返回)。
  2. 只解析完整的日志行:「times out, interrupts the temporary turn, and reaps the app-server」轮询请求日志时,只解析以换行结尾的行(split('\n').slice(0, -1)),不再把空文件或写了一半的行交给 JSON.parse。
  3. 归档 RPC 用例不再被冷启动卡住:Codex archive lifecycle RPC 里成功路径和路径校验用例的截止时间从 500ms/1000ms 改为 10s;只有 hang-archive 需要截止时间触发,保留 500ms。

为什么(根因)

CI 上这个文件近两周红了 10 次(扫了约 1000 次 CI 运行的失败 job),共三种形态,都是测试读取方式的问题,与生产代码无关:

形态 次数 机制
fake Codex app-server 0 was not reaped(abort / force-close 两例) 6 writeFileSync 先建空文件再写入,测试在 existsSync 为真后立刻读,读到 '',Number('') 为 0。process.kill(0, 0) 探测的是测试自己所在的进程组,永远成功,所以 2 秒后必然报「没回收」
Unexpected end of JSON input / JSON Parse error: Unexpected EOF(timeout 一例,#2118 上红的就是这条) 3 假 app-server 还在追加请求日志,''.trim().split('\n') 得到 [''],JSON.parse('') 抛错
initialize timed out after 200ms 1 10-13 的旧代码里 setCodexAppThreadName 不读 initializeTimeoutMs,master 已修,本 PR 不涉及

第 3 项是本地对照压测时发现的:未改动的 master 在 2 核压 16 路下出现 Codex app-server initialize timed out after 500ms。截止时间同时覆盖了 fixture 的 node 冷启动。这些用例断言的是 RPC 做了什么,不是快慢,所以不应被它卡住。

生产代码排查:CodexAppServerProbe 的超时、回收、force-close 路径在这几种失败里都按预期工作,失败全部发生在测试读文件这一步。另外查到 CodexAppServerProbe 往子进程 stdin 写入时没挂 error 监听,与本偶发无关,归入 #2079 的 EPIPE 一类另行处理。

影响面

  • 只涉及 test/codex-app-threads.test.ts 与 test/fixtures/fake-codex-app-server.mjs。
  • fixture 的其他使用方(codex-app-runner.integration、worker-codex-app-turn-routing.integration)读 pid 用的是 Number(readFileSync(...)),末尾换行对它们没有影响(Number('123\n') === 123)。
  • 同 fixture 的请求日志在另外两个集成测试里用 split('\n').filter(Boolean) 读,空文件的情况已经挡住;剩下「一行跨页写入时读到半行」的微秒级窗口,扫描到的 CI 记录里零次出现,本 PR 不动。

验证

# 探针:临时让 fixture 建文件后停 30ms 再写 / 把每行拆两半中间停 15ms(不提交)
修复前  PROBE_GAP_MS=30  → 3 failed:Unexpected end of JSON input、
                           fake Codex app-server 0 was not reaped (:454)、
                           force-closed fake Codex app-server 0 was not reaped (:497)
                           (与 CI 报错逐字一致)
修复后  PROBE_GAP_MS=30                → 23 passed
修复后  PROBE_GAP_MS=15 PROBE_SPLIT=1  → 23 passed

# 高负载压测:taskset -c 0-1,16 路并发,每组 48 次
对照(master dbefd6bf2) vitest  47/48(1 次 archive initialize timed out after 500ms)
修复后                   vitest  48/48
修复后                   bun test 48/48

# 变异(改坏 src/services/codex-app-threads.ts,13 处全红)
close 不发 SIGTERM / close 完全不杀 / forceClose 不发 SIGKILL / 不发 turn/interrupt /
不发 thread/unsubscribe / 忽略 abort / 标题生成后不 close / 不注册 force-close /
忽略标题截止时间 / 不回复 item/tool/call / 归档放行 home 外路径 / 去掉已归档 no-op /
归档 RPC 不设超时 —— 每个都至少有一个用例变红

npx vitest run --project unit test/codex-app-threads.test.ts   → 23 passed
npx vitest run --project unit test/test-hygiene.test.ts test/pid-file.test.ts → 43 passed

Refs #2079

🤖 Generated with Claude Code

deepcoldy and others added 2 commits October 22, 2026 04:49
测试轮询假 app-server 正在写的文件,CI 高负载时会读到写了一半的内容:

- pid 文件由 writeFileSync 先建空文件再写入,测试在这个窗口读到 '',
  Number('') 为 0;process.kill(0, 0) 探测的是测试自己的进程组,永远成功,
  于是必然报「fake Codex app-server 0 was not reaped」(abort / force-close 两例)。
- 请求日志在追加中被读取,空文件或半行交给 JSON.parse,报
  「Unexpected end of JSON input」(timeout 一例,即 #2118 上红的那条)。

改为:fixture 在 PID 末尾写换行,测试复用 helpers/pid-file 的 waitForPidFile
只认完整 PID;轮询日志时只解析以换行结尾的整行。生产代码不在这条链上,未改。

Refs #2079

Co-Authored-By: Claude Opus 5.5 <[email protected]>
成功路径与路径校验用例测的是 RPC 做了什么,不是多快;500ms/1000ms 的截止
时间同时覆盖了 fixture 冷启动,2 核压测下会报 initialize timed out。只有
hang-archive 需要截止时间触发,保留短值。

Refs #2079

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@deepcoldy
deepcoldy merged commit f3ae866 into master Oct 21, 2026
14 checks passed
@deepcoldy

Copy link
Copy Markdown
Owner Author

已合并 PR #2124 → f3ae866(squash)

门禁:首审与 Pure(pi) 复审都在 head b82b44b 上给出可合(0 P1 / 0 P2)/ 必需 CI(build、test)与 bun-test、bun-binary* 共 14 项首跑全绿 / 老用户无 breakchange:只改测试与测试 fixture,生产代码零改动 / 落地树与审过的树 patch-id 一致

follow-up:

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

@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