Skip to content

test(core): pm2 reaper 用例按完整 pid 内容就绪读取,修 bun-test 空 pidfile 竞态 - #2561

Merged
deepcoldy merged 1 commit into
masterfrom
fix/pm2-reaper-pidfile-readiness
Oct 30, 2026
Merged

deepcoldy merged 1 commit into
masterfrom
fix/pm2-reaper-pidfile-readiness

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

问题

test/legacy-pm2-reaper.test.ts 的「stops the God BEFORE its daemons, so nothing gets respawned」用例在 CI bun-test 下偶发 60s 超时:

error: pid NaN cmdline never contained "/fake/dist/index-daemon.js"
  at waitForPidCmdline (test/helpers/proc-ready.ts:56)
  at test/legacy-pm2-reaper.test.ts:733

在 #2514 首跑(run 40937210302 attempt 1)出现,attempt 2 同 SHA 转绿。按合码门禁「偶发失败不靠同 SHA 重跑放行」,先根治本 flake,#2514 同步含此修复的 master 后再合。

根因

用例只轮询 existsSync(pidFile),文件一存在就读一次 parseInt:

while (… && !existsSync(pidFile)) spinMs(50);
const firstChild = parseInt(readFileSync(pidFile, 'utf-8').trim(), 10);

写侧 god 脚本的 fs.writeFileSync 对独立读进程是 open(O_CREAT)→write→close 三步:文件在 open 后就 existsSync=true,但数字要等 write 才落盘。CI 负载下读侧每 50ms 轮询,能踩进 open→write 之间的空窗读到空字符串,parseInt('') = NaN,再喂给 waitForPidCmdline(NaN) 必超时。等「路径存在」不等于等「内容就绪」,与 #2509 修的 session-env create-then-write 空窗同族。

改动(纯测试侧,3 文件)

  • test/helpers/proc-ready.ts:新增 readPidFileWhenReady,按「当前内容是完整正整数」轮询就绪,空/部分/被替换都重试到超时,错误信息带 last content;注释明确只保证「完整正整数」、不保证是写侧最终值。
  • test/legacy-pm2-reaper.test.ts:两处 pidfile 读取(firstChild / lastChild)都改走该 helper,不只修触发点。
  • test/proc-ready-helper.test.ts:新增单测,覆盖「已完整」「open 空窗后写入」「永不合法→超时」三态,vitest/bun 双引擎 3/3。

生产侧安全,不动生产代码:src/core/legacy-pm2-reaper.ts:104 同样 parseInt(readFileSync()),但下一行 if (!Number.isSafeInteger(pid) || pid <= 1) continue 直接挡掉 NaN,其后还有 isAlive(pid) 与 cmdline pid-reuse 两重校验,NaN 永不被 signal。

验证

  • bun run build、tsc --noEmit、test-hygiene 通过
  • vitest:legacy-pm2-reaper(26) + 新 helper(3) + hygiene(26) = 55/55
  • 确定性探针(独立进程 open 空文件 → 300ms 后写 pid):旧一次性读法必得 NaN,新 helper 拿到 pid(one-shot=NaN / readiness=424242 / PROBE PASS)
  • 变异:helper 改回一次性读取 → 新单测 3 中 2 红(空窗用例 + 超时用例)
  • 目标用例 isolate-run 全新环境 bun 连跑 12/12 绿

一个无关的已知环境现象(不在本 PR 范围)

同文件「treats a REAL orphaned socket (listener SIGKILLed)」在本机 bun 1.4.4 下(含干净 master、isolate 命名空间)确定性 5s 超时,但 vitest 本机绿、CI bun(钉版 1.4.3)1813/1813 绿。这是本机 bun 版本/宿主环境与该真实 SIGKILL-listener 用例不兼容,非本 PR 引入,干净 master 同样复现。

影响面

只动测试 helper 与一个测试文件,零 src/ 改动,不影响任何 CLI/后端/会话类型,无需部署。

legacy-pm2-reaper 的「stops the God BEFORE its daemons」用例只轮询
existsSync(pidFile),存在就读一次 parseInt。写侧 fs.writeFileSync 对观察者是
open(O_CREAT)→write→close:文件在 open 后即 existsSync=true,但数字要等 write
才落盘。CI bun-test 负载下读侧(每 50ms 独立轮询)会踩进 open→write 空窗读到
空字符串,parseInt('')=NaN,喂给 waitForPidCmdline(NaN) 必超时(#2514 首跑
run 40937210302 attempt1 即此,pid NaN cmdline never contained)。

- test/helpers/proc-ready.ts 新增 readPidFileWhenReady:按「内容是完整正整数」
  轮询就绪而非「路径存在」,空/异常重试到超时并带 last content 诊断;只保证
  当前是完整正整数、不保证是写侧最终值
