From 74c1a6e75af9dacdf5f8898ea225e8154806b6f3 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 02:29:59 -0600 Subject: [PATCH] id(serve): is_process_alive treats EPERM as alive (#2384) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit kill(pid, 0) failing was read as death, but EPERM means the process exists and is owned by another user — get_serve_info then deleted the live serve's .iroh-serve.lock and reported no serve running. Read the errno: alive iff the probe succeeds or errno is EPERM; ESRCH and other errnos stay dead. Mirrors mcpx ProcessAlive (pkgs/mcpx/internal/daemon/paths.go). The assert-nothing pid-1 test now asserts pid 1 reads as alive on Unix. --- pkgs/id/src/commands/serve.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pkgs/id/src/commands/serve.rs b/pkgs/id/src/commands/serve.rs index d72fcbcd94..1497a5fb99 100644 --- a/pkgs/id/src/commands/serve.rs +++ b/pkgs/id/src/commands/serve.rs @@ -203,7 +203,12 @@ pub fn is_process_alive(pid: u32) -> bool { // SAFETY: libc::kill with signal 0 only checks process existence without // sending any signal. The pid cast from u32 to i32 is safe because valid // PIDs on Unix are always positive and fit in i32. - unsafe { libc::kill(pid as i32, 0) == 0 } + // #2384: a failed probe is not proof of death — EPERM means the + // process exists but is owned by another user, so it is alive; + // only ESRCH (no such process) and other errnos mean dead. This + // mirrors mcpx's ProcessAlive (pkgs/mcpx/internal/daemon/paths.go). + let rc = unsafe { libc::kill(pid as i32, 0) }; + rc == 0 || std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM) } #[cfg(not(unix))] { @@ -1927,12 +1932,14 @@ mod tests { #[test] fn test_is_process_alive_pid_1() { - // PID 1 (init) should exist on Unix systems, but may not be visible - // in containerized environments where the container has its own PID namespace + // PID 1 exists in every Unix PID namespace (as init, or as the + // namespace's init in a container). #2384: when the test runs + // unprivileged, pid 1 is foreign-owned and kill(1, 0) fails with + // EPERM — that must still answer alive; as root the probe succeeds + // outright. Either way the assertion holds. #[cfg(unix)] { - // Just check that the function doesn't panic - the result depends on environment - let _ = is_process_alive(1); + assert!(is_process_alive(1), "pid 1 exists and must read as alive (EPERM is not death)"); } }