Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Patch async-process to allow reusing their reaper (#59156)
Cole Millerd4cc8d240965
diff --git a/Cargo.lock b/Cargo.lockindex 4479182837d..2641cecc89e 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -1119,8 +1119,7 @@ dependencies = [[[package]]name = "async-process"version = "2.5.0"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "fc50921ec0055cdd8a16de48773bfeec5c972598674347252c0399676be7da75"+source = "git+https://github.com/zed-industries/async-process.git?rev=0b6d6713570af61806e1e5cb40e0f757cb93fd9d#0b6d6713570af61806e1e5cb40e0f757cb93fd9d"dependencies = ["async-channel 2.5.0","async-io",@@ -5266,7 +5265,7 @@ dependencies = ["libc","option-ext","redox_users 0.5.2",- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -6063,7 +6062,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"dependencies = ["libc",- "windows-sys 0.52.0",+ "windows-sys 0.61.2",][[package]]@@ -7271,7 +7270,7 @@ dependencies = ["gobject-sys","libc","system-deps",- "windows-sys 0.52.0",+ "windows-sys 0.61.2",][[package]]@@ -11122,7 +11121,7 @@ version = "0.6.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "536bfad37a309d62069485248eeaba1e8d9853aaf951caaeaed0585a95346f08"dependencies = [- "windows-sys 0.60.2",+ "windows-sys 0.61.2",][[package]]@@ -11552,7 +11551,7 @@ version = "0.50.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"dependencies = [- "windows-sys 0.59.0",+ "windows-sys 0.61.2",][[package]]@@ -15633,7 +15632,7 @@ dependencies = ["errno 0.3.14","libc","linux-raw-sys 0.11.0",- "windows-sys 0.52.0",+ "windows-sys 0.61.2",][[package]]@@ -17066,7 +17065,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e"dependencies = ["libc",- "windows-sys 0.60.2",+ "windows-sys 0.61.2",][[package]]@@ -18183,7 +18182,7 @@ dependencies = ["getrandom 0.3.4","once_cell","rustix 1.1.2",- "windows-sys 0.52.0",+ "windows-sys 0.61.2",][[package]]@@ -21184,7 +21183,7 @@ version = "0.1.11"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"dependencies = [- "windows-sys 0.48.0",+ "windows-sys 0.61.2",][[package]]diff --git a/Cargo.toml b/Cargo.tomlindex e726f7cba5c..4c73396a45c 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -893,6 +893,7 @@ features = [][patch.crates-io]+async-process = { git = "https://github.com/zed-industries/async-process.git", rev = "0b6d6713570af61806e1e5cb40e0f757cb93fd9d" }async-task = { git = "https://github.com/smol-rs/async-task.git", rev = "b4486cd71e4e94fbda54ce6302444de14f4d190e" }windows-capture = { git = "https://github.com/zed-industries/windows-capture.git", rev = "f0d6c1b6691db75461b732f6d5ff56eed002eeb9" }calloop = { git = "https://github.com/zed-industries/calloop" }diff --git a/crates/util/src/command/darwin.rs b/crates/util/src/command/darwin.rsindex 01a3c9c8249..cf99eada331 100644--- a/crates/util/src/command/darwin.rs+++ b/crates/util/src/command/darwin.rs@@ -3,14 +3,13 @@ use mach2::exception_types::{};use mach2::port::{MACH_PORT_NULL, mach_port_t};use mach2::thread_status::{THREAD_STATE_NONE, thread_state_flavor_t};-use smol::Unblock;+use smol::Async;use std::collections::BTreeMap;use std::ffi::{CString, OsStr, OsString};use std::io;use std::os::fd::AsRawFd;use std::os::unix::ffi::OsStrExt;use std::os::unix::io::FromRawFd;-use std::os::unix::process::ExitStatusExt;use std::path::{Path, PathBuf};use std::process::{ExitStatus, Output};use std::ptr;@@ -230,79 +229,30 @@ impl Command {#[derive(Debug)]pub struct Child {- pid: libc::pid_t,- pub stdin: Option<Unblock<std::fs::File>>,- pub stdout: Option<Unblock<std::fs::File>>,- pub stderr: Option<Unblock<std::fs::File>>,- kill_on_drop: bool,- status: Option<ExitStatus>,-}--impl Drop for Child {- fn drop(&mut self) {- if self.kill_on_drop && self.status.is_none() {- let _ = self.kill();- }- }+ inner: smol::process::Child,+ pub stdin: Option<Async<std::fs::File>>,+ pub stdout: Option<Async<std::fs::File>>,+ pub stderr: Option<Async<std::fs::File>>,}impl Child {pub fn id(&self) -> u32 {- self.pid as u32+ self.inner.id()}pub fn kill(&mut self) -> io::Result<()> {- let result = unsafe { libc::kill(self.pid, libc::SIGKILL) };- if result == -1 {- Err(io::Error::last_os_error())- } else {- Ok(())- }+ self.inner.kill()}pub fn try_status(&mut self) -> io::Result<Option<ExitStatus>> {- if let Some(status) = self.status {- return Ok(Some(status));- }-- let mut status: libc::c_int = 0;- let result = unsafe { libc::waitpid(self.pid, &mut status, libc::WNOHANG) };-- if result == -1 {- Err(io::Error::last_os_error())- } else if result == 0 {- Ok(None)- } else {- let exit_status = ExitStatus::from_raw(status);- self.status = Some(exit_status);- Ok(Some(exit_status))- }+ self.inner.try_status()}pub fn status(&mut self,) -> impl std::future::Future<Output = io::Result<ExitStatus>> + Send + 'static {self.stdin.take();-- let pid = self.pid;- let cached_status = self.status;-- async move {- if let Some(status) = cached_status {- return Ok(status);- }-- smol::unblock(move || {- let mut status: libc::c_int = 0;- let result = unsafe { libc::waitpid(pid, &mut status, 0) };- if result == -1 {- Err(io::Error::last_os_error())- } else {- Ok(ExitStatus::from_raw(status))- }- })- .await- }+ self.inner.status()}pub async fn output(mut self) -> io::Result<Output> {@@ -526,13 +476,13 @@ fn spawn_posix_spawn(cvt_nz(spawn_result)?;+ let inner = smol::process::Child::adopt_raw_pid(pid as u32, true, kill_on_drop)?;+Ok(Child {- pid,- stdin: stdin_write.map(|fd| Unblock::new(fd)),- stdout: stdout_read.map(|fd| Unblock::new(fd)),- stderr: stderr_read.map(|fd| Unblock::new(fd)),- kill_on_drop,- status: None,+ inner,+ stdin: stdin_write.map(Async::new).transpose()?,+ stdout: stdout_read.map(Async::new).transpose()?,+ stderr: stderr_read.map(Async::new).transpose()?,})}}@@ -603,6 +553,8 @@ fn invalid_input_error() -> io::Error {#[cfg(test)]mod tests {+ use std::os::unix::process::ExitStatusExt as _;+use super::*;use futures_lite::AsyncWriteExt;@@ -646,6 +598,47 @@ mod tests {);}+ fn wait_until_gone(pid: u32) {+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);+ loop {+ let result = unsafe { libc::kill(pid as libc::pid_t, 0) };+ if result == -1 && io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH) {+ return;+ }+ assert!(+ std::time::Instant::now() < deadline,+ "process {pid} was not reaped"+ );+ std::thread::sleep(std::time::Duration::from_millis(10));+ }+ }++ #[test]+ fn test_drop_reaps_child() {+ smol::block_on(async {+ let child = Command::new("/usr/bin/true")+ .spawn()+ .expect("failed to spawn command");+ let pid = child.id();+ drop(child);+ wait_until_gone(pid);+ });+ }++ #[test]+ fn test_kill_on_drop_kills_and_reaps_child() {+ smol::block_on(async {+ let child = Command::new("/bin/sleep")+ .arg("60")+ .kill_on_drop(true)+ .spawn()+ .expect("failed to spawn command");+ let pid = child.id();+ drop(child);+ wait_until_gone(pid);+ });+ }+#[test]fn test_spawn_echo() {smol::block_on(async {