Repository navigation
fix(git-host): macOS 上只剩未回收僵尸进程的进程组也能正常结束 - #63
Merged
Merged
Conversation
… 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
9 tasks done
xiaoyumuxi
marked this pull request as ready for review
November 16, 2026 08:03
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue / Linked issue
PR 类型 / Type
改动说明 / What changes
背景 / Why
#2017:
lithe-platform-tests的lsp_process::terminating_a_language_server_ends_the_processes_it_started在 macOS 上不稳定。在 main 上连续运行 15 次,失败 8 次。报错是:原因在
git-host的 Unix 进程组:ProcessTree::terminate先调用Group::kill,也就是kill(-pgid, SIGKILL),再轮询进程组里是否还有进程。Group::kill只把ESRCH(组里没有进程)当作正常,其他错误一律返回,terminate随即失败,不再等待。kill(-pgid, …)返回EPERM,不是ESRCH。等它们被回收后,才返回ESRCH。我用一个小程序在本机确认过:killpg 0 -> EPERM,killpg SIGKILL -> EPERM,回收后-> ESRCH。dispose在根进程被回收后调用tree.terminate。这时根进程启动的 runtime 子进程也已经被杀掉,正在等新的父进程 launchd 回收。如果正好落在这个时间窗口里,就会出现上面的报错。这不只是测试的问题。生产代码走的是同一条路径:
lsp-engine的destroy会把这个错误返回给调用方,会话留在注册表里不会删除。启动失败时的清理路径也一样。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-teststests/platform/process_tree.rs:新增#[cfg(unix)]测试a_unix_tree_of_exited_unreaped_processes_ends_without_error。Mode::Hold夹具,用tree.kill()杀掉。waitid(P_PID, …, WEXITED | WNOWAIT)等到它退出,但不回收,确定它已经是僵尸进程。这一步在辅助线程里阻塞,主线程用recv_timeout(DEADLINE)等待,有明确的截止时间。tree.kill(),断言返回Ok。在 macOS 上,修复前这里返回EPERM。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
复现与根因
cargo test --manifest-path rust/Cargo.toml -p lithe-platform-tests,15 次中失败 8 次。Group::kill的错误处理来自git-host引入ProcessTree的时候,没有考虑到 macOS 对只剩僵尸进程的进程组返回EPERM。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()处失败: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
lithe-platform-fixture夹具。remote.session.cleanup_failed。terminate原有的轮询不变。验证 / Verification
本机为 Apple M4,Rust 1.100.0。
cargo fmt --manifest-path rust/Cargo.toml --all -- --checkscripts/rust-core/verify-rust-core-comments.shnode scripts/rust-core/verify-platform-tests.mjscargo test --manifest-path rust/Cargo.toml --workspace --features lithe-remote/server --no-fail-fastcargo test --manifest-path rust/Cargo.toml -p lithe-platform-tests,连续 20 次git-host的修改后运行新测试cargo clippy --manifest-path rust/Cargo.toml --workspace --all-targets -- -D warningscargo clippy --manifest-path rust/Cargo.toml --workspace --all-targets --target x86_64-pc-windows-msvc -- -D warnings未能执行的检查和原因:
lithe-platform-tests。提交前确认 / Final checks
main(6a14126)创建,目标分支main。不依赖 fix(platform): Unix 上 \ 和 : 是文件名字符,路径规则统一到 lithe-platform #62。