Repository navigation
fix: detach win32 self-restart replacement so it outlives parent exit (#2095) - #2112
Conversation
…#2095) performSelfRestart spawned the replacement non-detached with stdio inherit on every platform. On Windows a non-detached child of a console-attached parent rides libuv's kill-on-job-close job object and is terminated the instant the parent exits mid-boot, so bili went offline after every auto-update until something else started it. The readiness probe only proves the child bound the port, not that it survives, so handover logged as success. Split spawn options by platform via selfRestartSpawnOptions(): - win32: detached + windowsHide + stdio ignore (mirrors the launcher's own proxy spawn). Detaching drops both the console attachment and the job object so the replacement outlives the parent; stdio ignore loses nothing because the durable log file (logger.ts) is independent of stderr. The #414 group-kill rationale is moot on win32 - stopProxy() relies on the BILI_PARENT_PID watchdog there, never a group kill. - posix: unchanged (non-detached + stdio inherit) so a launcher's stopProxy() group kill still reaches the replacement (#414). Adds a pure options-matrix test plus a wiring guard that runs performSelfRestart for both platforms and asserts the requested options actually reach spawn.
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.2158-pr.2112.7baef40This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2112.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 PR #2112 审查完成(拉取分支 CI:15/15 全绿 —— test 矩阵 ubuntu×{22,24} + windows×{22,24}、全部 e2e lane(codex / dsh / pi / opencode-native / V2-WS / hermetic-registry 双平台)、build-artifact、version-guard、kernel-guard。 ① 原 issue 方向关:#2095(ezjbc)给出生产日志 + 最小复现(同 Node、同子脚本、唯一变量是 spawn options:inherit 死 / detached 活),根因定位成立。本 PR 采纳 OP 建议 #1(detach);OP 建议 #2(加固 probeReady 断言)有意不采纳——理由成立:detach 移除 kill-on-job-close 机制后「端口绑定 ⇒ 存活过父进程退出」恢复自洽,probe 失败 / 子进程早亡仍走既有 exit-event + resumeListening 回滚路径。修的是产生缺陷的机制本身(疏),不是去堵断言。方向通过。 ② 回归审查(diff 触碰路径的既有行为逐项清点):
③ 代码审查:平台分流分支正确; ④ 测试有效性:
查重:open PR 中无其他触碰 self-restart / win32 spawn 路径的条目;关联 issue(#1887/#1742/#813/#1603/#2079)均为不同机制,已在 issue 中正确引用。无重复。 结构化七问:
判定:可合并。 单模块 process-lifecycle fix,回归测试有牙且 CI 四平台全绿,rebase 到当前 master。涉及 win32 默认行为(spawn options)变更,按 §7.4 留人工合并不走 auto-merge。请 @ranxianglei 自行合并:#2112 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.30-master.06a9047 # this exact build
npm install -g billion-context@master # always the newest merged build
|
Closes #2095
是什么问题
Windows 上 auto-update 的 self-restart「报告成功,但替换进程随父进程一起死掉」——升级后 bili 离线,直到别的东西把它拉起来。
根因(已对照代码验证)
performSelfRestart()(src/restart.ts)在所有平台都用{ stdio: "inherit", env }非 detached spawn 替换进程。Windows 上,console-attached 父进程的非 detached 子进程挂在 libuv 的 kill-on-job-close JOB OBJECT 上;父进程 readiness probe 一过就process.exit(0),job object 随即销毁、把刚起来的子进程一起杀掉。probe 只证明子进程绑定了端口,不证明它能活过父进程退出,所以 handover 记成了成功。怎么修复的(按平台分流)
新增纯函数
selfRestartSpawnOptions(platform),performSelfRestart改为按它取 options:detached + windowsHide + stdio ignore(与 launcher 自己 spawn proxy 的方式一致)。detached 同时去掉 console attachment 和 job object,替换进程得以活过父进程;stdio ignore 不丢日志——durable log file(logger.ts)独立于 stderr。Client cancellation = orphaned AbortError ×260/day + billed usage discarded (lastInputTokens frozen) + passthrough path missing 10min timer (no finally) | 客户端取消 = 无主 AbortError ×260/天 + 已计费 usage 丢弃(lastInputTokens 冻结)+ 透传路径漏 10min timer(无 finally) #414 的 group-kill 理由在 win32 上不成立:那边stopProxy()靠BILI_PARENT_PIDwatchdog,从不 group kill。stopProxy()group kill 仍能打到替换进程(Client cancellation = orphaned AbortError ×260/day + billed usage discarded (lastInputTokens frozen) + passthrough path missing 10min timer (no finally) | 客户端取消 = 无主 AbortError ×260/天 + 已计费 usage 丢弃(lastInputTokens 冻结)+ 透传路径漏 10min timer(无 finally) #414)。修的是产生缺陷的机制本身(非 detached → job-object kill),而不是去加固
probeReady断言(那只是治标)。一旦 detach,端口绑定即隐含存活,probeReady → handover 恢复自洽。{ stdio: "inherit", env }(非 detached){ detached: true, windowsHide: true, stdio: "ignore", env }{ stdio: "inherit", env }不涉及 config schema / persistence 格式版本 / wire protocol / message shape;纯 process-lifecycle 内部改动,无新配置项。
测试
selfRestartSpawnOptions平台矩阵测试:win32 → detach/windowsHide/stdio-ignore;linux+darwin → non-detached/no-windowsHide/stdio-inherit。deps.platform,都会被抓住)。tests/auto-restart.test.ts10/10 绿;typecheck 干净(rebase 到当前 master v0.1.184 后复验)。全量回归交给 CI。