Skip to content

fix: detach win32 self-restart replacement so it outlives parent exit (#2095) - #2112

Merged
ranxianglei merged 1 commit into
masterfrom
2026-10-04_fix-windows-self-restart-detach
Oct 5, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-10-04_fix-windows-self-restart-detach

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

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 → job-object kill),而不是去加固 probeReady 断言(那只是治标)。一旦 detach,端口绑定即隐含存活,probeReady → handover 恢复自洽。

⚠️ 行为变更(必报)

项 旧值 新值 原因
win32 self-restart spawn options { stdio: "inherit", env }(非 detached) { detached: true, windowsHide: true, stdio: "ignore", env } 非 detached 正是 bug 本身
posix spawn options { stdio: "inherit", env } 不变 保住 #414 group kill

不涉及 config schema / persistence 格式版本 / wire protocol / message shape;纯 process-lifecycle 内部改动,无新配置项。

测试

  • 新增 selfRestartSpawnOptions 平台矩阵测试:win32 → detach/windowsHide/stdio-ignore;linux+darwin → non-detached/no-windowsHide/stdio-inherit。
  • 新增 wiring guard:对 win32 和 linux 各跑一遍 performSelfRestart happy-path,断言请求 platform 对应的 options 真的传进了 spawn(防回归:硬编码一套 options、或忽略 deps.platform,都会被抓住)。
  • tests/auto-restart.test.ts 10/10 绿;typecheck 干净(rebase 到当前 master v0.1.184 后复验)。全量回归交给 CI。

…#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.
@ranxianglei ranxianglei mentioned this pull request Oct 4, 2026
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-04_fix-windows-self-restart-detach (7aa0e00)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2158-pr.2112.7baef40

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2112.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #2112 审查完成(拉取分支 7aa0e00f,base = 当前 master 2f9cc00b,单 commit,diff 仅 src/restart.ts + tests/auto-restart.test.ts,+105/-15)

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 触碰路径的既有行为逐项清点):

③ 代码审查:平台分流分支正确;child.unref()(src/restart.ts:283)既有,父进程不被子句柄挂住;失败路径(子早亡 → kill + resumeListening + ok:false)在 detach 下等价工作(win32 kill = TerminateProcess 定向 pid)。无 as any/@ts-ignore,server 模块无 console.error。通过。

④ 测试有效性:

  • 平台矩阵测试钉住两个分支(win32: detached/windowsHide/ignore/env;linux+darwin: non-detached/no-windowsHide/inherit/env)——未来任何「简化回单一非 detached」都会被抓住。有牙,非恒真。
  • wiring guard 对 win32/linux 各跑一遍 happy-path,deepEqual 断言 record.options === selfRestartSpawnOptions(platform)——硬编码一套 options 或忽略 deps.platform 两种回归都抓得住;stubbed spawnImpl 使跨平台 options 从不进真实 spawn,listen(0) + 固定超时,确定性成立。
  • 残留说明(权衡后接受,非缺陷):没有真实进程「存活过父进程退出」的 e2e(需跨进程时序断言,共享 CI runner 上 flake 风险高);存活性质由 OP 受控复现(options 为唯一变量)+ 选项矩阵钉住共同支撑,证据链充分。

查重:open PR 中无其他触碰 self-restart / win32 spawn 路径的条目;关联 issue(#1887/#1742/#813/#1603/#2079)均为不同机制,已在 issue 中正确引用。无重复。


结构化七问:

  1. 是什么问题:Windows 上 auto-update self-restart「报告成功但替换进程随父进程一起死掉」,升级后 bili 离线直到别的东西把它拉起。
  2. 什么原因导致的:performSelfRestart 全平台用非 detached + stdio inherit spawn 替换进程;Windows 上 console-attached 父进程的非 detached 子进程挂在 libuv 的 kill-on-job-close job object 上(并共享 console teardown),父进程 probe 一过即 exit、job object 销毁连坐子进程;probe 只证明端口绑定、不证明存活过父退出,handover 因此记成成功。缺陷产生于 spawn 选项,不在 probe。
  3. 怎么修复的:spawn options 按平台分流(纯函数 selfRestartSpawnOptions)——win32 取 detached+windowsHide+stdio ignore(与 launcher 自身 spawn proxy 的方式一致,同时去掉 console attachment 和 job object);posix 保持不变以保住 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。修机制本身而非加固断言。
  4. 是否有回归问题:无。posix 字节级不变;win32 唯一有意变更且已完整披露(见上逐项清点);无 fail-fast/阈值/时序/输出格式漂移。
  5. 是否破坏缓存:不涉及。
  6. 是否改了设计方向:无。仅把单一套 options 拆成按平台取值的纯函数(可测性 seam)+ deps 增加可选 platform 字段(默认 process.platform)。
  7. 是否引入了新的配置:无新 env / config / 默认值。

判定:可合并。 单模块 process-lifecycle fix,回归测试有牙且 CI 四平台全绿,rebase 到当前 master。涉及 win32 默认行为(spawn options)变更,按 §7.4 留人工合并不走 auto-merge。请 @ranxianglei 自行合并:#2112

@ranxianglei
ranxianglei merged commit 06a9047 into master Oct 5, 2026
15 checks passed
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.30-master.06a9047 (rolling master tag).

npm install -g billion-context@0.0.30-master.06a9047  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant