Skip to content

fix(fleet-supervisor): 清理系统复用的历史 PID 并固定非 Linux 持久化身份时区 - #3942

Merged
deepcoldy merged 3 commits into
deepcoldy:masterfrom
big-omega:fix/supervisor-recycled-pid-recovery
Nov 28, 2026
Merged

deepcoldy merged 3 commits into
deepcoldy:masterfrom
big-omega:fix/supervisor-recycled-pid-recovery

Conversation

@big-omega

@big-omega big-omega commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

背景与问题

宿主机重启后,如果 plugin-supervisor(或主 fleet)持久化状态文件里的历史 pid 恰好被操作系统分配给了其他无关进程,服务会被永久阻塞无法拉起:

  1. plugin-supervisor 启动先调用 supervisor.start([]),状态文件里的历史插件行因不在本次配置列表中而走 retireOnly 路径进入 retireUnowned();
  2. inspectFleetProcess() 对比 processStart 发现启动身份不匹配后返回 { status: 'stale' },但 retireUnowned() 对 retireOnly && inspection.status === 'stale' && runtime.pidExists(proc.pid) 一律抛错(cannot prove removed ... is a botmux daemon),未区分「processStart 已证伪的 PID 复用」与「身份一致但命令无法证明的活进程」;
  3. 该旧行以 status: 'launching' 和旧 pid 残留于 state.json,随后 upsertExternal() → spawnBot() 仅检查 runtime.pidExists(p.pid),触发 blocked by an unowned live process,导致插件或已移除成员重启后永久失联。

此外,非 Linux 平台原先通过不带 TZ 的 ps -o lstart= 生成持久化身份,宿主时区变化会导致同一活进程身份字符串漂移,若直接将任意字符串不等视为 PID 复用,会在时区变化后误清活插件行并重复拉起。

修改内容

  • 时区稳定的持久化身份(src/utils/process-identity.ts):
    • 非 Linux / 非 Windows 平台的 readDurableProcessIdentity() 固定使用 TZ=UTC0 执行 ps -o lstart= 并加 utc: 前缀;Linux(boot_id:starttime)与 Windows(UTC ticks)保持原有格式。
    • 新增 isTimezoneStableProcessIdentity()(仅放行 utc:、Linux boot_id:starttime、Windows 纯数字 UTC ticks),以及 durableProcessIdentityMatches() 为升级前残留的未加前缀本地时区 lstart 行保留滚动升级兼容匹配(接入 fleet-process-identity.ts、index-plugin-supervisor.ts、supervisor-client.ts)。
  • PID 复用安全清理(src/core/fleet-supervisor.ts):
    • 新增 isRecycledPid(proc, runtime):仅当记录的 processStart 与当前 readIdentity(pid) 均满足时区稳定格式且两者不等时,才判定为系统复用 PID;未标记旧 lstart、缺失或不可读身份一律 fail-closed 返回 false。
    • 在 retireUnowned()、start() filter、spawnBot() 与 stopAll() 排除已证实复用的 PID,全程不向当前无关进程发送信号。
  • 测试与安全清理(test/):
    • test/plugin-supervisor.integration.test.ts:共享 afterEach 改为仅对通过身份 attest 且非当前测试进程的 PID 发送信号;新增用例改用独立 spawn 的测试子进程与 supervisorPid: 0,避免断言失败时误杀测试 worker 或无关进程。
    • 补充单点行为回归用例:时区稳定身份与未标记旧 lstart 时区漂移 fail-closed 探针、start() filter(注入 retireUnowned 完成写 ENOSPC)、直接 upsertExternal()(spawnBot 路径)以及 stopAll() 跨停机清理。

影响范围评估

  • 共用路径:src/utils/process-identity.ts、src/core/fleet-process-identity.ts、src/core/fleet-supervisor.ts、src/index-plugin-supervisor.ts、src/core/plugins/supervisor-client.ts。
  • 跨平台:macOS/POSIX 新写入 utc: 前缀消除时区漂移,并通过 durableProcessIdentityMatches 保持与升级前同机活进程的滚动升级兼容;Linux/Windows 保持既有持久化格式;Linux hidepid 不可读场景保持 fail-closed。
  • Fail-closed 边界不变:无 processStart 的 legacy 行、未标记旧 lstart 发生时区漂移的行、readIdentity() 不可读行、processStart 一致但命令无法证明的活非 daemon 行均保持原有拦截语义,不发信号、不清行、不重复拉起。