- 用例两处 pidfile 读取(firstChild / lastChild)都改走该 helper
- 新增 proc-ready-helper 单测:已完整、空窗后写入、永不合法三态

纯测试侧改动。生产 src/core/legacy-pm2-reaper.ts:104 同样 parseInt 但下一行
Number.isSafeInteger(pid)||pid<=1 即 continue(NaN 被挡),后接 isAlive 与
cmdline pid-reuse 双校验,生产行为安全、不在本次范围。

确定性探针(open 空文件→300ms 后写):旧一次性读法必得 NaN,新 helper 拿到
pid。变异:helper 改回一次性读取,新单测 3 中 2 红。

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

Copy link
Copy Markdown
Owner Author

复审:代码 0 P0 / 0 P1 / 0 P2(待 CI bun-test 绿后给最终可合表态)

在 head 541302a6f 上独立复核,不是阅读结论。

逐项确认你吸收的三点

  1. 生产侧澄清:我核了 src/core/legacy-pm2-reaper.ts:104-108,parseInt 之后紧接 if (!Number.isSafeInteger(pid) || pid <= 1) continue;,NaN 直接被挡,后接 isAlive + looksLikeLegacyBotmuxDaemon 双校验。纯测试侧修复成立,本 PR 零 src/ 改动,确认。
  2. 注释缺口已补:readPidFileWhenReady 的 docblock 现在明写「只保证当前是完整正整数、不保证是写侧最终值」,并列了 4→4242 的例子。745 处 lastChild 也注明了「God 已死、写侧静止后读取,取的是 settled 值」——与 helper 的语义边界对齐,不会再被误当成「写入已结算」判据。
  3. import 无残留:existsSync / readFileSync / parseInt 在 legacy-pm2-reaper.test.ts 的其余用例仍在用(grep 逐条确认),本 PR 移除的两处不产生未使用符号。

我的独立取证

helper 行为(vitest + bun 双引擎)

test/proc-ready-helper.test.ts  3/3 通过(两个引擎)
test/legacy-pm2-reaper.test.ts 26/26
test/test-hygiene.test.ts      26/26
合计 55/55;tsc --noEmit exit 0

变异复验

  • helper 改回一次性 readFileSync → proc-ready-helper.test.ts 2/3 红(waits through the open→write window、throws a descriptive error 都咬住)。与你说的「3 中 2 红」一致。
  • 目标用例修复态:isolate 全新环境 bun 连跑 14 轮,stops the God BEFORE its daemons 14/14 pass,未再出现 pid NaN。

关于 REAL orphaned socket 那条红——我做了独立对照,你的归因正确

这条在本机我这边也是每轮都红(this test timed out after 5000ms),我起初怀疑是本 PR 引入。做了干净对照:

树 轮数 treats a REAL orphaned socket
干净 origin/master (e3b5586) 6 6/6 红
本 PR head 541302a6f 14 14/14 红

干净 master 上完全相同的失败,确认是既有环境因素(本机 bun 1.4.4 vs CI 钉的 1.4.3、真实 SIGKILL-listener 在本机命名空间下的行为),与本 PR 无关。已写进 PR 描述的「已知环境现象」是恰当的,建议保留该段。

一个可选的后续(不阻塞本 PR)

test/legacy-pm2-reaper.test.ts:144-153 的 realOrphanGodSocket 也是「轮询 existsSync 后立刻 SIGKILL」,与本次修的形态同族(等路径存在而非等状态就绪)。它靠 spinMs(150) 兜底,目前只在特定环境下红。若要一并收,可以另开小 PR;不建议塞进这个已经聚焦的修复里。

待办

等 CI 首跑(当前 QUEUED)。我要求的是 attempt=1 首跑 bun-test 绿,不重跑——这正是本 PR 要修的东西,如果首跑还红就先别合并、拿日志回来一起看。

@deepcoldy

Copy link
Copy Markdown
Owner Author

新 head 表态:可合(0 P0 / 0 P1 / 0 P2)

head OID:541302a6f(base e3b5586,MERGEABLE)。

G4 —— 这一次是首跑绿,不是重跑

  • CI run 40970543572,run_attempt = 1(API 直核,非 UI 目测)。
  • bun-test:completed / success。
  • 其余实质 job 全 success:build、test (1/4)、test (2/4)、test (3/4)、test (4/4)、bun-binary、bun-binary-musl、bun-binary-darwin。
  • 仅剩汇总 job test 排队(只聚合结果,不跑测试)。
  • 本 PR 修的就是 bun-test 腿上的空 pidfile 竞态,它第一次跑就绿 —— 正是该有的验证形态。

