Skip to content

fix(git-host): macOS 上只剩未回收僵尸进程的进程组也能正常结束 - #63

Merged
xiaoyumuxi merged 1 commit into
mainfrom
fix/2017-macos-zombie-process-group
Nov 16, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
fix/2017-macos-zombie-process-group

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联 Issue / Linked issue

PR 类型 / Type

  • fix:缺陷修复

改动说明 / What changes

背景 / Why

#2017:lithe-platform-tests 的 lsp_process::terminating_a_language_server_ends_the_processes_it_started 在 macOS 上不稳定。在 main 上连续运行 15 次,失败 8 次。报错是:

a terminated server is disposed: CoreError { code: ProcessFailed, message: "Language-server process tree did not exit.", details: Some("Operation not permitted (os error 1)") }

原因在 git-host 的 Unix 进程组:

  • ProcessTree::terminate 先调用 Group::kill,也就是 kill(-pgid, SIGKILL),再轮询进程组里是否还有进程。
  • Group::kill 只把 ESRCH(组里没有进程)当作正常,其他错误一律返回,terminate 随即失败,不再等待。
  • 在 macOS 上,如果组里的进程都已经退出、但还没被父进程回收(都是僵尸进程),kill(-pgid, …) 返回 EPERM,不是 ESRCH。等它们被回收后,才返回 ESRCH。我用一个小程序在本机确认过:killpg 0 -> EPERM,killpg SIGKILL -> EPERM,回收后 -> ESRCH。
  • 语言服务器的 dispose 在根进程被回收后调用 tree.terminate。这时根进程启动的 runtime 子进程也已经被杀掉,正在等新的父进程 launchd 回收。如果正好落在这个时间窗口里,就会出现上面的报错。

这不只是测试的问题。生产代码走的是同一条路径:

  • lsp-engine 的 destroy 会把这个错误返回给调用方,会话留在注册表里不会删除。启动失败时的清理路径也一样。
  • 关闭 SSH 远程会话时,tree.kill 和 tree.terminate 会误报失败,写出 remote.session.cleanup_failed 的 WARN 日志。

改动 / Changes

rust/crates/git-host/src/lib.rs(Unix 的 Group)

  • kill:EPERM 也返回 Ok。EPERM 表示组里没有一个进程接受这个信号:要么在 macOS 上都是还没回收的僵尸进程,要么属于其他用户。两种情况下都没有别的信号可以发。是否真的结束,交给 ProcessTree::terminate 在截止时间内等待判断;如果进程一直不消失,terminate 照常返回 TimedOut。
  • has_live_processes:行为不变,EPERM 仍然算作"还有进程",所以僵尸进程在被回收之前仍然算在组里。只改了注释:原来只写了"属于其他用户",补上 macOS 的僵尸进程。

rust/crates/platform-tests

  • tests/platform/process_tree.rs:新增 #[cfg(unix)] 测试 a_unix_tree_of_exited_unreaped_processes_ends_without_error。

    1. 启动一个 Mode::Hold 夹具,用 tree.kill() 杀掉。
    2. 用 waitid(P_PID, …, WEXITED | WNOWAIT) 等到它退出,但不回收,确定它已经是僵尸进程。这一步在辅助线程里阻塞,主线程用 recv_timeout(DEADLINE) 等待,有明确的截止时间。
    3. 再调用一次 tree.kill(),断言返回 Ok。在 macOS 上,修复前这里返回 EPERM。
    4. 在后台线程回收根进程,断言 tree.terminate(DEADLINE) 返回 Ok,根进程是被信号结束的。

    这个测试不依赖时间窗口:组里只剩僵尸进程的状态是确定构造出来的,不靠竞争条件。Linux 允许给僵尸进程发信号,所以这个测试在 Linux 上修复前后应当都通过(没有实测);所有 Unix 平台的预期相同,没有用 cfg! 区分。

  • Cargo.toml:加 [target.'cfg(unix)'.dev-dependencies] libc = "0.2",测试要调用 waitid。Cargo.lock 随之更新,没有引入新版本的 crate。

不在本 PR 范围内 / Out of scope

类型专属:fix

复现与根因

  • 复现:在 main(6a14126)上反复运行 cargo test --manifest-path rust/Cargo.toml -p lithe-platform-tests,15 次中失败 8 次。
  • 根因:见上方"背景"。Group::kill 的错误处理来自 git-host 引入 ProcessTree 的时候,没有考虑到 macOS 对只剩僵尸进程的进程组返回 EPERM。
  • 影响平台:macOS。Linux 应当不受影响(没有实测):Linux 允许给僵尸进程发信号,kill 返回成功。Windows 不走这段代码。

