From 746a2f00902c00daa8eab865370a9cca6f41ae9a Mon Sep 17 00:00:00 2001 From: Yao Jingxi <23722032@bjtu.edu.cn> Date: Mon, 16 Nov 2026 16:01:48 +0800 Subject: [PATCH] fix(git-host): a process group of unreaped zombies ends without error 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 https://github.com/1lck/Lithe-IDEA/issues/2017 --- rust/Cargo.lock | 1 + rust/crates/git-host/src/lib.rs | 14 +++-- rust/crates/platform-tests/Cargo.toml | 3 + .../tests/platform/process_tree.rs | 57 +++++++++++++++++++ 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index b17a586f..8358298b 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -1187,6 +1187,7 @@ version = "0.1.0" name = "lithe-platform-tests" version = "0.1.0" dependencies = [ + "libc", "lithe-git-host", "lithe-lsp-transport", "lithe-platform", diff --git a/rust/crates/git-host/src/lib.rs b/rust/crates/git-host/src/lib.rs index ddafdd18..9609add5 100644 --- a/rust/crates/git-host/src/lib.rs +++ b/rust/crates/git-host/src/lib.rs @@ -426,10 +426,15 @@ mod platform { return Ok(()); } let error = io::Error::last_os_error(); - if error.raw_os_error() == Some(libc::ESRCH) { - return Ok(()); + match error.raw_os_error() { + Some(libc::ESRCH) => Ok(()), + // No member accepted the signal: on macOS every member is a + // zombie its parent has not reaped yet, or a member belongs + // to another user. Nothing more can be sent either way, and + // `ProcessTree::terminate` waits until the members are gone. + Some(libc::EPERM) => Ok(()), + _ => Err(error), } - Err(error) } pub fn has_live_processes(&self) -> io::Result { // SAFETY: signal 0 only checks whether the group still has members. @@ -439,7 +444,8 @@ mod platform { let error = io::Error::last_os_error(); match error.raw_os_error() { Some(libc::ESRCH) => Ok(false), - // A member exists but belongs to another user, so it is alive. + // A member exists that refuses signals: an unreaped zombie on + // macOS or another user's process. It counts until it is gone. Some(libc::EPERM) => Ok(true), _ => Err(error), } diff --git a/rust/crates/platform-tests/Cargo.toml b/rust/crates/platform-tests/Cargo.toml index e530e868..eff76995 100644 --- a/rust/crates/platform-tests/Cargo.toml +++ b/rust/crates/platform-tests/Cargo.toml @@ -15,3 +15,6 @@ hosts = ["macos", "windows"] lithe-git-host = { path = "../git-host" } lithe-lsp-transport = { path = "../lsp-transport" } lithe-platform = { path = "../platform" } + +[target.'cfg(unix)'.dev-dependencies] +libc = "0.2" diff --git a/rust/crates/platform-tests/tests/platform/process_tree.rs b/rust/crates/platform-tests/tests/platform/process_tree.rs index 06b29381..ff2200e0 100644 --- a/rust/crates/platform-tests/tests/platform/process_tree.rs +++ b/rust/crates/platform-tests/tests/platform/process_tree.rs @@ -151,6 +151,63 @@ fn a_unix_tree_ends_only_once_its_root_is_reaped() { .expect("a reaped root no longer counts"); } +/// A group whose members have all exited but are not reaped yet is a normal +/// moment while a tree ends: the runtime a language server started can still +/// wait for its new parent to reap it after the root is gone. macOS refuses to +/// signal such a group (`EPERM`), and ending the tree must not report that as +/// a failure. +#[cfg(unix)] +#[test] +fn a_unix_tree_of_exited_unreaped_processes_ends_without_error() { + let Tree { + tree, + root, + output: _output, + _input, + } = spawn_tree(Mode::Hold); + + tree.kill().expect("a running tree is killed"); + wait_until_exited_unreaped(root.id()); + tree.kill() + .expect("a tree whose processes all exited is killed again without error"); + + let root_exit = reap_in_background(root); + tree.terminate(DEADLINE) + .expect("the tree ends once its exited root is reaped"); + let status = root_exit.recv_timeout(DEADLINE).expect("root reaped"); + assert!(!status.success()); +} + +/// Waits until the child `process_id` has exited and leaves it unreaped, so it +/// stays a zombie member of its process group until its `Child` is waited on. +#[cfg(unix)] +fn wait_until_exited_unreaped(process_id: u32) { + let (sender, receiver) = mpsc::channel(); + std::thread::spawn(move || loop { + // SAFETY: `info` is a valid out-parameter, and `WNOWAIT` leaves the + // child for its `Child` owner to reap. + let result = unsafe { + let mut info: libc::siginfo_t = std::mem::zeroed(); + libc::waitid( + libc::P_PID, + process_id as libc::id_t, + &mut info, + libc::WEXITED | libc::WNOWAIT, + ) + }; + let error = std::io::Error::last_os_error(); + if result != 0 && error.kind() == std::io::ErrorKind::Interrupted { + continue; + } + let _ = sender.send(if result == 0 { Ok(()) } else { Err(error) }); + break; + }); + receiver + .recv_timeout(DEADLINE) + .expect("the killed root exits") + .expect("wait for the root without reaping it"); +} + #[cfg(windows)] #[test] fn dropping_a_process_tree_ends_it_like_a_crashed_host() {