代码审(独立复核)

  • 纯测试侧:零 src/ 改动。生产侧 legacy-pm2-reaper.ts:104-108 的 Number.isSafeInteger(pid) || pid <= 1 → continue + isAlive + cmdline pid-reuse 双校验已核实,NaN 到不了副作用。
  • 两处读法都收:730(firstChild)与 745(lastChild)都换 readPidFileWhenReady;后者注释注明「God 已死、写侧静止后读 settled 值」。
  • helper 语义边界写清:「只保证当前是完整正整数、不保证写侧最终值」(4→4242 会提前返回)——我上轮提的补充点已落实。
  • imports 无残留:existsSync/readFileSync/parseInt 在该文件其余用例仍在用,逐条 grep 确认。

本地取证

proc-ready-helper.test.ts  3/3   (vitest + bun 双引擎)
legacy-pm2-reaper.test.ts 26/26
test-hygiene.test.ts      26/26
合计 55/55;tsc --noEmit exit 0

变异:helper 改回一次性 readFileSync → 新单测 2/3 红(与你所述一致)。目标用例修复态 isolate 全新环境 bun 连跑 14 轮 14/14 pass,不再出现 pid NaN。

REAL orphaned socket 归因——独立对照确认你的判断

树 轮数 结果
干净 origin/master (e3b5586) 6 6/6 红(同一条)
本 PR head 541302a6f 14 14/14 红

干净 master 上完全相同的失败 → 既有本机环境因素(本机 bun 1.4.4 vs CI 1.4.3、真实 SIGKILL-listener 命名空间行为),与本 PR 无关。PR 描述那段「已知环境现象」建议原样保留。

一条不阻塞的后续

legacy-pm2-reaper.test.ts:144-153 的 realOrphanGodSocket 同样是「轮询 existsSync 后立刻 SIGKILL」,与本次同族,靠 spinMs(150) 兜底。建议另开小 PR 收,不塞进这个已聚焦的修复。

结论

可合。squash 落地后把 #2514 rebase 到含 #2561 的 master,走新 head 首跑(不重跑),我在新 head 上表增量态。

@deepcoldy
deepcoldy merged commit f7c9508 into master Oct 30, 2026
15 checks passed
@deepcoldy

Copy link
Copy Markdown
Owner Author

后续跟进(非阻断,记录一个测试覆盖小缺口):

test/helpers/proc-ready.ts 的 readPidFileWhenReady 里 if (pid > 0) 这一判断目前没有直接用例覆盖——做变异把这行摘掉后,现有 3 条用例在 vitest 与 bun test 下仍然全绿。

风险评估:能越过 /^\d+$/ 到达该判断的输入只有字面量 "0"(负数、小数、非数字已被正则挡掉),而返回 0 之后下游 waitForPidCmdline(0) / alive(0) 还会再挡,不会造成错误 kill;实际影响只是把「读到无效值」变成一次 cmdline 等待超时,诊断信息变差。生产侧 src/core/legacy-pm2-reaper.ts 的同名读法靠 pid <= 1 守卫,不受影响。

建议后续补两条用例闭环:

  • 文件持续是 "0" → 超时抛 never held a valid pid;
  • 先写 "0"、稍后被真实 pid 覆盖 → helper 继续轮询并拿到真实 pid(这条在摘掉 pid > 0 后必定变红)。

另一个 nit(可不改):helper 用的是 Number.parseInt(raw, 10),没有像生产侧那样加 Number.isSafeInteger,超长数字串会得到 1e21;测试夹具基本不可达,加上更整齐。

deepcoldy pushed a commit that referenced this pull request Oct 30, 2026
pi 在 924e843 复审可合后,master 又进 #2553/#2467/#2561。与本 PR 同文件 2 个:
i18n.ts(#2553 仅追加 groups.* 105 处,本 PR 加 botDefaults/sessions/skills.*,命名空间不重叠)、
worker.ts(#2467 spawnCli@23947/24129,本 PR provisionIsolatedBotHome@2388,相距 2.1 万行)。
merge-tree 零冲突,同步以在最新主干跑 CI。
deepcoldy pushed a commit to uestcwangci/botmux that referenced this pull request Oct 30, 2026
…ity-recovery-2473-final

四次同步 master(0345cc5f1..eb43116,14 提交,含 deepcoldy#2514/deepcoldy#2534/deepcoldy#2535/deepcoldy#2541/
deepcoldy#2467/deepcoldy#2550/deepcoldy#2553/deepcoldy#2554/deepcoldy#2557/deepcoldy#2561/deepcoldy#2562 等)。与本 PR 仅 worker-pool.ts /
worker.ts 文件交集,git 自动合并干净、无冲突标记;PR 关键接线(codexApp
TurnFailureCode 四处、!turn.error 守卫、pool 恢复落账、worker 签名 final
失败字段、Pi boot 20s 预算)逐处 grep 仍在原位。合并树 build 过、
codex 集成/容量/engine/deployment/schedule 共 276 项 vitest 绿、
容量+Pi argv 46 项 bun 绿。

Co-Authored-By: Claude Code <[email protected]>
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

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