Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop): reject exited Linux handoff processes

Richie McIlroy13cec5e55d0e
diff --git a/apps/desktop-gpui/src/single_instance.rs b/apps/desktop-gpui/src/single_instance.rs
index 5b8f47376..1b566df39 100644
--- a/apps/desktop-gpui/src/single_instance.rs
+++ b/apps/desktop-gpui/src/single_instance.rs
@@ -85,20 +85,26 @@ pub fn acquire() {
}
/// Guard against pid reuse: only ever signal a process that is actually this
-/// binary. `comm` is the executable path on macOS and the (15-char) image
-/// name on Linux; `cap-gpui` fits both.
+/// binary. Linux zombies retain their comm name but lose their exe link.
#[cfg(unix)]
fn is_cap_gpui(pid: i32) -> bool {
if unsafe { libc::kill(pid, 0) } != 0 {
return false;
}
- std::process::Command::new("ps")
- .args(["-p", &pid.to_string(), "-o", "comm="])
- .output()
- .is_ok_and(|output| {
- output.status.success()
- && is_cap_gpui_image(Path::new(String::from_utf8_lossy(&output.stdout).trim()))
- })
+ #[cfg(target_os = "linux")]
+ {
+ std::fs::read_link(format!("/proc/{pid}/exe")).is_ok_and(|path| is_cap_gpui_image(&path))
+ }
+ #[cfg(not(target_os = "linux"))]
+ {
+ std::process::Command::new("ps")
+ .args(["-p", &pid.to_string(), "-o", "comm="])
+ .output()
+ .is_ok_and(|output| {
+ output.status.success()
+ && is_cap_gpui_image(Path::new(String::from_utf8_lossy(&output.stdout).trim()))
+ })
+ }
}
fn is_cap_gpui_image(path: &Path) -> bool {
@@ -107,9 +113,12 @@ fn is_cap_gpui_image(path: &Path) -> bool {
#[cfg(not(windows))]
const IMAGE_NAME: &str = "cap-gpui";
- path.file_name()
- .and_then(std::ffi::OsStr::to_str)
- .is_some_and(|name| name.eq_ignore_ascii_case(IMAGE_NAME))
+ let Some(name) = path.file_name().and_then(std::ffi::OsStr::to_str) else {
+ return false;
+ };
+ #[cfg(target_os = "linux")]
+ let name = name.strip_suffix(" (deleted)").unwrap_or(name);
+ name.eq_ignore_ascii_case(IMAGE_NAME)
}
#[cfg(windows)]
@@ -451,6 +460,56 @@ mod tests {
assert!(!is_cap_gpui_image(Path::new("cap-gpui-helper")));
}
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn linux_process_check_distinguishes_live_unlinked_and_exited_processes() {
+ let directory = std::env::temp_dir().join(format!(
+ "cap-gpui-process-test-{}",
+ crate::store::new_uuid_v4()
+ ));
+ std::fs::create_dir(&directory).unwrap();
+ let binary = directory.join("cap-gpui");
+ std::fs::copy("/bin/sleep", &binary).unwrap();
+ let mut child = std::process::Command::new(&binary)
+ .arg("30")
+ .spawn()
+ .unwrap();
+ let pid = child.id() as i32;
+ let started = std::time::Instant::now();
+ // spawn can return before /proc stops exposing the child's pre-exec image.
+ while !std::fs::read_link(format!("/proc/{pid}/exe")).is_ok_and(|path| path == binary)
+ && started.elapsed() < std::time::Duration::from_secs(5)
+ {
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ }
+ let live = super::is_cap_gpui(pid);
+ std::fs::remove_file(&binary).unwrap();
+ let unlinked = super::is_cap_gpui(pid);
+ child.kill().unwrap();
+ let started = std::time::Instant::now();
+ let mut zombie = false;
+ while started.elapsed() < std::time::Duration::from_secs(5) {
+ zombie = std::fs::read_to_string(format!("/proc/{pid}/stat")).is_ok_and(|stat| {
+ stat.rsplit_once(") ")
+ .is_some_and(|(_, state)| state.starts_with('Z'))
+ });
+ if zombie {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ }
+ let exited = super::is_cap_gpui(pid);
+ child.wait().unwrap();
+ std::fs::remove_dir(directory).unwrap();
+
+ assert!(live);
+ assert!(unlinked);
+ assert!(zombie);
+ assert!(!exited);
+ assert!(!super::is_cap_gpui(pid));
+ assert!(!super::is_cap_gpui(std::process::id() as i32));
+ }
+
#[test]
fn forwarding_endpoint_requires_a_live_instance_shape() {
assert_eq!(
diff --git a/apps/desktop/src-tauri/src/gpui_app.rs b/apps/desktop/src-tauri/src/gpui_app.rs
index 8f6d6f1ea..091479497 100644
--- a/apps/desktop/src-tauri/src/gpui_app.rs
+++ b/apps/desktop/src-tauri/src/gpui_app.rs
@@ -351,7 +351,12 @@ fn running_instance_pid() -> Option<u32> {
.parse::<u32>()
.ok()?;
- #[cfg(unix)]
+ #[cfg(target_os = "linux")]
+ {
+ linux_gpui_process_is_running(pid).then_some(pid)
+ }
+
+ #[cfg(all(unix, not(target_os = "linux")))]
{
let alive = std::process::Command::new("ps")
.args(["-p", &pid.to_string(), "-o", "comm="])
@@ -378,10 +383,18 @@ fn running_instance_pid() -> Option<u32> {
}
}
+#[cfg(target_os = "linux")]
+fn linux_gpui_process_is_running(pid: u32) -> bool {
+ std::fs::read_link(format!("/proc/{pid}/exe")).is_ok_and(|path| is_gpui_process_image(&path))
+}
+
fn is_gpui_process_image(path: &std::path::Path) -> bool {
- path.file_name()
- .and_then(std::ffi::OsStr::to_str)
- .is_some_and(|name| name.eq_ignore_ascii_case(BINARY_NAME))
+ let Some(name) = path.file_name().and_then(std::ffi::OsStr::to_str) else {
+ return false;
+ };
+ #[cfg(target_os = "linux")]
+ let name = name.strip_suffix(" (deleted)").unwrap_or(name);
+ name.eq_ignore_ascii_case(BINARY_NAME)
}
#[cfg(any(target_os = "macos", windows, test))]
@@ -900,6 +913,51 @@ mod tests {
)));
}
+ #[cfg(target_os = "linux")]
+ #[test]
+ fn linux_process_check_distinguishes_live_unlinked_and_exited_processes() {
+ let directory = tempfile::tempdir().unwrap();
+ let binary = directory.path().join(BINARY_NAME);
+ std::fs::copy("/bin/sleep", &binary).unwrap();
+ let mut child = std::process::Command::new(&binary)
+ .arg("30")
+ .spawn()
+ .unwrap();
+ let pid = child.id();
+ let started = std::time::Instant::now();
+ // spawn can return before /proc stops exposing the child's pre-exec image.
+ while !std::fs::read_link(format!("/proc/{pid}/exe")).is_ok_and(|path| path == binary)
+ && started.elapsed() < std::time::Duration::from_secs(5)
+ {
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ }
+ let live = super::linux_gpui_process_is_running(pid);
+ std::fs::remove_file(&binary).unwrap();
+ let unlinked = super::linux_gpui_process_is_running(pid);
+ child.kill().unwrap();
+ let started = std::time::Instant::now();
+ let mut zombie = false;
+ while started.elapsed() < std::time::Duration::from_secs(5) {
+ zombie = std::fs::read_to_string(format!("/proc/{pid}/stat")).is_ok_and(|stat| {
+ stat.rsplit_once(") ")
+ .is_some_and(|(_, state)| state.starts_with('Z'))
+ });
+ if zombie {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ }
+ let exited = super::linux_gpui_process_is_running(pid);
+ child.wait().unwrap();
+
+ assert!(live);
+ assert!(unlinked);
+ assert!(zombie);
+ assert!(!exited);
+ assert!(!super::linux_gpui_process_is_running(pid));
+ assert!(!super::linux_gpui_process_is_running(std::process::id()));
+ }
+
#[test]
fn gpui_forwarding_endpoint_requires_the_owner_identity() {
assert_eq!(