command.rs

  1use std::ffi::OsStr;
  2
  3#[cfg(target_os = "windows")]
  4const CREATE_NO_WINDOW: u32 = 0x0800_0000_u32;
  5
  6#[cfg(target_os = "windows")]
  7pub fn new_std_command(program: impl AsRef<OsStr>) -> std::process::Command {
  8    use std::os::windows::process::CommandExt;
  9
 10    let mut command = std::process::Command::new(program);
 11    command.creation_flags(CREATE_NO_WINDOW);
 12    command
 13}
 14
 15#[cfg(not(target_os = "windows"))]
 16pub fn new_std_command(program: impl AsRef<OsStr>) -> std::process::Command {
 17    std::process::Command::new(program)
 18}
 19
 20#[cfg(target_os = "windows")]
 21pub fn new_smol_command(program: impl AsRef<OsStr>) -> smol::process::Command {
 22    use smol::process::windows::CommandExt;
 23
 24    let mut command = smol::process::Command::new(program);
 25    command.creation_flags(CREATE_NO_WINDOW);
 26    command
 27}
 28
 29#[cfg(target_os = "macos")]
 30pub fn new_smol_command(program: impl AsRef<OsStr>) -> smol::process::Command {
 31    use std::os::unix::process::CommandExt;
 32
 33    // Create a std::process::Command first so we can use pre_exec
 34    let mut std_cmd = std::process::Command::new(program);
 35
 36    // WORKAROUND: Reset exception ports before exec to prevent inheritance of
 37    // crash handler exception ports. Due to a timing issue, child processes can
 38    // inherit the parent's exception ports before they're fully stabilized,
 39    // which can block child process spawning.
 40    // See: https://github.com/zed-industries/zed/issues/36754
 41    unsafe {
 42        std_cmd.pre_exec(|| {
 43            // Reset all exception ports to system defaults for this task.
 44            // This prevents the child from inheriting the parent's crash handler
 45            // exception ports.
 46            reset_exception_ports();
 47            Ok(())
 48        });
 49    }
 50
 51    // Convert to async_process::Command via From trait
 52    smol::process::Command::from(std_cmd)
 53}
 54
 55#[cfg(all(not(target_os = "windows"), not(target_os = "macos")))]
 56pub fn new_smol_command(program: impl AsRef<OsStr>) -> smol::process::Command {
 57    smol::process::Command::new(program)
 58}
 59
 60#[cfg(target_os = "macos")]
 61fn reset_exception_ports() {
 62    use mach2::exception_types::{
 63        EXC_MASK_ALL, EXCEPTION_DEFAULT, exception_behavior_t, exception_mask_t,
 64    };
 65    use mach2::kern_return::{KERN_SUCCESS, kern_return_t};
 66    use mach2::mach_types::task_t;
 67    use mach2::port::{MACH_PORT_NULL, mach_port_t};
 68    use mach2::thread_status::{THREAD_STATE_NONE, thread_state_flavor_t};
 69    use mach2::traps::mach_task_self;
 70
 71    // FFI binding for task_set_exception_ports (not exposed by mach2 crate)
 72    unsafe extern "C" {
 73        fn task_set_exception_ports(
 74            task: task_t,
 75            exception_mask: exception_mask_t,
 76            new_port: mach_port_t,
 77            behavior: exception_behavior_t,
 78            new_flavor: thread_state_flavor_t,
 79        ) -> kern_return_t;
 80    }
 81
 82    unsafe {
 83        let task = mach_task_self();
 84        // Reset all exception ports to MACH_PORT_NULL (system default)
 85        // This prevents the child process from inheriting the parent's crash handler
 86        let kr = task_set_exception_ports(
 87            task,
 88            EXC_MASK_ALL,
 89            MACH_PORT_NULL,
 90            EXCEPTION_DEFAULT as exception_behavior_t,
 91            THREAD_STATE_NONE,
 92        );
 93
 94        if kr != KERN_SUCCESS {
 95            // Log but don't fail - the process can still work without this workaround
 96            eprintln!(
 97                "Warning: failed to reset exception ports in child process (kern_return: {})",
 98                kr
 99            );
100        }
101    }
102}