测试验证

  • node scripts/tsc.mjs -p tsconfig.typecheck.json:通过(0 error)
  • npx vitest run test/fleet-process-identity.test.ts test/fleet-supervisor-recovery-failure.test.ts test/fleet-supervisor.integration.test.ts test/plugin-supervisor-state.test.ts test/plugin-supervisor-shutdown.test.ts test/plugin-supervisor.integration.test.ts test/test-hygiene.test.ts test/file-lock.test.ts:8 个测试文件 / 192 项测试全部通过
  • 单点变异复验(6/6 咬红且无 worker 崩溃):分别摘除 isTimezoneStableProcessIdentity 守卫、retireUnowned 守卫、start() filter 守卫、spawnBot() 守卫、stopAll() 守卫,以及整体失效 isRecycledPid 验证 plugin-supervisor.integration.test.ts teardown 安全性,对应单点用例全部稳定失败且未出现测试 worker 被杀
  • 本地未编译单文件二进制,跨平台与完整构建待 CI 校验

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复审结论:请求修改(2 个 P2,1 个 P3)

复审对象:1a140653759aca114c24ba5cce4e1f2e9b728840。原“0 阻断、可合”结论不成立;下面两项 P2 均已用真实组件探针复现。

P2 — macOS 时区变化会把同一活插件误判成 PID 复用,并重复拉起

位置:src/core/fleet-supervisor.ts:135-142、src/utils/process-identity.ts:74-85。

isRecycledPid 将“持久化 processStart 与当前身份字符串不同”直接当成原进程已死、PID 被复用的证据。但非 Linux 平台的身份来自 ps -o lstart=;这里只固定了 LANG,没有固定 TZ。lstart 是调用时按本地时区渲染的字符串,不是时区稳定的出生时间值,因此同一活进程在系统时区变化前后也会得到不同身份。

在插件 supervisor 被 SIGKILL、系统时区随后变化、下一次 startPluginServices 执行 start([]) 的场景中:

  1. retireOnly 检查看到 stale,新 !isRecycledPid(...) 条件不再 fail-closed;
  2. 状态行被清除,但原插件仍存活;
  3. upsertExternal 拉起第二个插件。

探针证据:

  • 同一 PID 的 ps -o lstart= 实测在 TZ=UTC0 下为 Fri Nov 27 19:32:34 2026,在 TZ=Asia/Shanghai 下为 Sat Nov 28 03:32:34 2026。
  • 临时 Vitest 探针使用真实 FleetSupervisor、真实外部子进程和可控 timer,仅把该子进程的 readIdentity 从不同时区的旧 lstart 字符串切到新字符串。当前 head 上观察到:start([]) 清行、upsertExternal 创建另一个 online PID、原 PID 仍存活,且原 PID 没有收到非 0 信号。
  • 将 isRecycledPid 整体失效以模拟旧行为后,同一探针立即咬红:原行保持 launching,未进入复制路径。

建议让新写入的 macOS 身份使用固定 TZ=UTC0(最好带格式版本),对未标记的旧 lstart 身份保守迁移;或者先只允许时区稳定的身份格式进入 recycled 清理,旧格式继续 fail-closed。仅给新读取加固定 TZ 而不处理存量字符串,仍会把升级前的同一进程误判为复用。

P2 — 新插件测试失败时会 SIGKILL 测试进程,还可能误伤伪造 supervisor PID 的占用者

位置:test/plugin-supervisor.integration.test.ts:43-51、:436-450。

新用例把 pid: process.pid 和 supervisorPid: 999_999 写入状态文件;共享 afterEach 随后对状态中的活 PID 直接 SIGTERM / SIGKILL,没有校验它是否属于测试 fixture。一旦启动失败、状态行未被替换,测试清理就会杀掉 Vitest worker,而不是只给出普通红色断言;如果 999999 恰好被其他进程占用,也可能向无关进程发 SIGTERM。

**探针证据:**在预先确认 999999 不存在后,将 isRecycledPid 整体失效,只运行新增插件用例。输出先出现 expected 'failed' to be 'started',随后出现 Worker exited unexpectedly 和 vitest-pool: Worker forks emitted error,复现了失败清理杀死 worker 的路径。

建议改用专用 spawn 的无关子进程作为 recycled PID 占用者,并在 finally 中只清理这个 fixture-owned 子进程;supervisorPid 使用 0 或已验证不存在的 PID。共享 teardown 不应信任状态文件里的任意 PID,至少应限定到本测试实际创建且身份已 attest 的进程。