检查项

  • 已先写回归测试,确认它修复前失败、修复后通过。

    • 测试路径:rust/crates/platform-tests/tests/platform/process_tree.rs 中的 a_unix_tree_of_exited_unreaped_processes_ends_without_error。
    • 修复前:只撤回 git-host 的修改再运行,测试在第二次 tree.kill() 处失败:
      a tree whose processes all exited is killed again without error: Os { code: 1, kind: PermissionDenied, message: "Operation not permitted" }
      
    • 修复后:通过。整个 lithe-platform-tests 连续运行 20 次,20 次全部通过,包括原来不稳定的 lsp_process::terminating_a_language_server_ends_the_processes_it_started。
  • [N/A] 手动复现:已经自动化。

  • 已检查全部调用方:Group::kill 只通过 ProcessTree::kill 和 ProcessTree::terminate 调用。它们的调用方有:

    • lsp-transport/process.rs 的 terminate_native 和 dispose;
    • workspace/sessions.rs 的 SSH 会话清理和 reap。

    这些调用方遇到 EPERM 时原来都会报失败或写 WARN,修复后都不再误报。进程真的没有结束时,terminate 仍然返回 TimedOut。

  • 另一平台:Windows 不走这段代码。Windows 目标的 clippy 已通过。

  • 只改了修复所需的代码。

  • [N/A] Agent Note:Core 仓库没有 .agents/notes/implemented/bug-fix/。这个约束写在 Group::kill 的注释和回归测试里。

范围检查 / Scope checks

  • 功能矩阵:N/A,没有新增宿主能力。
  • 插件归属:不涉及任何具体插件,测试只用 lithe-platform-fixture 夹具。
  • UI / i18n:N/A,没有界面文字。
  • 统一日志系统:没有新增或改动日志事件。修复后,SSH 会话清理不再误写 remote.session.cleanup_failed。
  • 逻辑归属:全部在 Rust Core。
  • 性能与内存:没有新增进程、线程或缓存,也没有新增轮询;terminate 原有的轮询不变。
  • 设置:N/A。
  • 插件契约:N/A,没有改插件契约、能力版本、插件函数协议或宿主方法。
  • 发行包与资源:N/A,没有新增运行时资源。
  • 无用代码:没有。

验证 / Verification

本机为 Apple M4,Rust 1.100.0。

命令或操作 平台 结果
cargo fmt --manifest-path rust/Cargo.toml --all -- --check macOS arm64 通过
scripts/rust-core/verify-rust-core-comments.sh macOS arm64 通过
node scripts/rust-core/verify-platform-tests.mjs macOS arm64 通过
cargo test --manifest-path rust/Cargo.toml --workspace --features lithe-remote/server --no-fail-fast macOS arm64 通过:2106 passed,0 failed
cargo test --manifest-path rust/Cargo.toml -p lithe-platform-tests,连续 20 次 macOS arm64 20 次全部通过(main 上 15 次失败 8 次)
撤回 git-host 的修改后运行新测试 macOS arm64 按预期失败,输出见上方
cargo clippy --manifest-path rust/Cargo.toml --workspace --all-targets -- -D warnings macOS arm64 通过
cargo clippy --manifest-path rust/Cargo.toml --workspace --all-targets --target x86_64-pc-windows-msvc -- -D warnings macOS 交叉检查 Windows 目标 通过

未能执行的检查和原因:

  • 没有在 Linux 上运行新测试。按 Linux 内核的行为,它在修复前后都应通过;PR 的 Linux CI 会运行它。
  • 没有在 Windows 上运行。Windows 不走这段代码,PR 标记 ready 后 CI 会运行 lithe-platform-tests。

提交前确认 / Final checks

… on macOS

macOS answers kill(-pgid, sig) with EPERM, not ESRCH, while every member of
the group has exited but is not reaped yet. Group::kill treated that as a
failure, so ProcessTree::terminate gave up at once instead of waiting.
Disposing a language server hit this when the runtime its launcher started
was still waiting for launchd to reap it: dispose reported "Language-server
process tree did not exit" with "Operation not permitted", and the
lsp_process platform test failed 8 times in 15 runs.

Group::kill now accepts EPERM: no member takes the signal, and terminate
keeps waiting until the members are gone or reports TimedOut. A new
platform test builds the zombie-only group on purpose with waitid(WNOWAIT)
and fails without the fix.

Closes 1lck/Lithe-IDEA#2017
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 16, 2026 08:03
@xiaoyumuxi
xiaoyumuxi merged commit ad148ec into main Nov 16, 2026
6 of 7 checks passed
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.

[Bug] macOS 上 lsp_process 测试不稳定:terminate 后 dispose 报 EPERM(Language-server process tree did not exit)

1 participant