Skip to content

test(session): 离线收口用例改用已退出并回收的 pid,消除 ownerPid 4242 撞上活进程的 flake - #2340

Merged
deepcoldy merged 1 commit into
masterfrom
fix/session-delete-dead-pid-flake
Oct 25, 2026
Merged

deepcoldy merged 1 commit into
masterfrom
fix/session-delete-dead-pid-flake

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

背景

外部 PR #2270 新 head(merge 4662d02b3)的必需 CI test (3/3) 偶发失败:

FAIL test/session-delete-cli.test.ts
  × closes offline once the lease has expired and no daemon is discoverable
  AssertionError: expected 1 to be +0   (test/session-delete-cli.test.ts:456)

根因

src/services/session-store.ts 的 hostOccupancyLeaseHeld() 是有意的 fail-closed:

return !!lease && (lease.leaseUntil > now || processAlive(lease.ownerPid));

TTL 过期后,只要 ownerPid 对应的进程还活着(kill(pid,0) 不返回 ESRCH),就仍然视为租约持有、拒绝离线收口。生产逻辑不动(注释已说明 pid 复用误报是接受的已知边界,start-identity 是后续 C-13)。

问题在测试:两个「租约过期且 owner 已死」用例把 ownerPid 硬编码为 4242,假定它不存在。CI runner 上恰好有 pid 4242 的进程时,用例必红。证据:

修复

改用仓库既有的 deadPid() 约定(test/session-occupancy.test.ts:84 同款):spawnSync(process.execPath, ['-e','0']) 同步等待退出并回收,该 pid 被探测时确定 ESRCH。替换两处过期租约播种(390 / 447 行);活租约用例继续显式用 process.pid,语义对照不变。

验证

  • test/session-delete-cli.test.ts 连跑 3 次 22/22 全绿;tsc --noEmit 通过;bun run build 通过。
  • 变异:把 457 行的 deadPid() 换成 process.pid(owner 活着)→ 该用例精确变红(1 failed / 21 passed),还原后恢复全绿,证明断言确实在区分 owner 存活性、修复是承重的。
  • 仅改测试文件,不动生产代码,无 breakchange。

🤖 Generated with Claude Code

hostOccupancyLeaseHeld() 在 TTL 过期后仍对活着的 ownerPid fail-closed;两个
「租约已过期、daemon 不可发现」用例硬编码 4242 假定其已死,CI runner 上恰好
存在 pid 4242 时期望 status 0 拿到 1。改用仓库既有的 deadPid() 约定
(spawnSync 立即退出的子进程,确定 ESRCH),与 session-occupancy.test.ts 一致。

Co-Authored-By: Claude Code <[email protected]>
@deepcoldy
deepcoldy merged commit 756e0fb into master Oct 25, 2026
14 checks passed
@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.

1 participant