P3 — 三个纵深守卫仍没有单点行为探针

位置:test/fleet-supervisor.integration.test.ts:276-302、test/plugin-supervisor.integration.test.ts:432-455。

两个新增端到端场景都会让 start([]) 的 retireUnowned 先清行,所以它们没有独立钉住:

  • start() 的同步 filter / 第二循环(src/core/fleet-supervisor.ts:253-269);
  • spawnBot() 对半写残留行的过滤(:540-555);
  • stopAll() 的跨停机清理(:802-817)。

现有变异记录显示,分别摘除这些单点条件时新增测试仍全绿;它们目前主要靠 retireUnowned 的端到端结果互相掩盖。建议补直接 start、admission 写成功但 completion 写失败、以及跨停机保留的用例,并对每个新增条件逐点做变异复验。可复用 test/fleet-supervisor-recovery-failure.test.ts 的故障注入方式。

已执行验证

  • bun run build:通过。
  • npx vitest run test/fleet-process-identity.test.ts test/fleet-supervisor-recovery-failure.test.ts test/fleet-supervisor.integration.test.ts test/plugin-supervisor-state.test.ts test/plugin-supervisor-shutdown.test.ts test/plugin-supervisor.integration.test.ts test/test-hygiene.test.ts:7 文件、153 项全部通过。
  • 上述两组新探针及 helper 失效对照:结果如各条目所述;临时探针与源码变异均已清理,工作树恢复干净。
  • GitHub CI 在发布本 review 时尚有 Bun / binary 分片未完成;这不影响上述已复现问题的结论。

@big-omega
big-omega force-pushed the fix/supervisor-recycled-pid-recovery branch from 1a14065 to b0ce5ab Compare November 27, 2026 19:51
@big-omega big-omega changed the title fix(fleet-supervisor): 清理系统复用的历史 PID 避免重启后阻塞拉起 fix(fleet-supervisor): 清理系统复用的历史 PID 并固定非 Linux 持久化身份时区 Nov 27, 2026
@deepcoldy

Copy link
Copy Markdown
Owner

修复已推送:be8e416102dfa9dccb731a8cf4aed769e7d11cce

基于同期更新的 b0ce5abbfa 整合,未覆盖远端提交。

修复内容

  • P2-1:fleet 专用 durable 身份在 POSIX 上使用 TZ=UTC0 与 lstart-utc-v1: 版本前缀;读取存量裸 lstart 时仍使用 legacy renderer,避免升级后同一活进程被新格式误判。只有记录值和当前值都是时区稳定格式且不相等时,才允许按 recycled PID 清理;legacy 记录继续保持 fail-closed。Linux / Windows 输出不变,其他 feedback / session / retention durable 协议不改格式。
  • P2-2:插件回归用例改用专用 spawn 子进程和 supervisorPid: 0;测试 finally 只清理该 fixture。共享 teardown 增加自身 PID 排除、supervisor/member exact 身份核验和 attested signal,不再盲信状态文件里的任意 PID。
  • P3:补齐 start filter 的真实 completion 写失败故障注入、第二循环与 removed/stale 判定的直接行为用例、spawnBot 半写残留和 stopAll 跨停机用例;格式混读也有独立对照。

实际验证

  • bun run build:通过。
  • 相关 10 文件:188/188 通过:fleet-durable-process-identity、fleet-process-identity、fleet-supervisor-recovery-failure、fleet-supervisor.integration、plugin-supervisor-state、plugin-supervisor-shutdown、plugin-supervisor.integration、session-worker-pid-identity、proc-stat-identity-equivalence、test-hygiene。
  • resource-monitor 邻近路径 6 文件:52 通过、3 项按平台跳过。
  • 定向变异:retire、start filter、start 第二循环、spawnBot、stopAll、legacy 记录格式与当前格式闸门均被对应测试咬红;真实 completion 写失败用例在摘除 filter 后由“删行”退化为残留 stopped/pid=0,按预期失败。
  • helper 整体失效后单跑新插件用例:只出现普通 expected 'failed' to be 'started',不再出现 Worker exited unexpectedly。
  • 全量 bun run test 本地运行 10 分钟超时后停止,不计为通过证据;其遗留 fixture 进程和私有 tmux server 已清理。新 head 的完整 CI 矩阵待运行。

提交前对抗复核

覆盖了人工中途干预、admission/completion 半写入、重启对账、退休与等待窗口交叠。当前新增的 UTC 格式及 retireOnly 路径未发现新的阻断。

另用真实 FleetSupervisor、真实子进程和可控 timer 复现两类 legacy 既有风险:配置内成员在时区漂移后可能重拉,以及 SIGTERM 后身份漂移可能提前结束退休;legacy 活 owner 漂移也可能被新实例接管。这些行为在旧基底同样存在,本次 legacy 兼容路径未扩大其语义,因此不作为本 PR 新增回归处理。后续可单独将 live legacy mismatch 归为 unverifiable,并仅允许“OS 证明消失”或“稳定身份证明复用”推进退休/owner 接管。

@deepcoldy

Copy link
Copy Markdown
Owner

无 supervisor 回退的 P3 已在 PR 内修复

新 head:bb0e56047236c1768d940441fe5b2e7a2192178b。

  • service-target.ts 的默认 startFn 改为按 serviceState.processStart 调用格式感知 fleet reader;显式依赖注入行为不变,两个 dashboard 生产入口均走修复后的默认路径。
  • 新增真实 ps 回归:模拟 POSIX v1 service.json、无 supervisor 文件且不覆写 readProcessStart。修复前该用例稳定复现 503 plugin_service_unavailable,修复后正常解析 loopback target。
  • bun run build:通过。
  • 相关 11 文件:197/197 通过(含完整 plugin-service-env-injection 与此前 188 项 fleet/plugin/identity 回归)。

此次只修正只读身份校验,不改变持久状态语义。完整 CI 以该新 head 的首跑为准。

@deepcoldy
deepcoldy merged commit 76184f4 into deepcoldy:master Nov 28, 2026
14 checks passed
@deepcoldy

Copy link
Copy Markdown
Owner

感谢贡献,这个 PR 已合入 master(squash 落地 76184f41)。

评审过程中在原方案上补强了两点,均已包含在合入的代码里:

  • 非 Linux 平台的持久化进程身份改为 UTC 渲染并加 lstart-utc-v1: 版本前缀,存量裸 lstart 记录继续走旧渲染器、保持 fail-closed,系统时区变化不会再把同一活进程误判为 PID 复用;
  • 回归测试改用专用 fixture 进程并对清理路径加身份校验,测试失败时不会误伤测试运行器自身;另外补齐了启动对账、半写残留和停机路径的单点行为用例与变异验证。

无遗留 follow-up。欢迎继续贡献。

@big-omega
big-omega deleted the fix/supervisor-recycled-pid-recovery branch November 28, 2026 00:31
deepcoldy pushed a commit to uestcwangci/botmux that referenced this pull request Nov 28, 2026
…rity

同步主干 deepcoldy#3931(pi 转写目录)、deepcoldy#3942(fleet PID)、deepcoldy#3914(workflow 就绪信号)、
deepcoldy#3945(codex scheduled retain)、deepcoldy#3911(状态卡静默)。与本 PR 在
src/worker.ts、src/adapters/cli/types.ts 重叠,git 自动合并无冲突:
- types.ts:各提交在 StructuredBridgeDrainContext 等接口不同字段演进,
  本 PR 的 expectedTrailingFinal 与主干 backendType 等共存不互覆。
- worker.ts:主干改动落在 flushPending/spawnCliBody/workflow 就绪/
  readySignal 等其它函数与 import/常量区,与本 PR quiet-final、raw PTY
  working 观察、单调 interrupted episode、viewport 续租函数不同区段。
实际合并 tree 与 merge-tree --write-tree 预测逐字相等(3620e90f)。

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

Copy link
Copy Markdown

@big-omega @deepcoldy 合入 master 后的 Midscene E2E 失败:Feishu live Midscene 失败。

这次运行覆盖 4a364d9…2befd4807(6 个提交),同批合入的还有 #3914 #3931 #3932 #3936 #3929,失败不一定由本 PR 引起。

用例 跑道 状态 失败信息
Sandbox hides home and harness secrets feishu-core-sandbox failed Reply "ACK-e2e-sbx-1795838030573 SBX pid1=isolated home=HIDDEN bots=HIDDEN state=HIDDEN outside=BLOCKED workdir=OK" appeared 0 time(s) within 240000ms, expected 1 (clicked 直接开启会话 0×; re-opened 1×; re…

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.59.37

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.

3 participants