Skip to main content

vtcode_bash_runner/
pipe.rs

1//! Async pipe-based process spawning with unified handle interface.
2//!
3//! This module provides helpers for spawning non-interactive processes using
4//! regular pipes for stdin/stdout/stderr, with proper process group management
5//! for reliable cleanup.
6//!
7//! Inspired by codex-rs/utils/pty pipe spawning patterns.
8
9use hashbrown::HashMap;
10use std::io::{self, ErrorKind};
11use std::path::Path;
12use std::process::Stdio;
13use std::sync::Arc;
14use std::sync::Mutex as StdMutex;
15use std::sync::atomic::AtomicBool;
16
17use anyhow::{Context, Result};
18use bytes::{Bytes, BytesMut};
19use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt, BufReader};
20use tokio::process::Command;
21use tokio::sync::{broadcast, mpsc, oneshot};
22use tokio::task::JoinHandle;
23
24use crate::process::{ChildTerminator, ProcessHandle, SpawnedProcess};
25use crate::process_group;
26
27/// Terminator for pipe-based child processes.
28struct PipeChildTerminator {
29    #[cfg(windows)]
30    pid: u32,
31    #[cfg(unix)]
32    process_group_id: u32,
33}
34
35impl ChildTerminator for PipeChildTerminator {
36    fn kill(&mut self) -> io::Result<()> {
37        #[cfg(unix)]
38        {
39            process_group::kill_process_group(self.process_group_id)
40        }
41
42        #[cfg(windows)]
43        {
44            process_group::kill_process(self.pid)
45        }
46
47        #[cfg(not(any(unix, windows)))]
48        {
49            Ok(())
50        }
51    }
52}
53
54const RELIABLE_OUTPUT_CHANNEL_CAPACITY: usize = 128;
55
56/// Read from an async reader and send chunks to both compatibility and
57/// optional lossless output channels. The compatibility broadcast is always
58/// independent: callers that only use the legacy receiver must never be able
59/// to stop the child-process pipe from being drained.
60async fn read_output_stream<R>(
61    mut reader: R,
62    output_tx: broadcast::Sender<Bytes>,
63    mut reliable_output_tx: Option<mpsc::Sender<Bytes>>,
64) where
65    R: AsyncRead + Unpin,
66{
67    // Read directly into a BytesMut so each chunk can be frozen without a
68    // separate Vec-to-Bytes copy.
69    let mut buf = BytesMut::with_capacity(65_536);
70    loop {
71        buf.clear();
72        match reader.read_buf(&mut buf).await {
73            Ok(0) => break,
74            Ok(n) => {
75                let chunk = buf.split_to(n).freeze();
76                let _ = output_tx.send(chunk.clone());
77                if let Some(sender) = reliable_output_tx.as_ref().cloned()
78                    && sender.send(chunk).await.is_err()
79                {
80                    // A lossless consumer is optional. Once it goes away,
81                    // continue draining the child stream for compatibility
82                    // subscribers instead of deadlocking the child.
83                    reliable_output_tx = None;
84                }
85            }
86            Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
87            Err(_) => break,
88        }
89    }
90}
91
92/// Stdin mode for pipe-based processes.
93#[derive(Clone, Copy)]
94pub enum PipeStdinMode {
95    /// Stdin is available as a pipe.
96    Piped,
97    /// Stdin is connected to /dev/null (immediate EOF).
98    Null,
99}
100
101/// Options for spawning a pipe-based process.
102#[derive(Clone)]
103pub struct PipeSpawnOptions {
104    /// The program to execute.
105    program: String,
106    /// Arguments to pass to the program.
107    args: Vec<String>,
108    /// Working directory for the process.
109    cwd: std::path::PathBuf,
110    /// Environment variables (if None, inherits from parent).
111    env: Option<HashMap<String, String>>,
112    /// Override for `argv[0]` (Unix only).
113    arg0: Option<String>,
114    /// Stdin mode.
115    stdin_mode: PipeStdinMode,
116    /// Enable the bounded lossless stream used by spoolers and
117    /// `wait_with_output`. Legacy callers should leave this disabled so an
118    /// unconsumed reliable receiver cannot apply backpressure.
119    lossless_output: bool,
120}
121
122impl PipeSpawnOptions {
123    /// Create new spawn options with default settings.
124    pub fn new(program: impl Into<String>, cwd: impl Into<std::path::PathBuf>) -> Self {
125        Self {
126            program: program.into(),
127            args: Vec::new(),
128            cwd: cwd.into(),
129            env: None,
130            arg0: None,
131            stdin_mode: PipeStdinMode::Piped,
132            lossless_output: false,
133        }
134    }
135
136    /// Add arguments.
137    pub fn args(mut self, args: impl IntoIterator<Item = impl Into<String>>) -> Self {
138        self.args = args.into_iter().map(Into::into).collect();
139        self
140    }
141
142    /// Set environment variables.
143    pub fn env(mut self, env: HashMap<String, String>) -> Self {
144        self.env = Some(env);
145        self
146    }
147
148    /// Set arg0 override (Unix only).
149    pub fn arg0(mut self, arg0: impl Into<String>) -> Self {
150        self.arg0 = Some(arg0.into());
151        self
152    }
153
154    /// Set stdin mode.
155    pub fn stdin_mode(mut self, mode: PipeStdinMode) -> Self {
156        self.stdin_mode = mode;
157        self
158    }
159
160    /// Enable the lossless output stream for a consumer that drains it.
161    pub fn lossless_output(mut self, enabled: bool) -> Self {
162        self.lossless_output = enabled;
163        self
164    }
165}
166
167/// Spawn a process using regular pipes, with configurable options.
168async fn spawn_process_internal(opts: PipeSpawnOptions) -> Result<SpawnedProcess> {
169    if opts.program.is_empty() {
170        anyhow::bail!("missing program for pipe spawn");
171    }
172
173    let mut command = Command::new(&opts.program);
174
175    #[cfg(unix)]
176    if let Some(ref arg0) = opts.arg0 {
177        command.arg0(arg0);
178    }
179
180    #[cfg(unix)]
181    #[expect(
182        unsafe_code,
183        reason = "detach_from_tty only calls setsid/setpgid via safe nix wrappers to detach the child from the controlling terminal; it is a pure process-group operation with no undefined behavior"
184    )]
185    // SAFETY: `pre_exec` runs the closure in the forked child before `exec`,
186    // so only async-signal-safe operations are permitted. `detach_from_tty`
187    // calls `setsid()` (falling back to `setpgid(0, 0)` on EPERM) through safe
188    // `nix` wrappers — both are async-signal-safe POSIX calls that perform no
189    // allocation, no locking, and no mutable aliasing of process memory.
190    unsafe {
191        command.pre_exec(process_group::detach_from_tty);
192    }
193
194    #[cfg(not(unix))]
195    let _ = &opts.arg0;
196
197    command.current_dir(&opts.cwd);
198
199    // Handle environment
200    if let Some(ref env) = opts.env {
201        command.env_clear();
202        for (key, value) in env {
203            command.env(key, value);
204        }
205    }
206
207    for arg in &opts.args {
208        command.arg(arg);
209    }
210
211    match opts.stdin_mode {
212        PipeStdinMode::Piped => {
213            command.stdin(Stdio::piped());
214        }
215        PipeStdinMode::Null => {
216            command.stdin(Stdio::null());
217        }
218    }
219    command.stdout(Stdio::piped());
220    command.stderr(Stdio::piped());
221
222    let mut child = command.spawn().context("failed to spawn pipe process")?;
223    let pid = child.id().ok_or_else(|| io::Error::other("missing child pid"))?;
224
225    #[cfg(unix)]
226    let process_group_id = pid;
227
228    let stdin = child.stdin.take();
229    let stdout = child.stdout.take();
230    let stderr = child.stderr.take();
231
232    let (writer_tx, mut writer_rx) = mpsc::channel::<Vec<u8>>(128);
233    let (output_tx, _) = broadcast::channel::<Bytes>(256);
234    let initial_output_rx = output_tx.subscribe();
235    let (reliable_output_tx, reliable_output_rx) = mpsc::channel::<Bytes>(RELIABLE_OUTPUT_CHANNEL_CAPACITY);
236    let reliable_output_enabled = opts.lossless_output;
237
238    // Spawn writer task
239    let writer_handle = if let Some(stdin) = stdin {
240        let writer = Arc::new(tokio::sync::Mutex::new(stdin));
241        tokio::spawn(async move {
242            while let Some(bytes) = writer_rx.recv().await {
243                let mut guard = writer.lock().await;
244                let _ = guard.write_all(&bytes).await;
245                let _ = guard.flush().await;
246            }
247        })
248    } else {
249        drop(writer_rx);
250        tokio::spawn(async {})
251    };
252
253    // Spawn reader tasks for stdout and stderr
254    let stdout_handle = stdout.map(|stdout| {
255        let output_tx = output_tx.clone();
256        let reliable_output_tx = opts.lossless_output.then(|| reliable_output_tx.clone());
257        tokio::spawn(async move {
258            read_output_stream(BufReader::new(stdout), output_tx, reliable_output_tx).await;
259        })
260    });
261
262    let stderr_handle = stderr.map(|stderr| {
263        let output_tx = output_tx.clone();
264        let reliable_output_tx = opts.lossless_output.then(|| reliable_output_tx.clone());
265        tokio::spawn(async move {
266            read_output_stream(BufReader::new(stderr), output_tx, reliable_output_tx).await;
267        })
268    });
269    drop(reliable_output_tx);
270
271    let mut reader_abort_handles = Vec::new();
272    if let Some(ref handle) = stdout_handle {
273        reader_abort_handles.push(handle.abort_handle());
274    }
275    if let Some(ref handle) = stderr_handle {
276        reader_abort_handles.push(handle.abort_handle());
277    }
278
279    let reader_handle = tokio::spawn(async move {
280        if let Some(handle) = stdout_handle {
281            let _ = handle.await;
282        }
283        if let Some(handle) = stderr_handle {
284            let _ = handle.await;
285        }
286    });
287
288    // Spawn wait task
289    let (exit_tx, exit_rx) = oneshot::channel::<i32>();
290    let exit_status = Arc::new(AtomicBool::new(false));
291    let wait_exit_status = Arc::clone(&exit_status);
292    let exit_code = Arc::new(StdMutex::new(None));
293    let wait_exit_code = Arc::clone(&exit_code);
294
295    let wait_handle: JoinHandle<()> = tokio::spawn(async move {
296        let code = match child.wait().await {
297            Ok(status) => status.code().unwrap_or(-1),
298            Err(_) => -1,
299        };
300        wait_exit_status.store(true, std::sync::atomic::Ordering::SeqCst);
301        if let Ok(mut guard) = wait_exit_code.lock() {
302            *guard = Some(code);
303        }
304        let _ = exit_tx.send(code);
305    });
306
307    let (handle, output_rx) = ProcessHandle::new(
308        writer_tx,
309        output_tx,
310        initial_output_rx,
311        Box::new(PipeChildTerminator {
312            #[cfg(windows)]
313            pid,
314            #[cfg(unix)]
315            process_group_id,
316        }),
317        reader_handle,
318        reader_abort_handles,
319        writer_handle,
320        wait_handle,
321        exit_status,
322        exit_code,
323        None,
324    );
325
326    Ok(SpawnedProcess {
327        session: handle,
328        process_id: pid,
329        output_rx,
330        reliable_output_rx,
331        reliable_output_enabled,
332        exit_rx,
333    })
334}
335
336/// Spawn a process using regular pipes (no PTY), returning handles for stdin, output, and exit.
337///
338/// # Example
339/// ```ignore
340/// use vtcode_bash_runner::pipe::spawn_process;
341/// use hashbrown::HashMap;
342/// use std::path::Path;
343///
344/// let env: HashMap<String, String> = std::env::vars().collect();
345/// let spawned = spawn_process("echo", &["hello".into()], Path::new("."), &env, &None).await?;
346/// let output_rx = spawned.output_rx;
347/// let exit_code = spawned.exit_rx.await?;
348/// ```
349pub async fn spawn_process(
350    program: &str,
351    args: &[String],
352    cwd: &Path,
353    env: &HashMap<String, String>,
354    arg0: &Option<String>,
355) -> Result<SpawnedProcess> {
356    let opts = PipeSpawnOptions {
357        program: program.to_string(),
358        args: args.to_vec(),
359        cwd: cwd.to_path_buf(),
360        env: Some(env.clone()),
361        arg0: arg0.clone(),
362        stdin_mode: PipeStdinMode::Piped,
363        lossless_output: false,
364    };
365    spawn_process_internal(opts).await
366}
367
368/// Spawn a process using regular pipes, but close stdin immediately.
369///
370/// This is useful for commands that should see EOF on stdin immediately.
371pub async fn spawn_process_no_stdin(
372    program: &str,
373    args: &[String],
374    cwd: &Path,
375    env: &HashMap<String, String>,
376    arg0: &Option<String>,
377) -> Result<SpawnedProcess> {
378    let opts = PipeSpawnOptions {
379        program: program.to_string(),
380        args: args.to_vec(),
381        cwd: cwd.to_path_buf(),
382        env: Some(env.clone()),
383        arg0: arg0.clone(),
384        stdin_mode: PipeStdinMode::Null,
385        lossless_output: false,
386    };
387    spawn_process_internal(opts).await
388}
389
390/// Spawn a process with full options control.
391pub async fn spawn_process_with_options(opts: PipeSpawnOptions) -> Result<SpawnedProcess> {
392    spawn_process_internal(opts).await
393}
394
395#[cfg(test)]
396mod tests {
397    use super::*;
398    use assert_fs::TempDir;
399
400    fn find_echo_command() -> Option<(String, Vec<String>)> {
401        #[cfg(windows)]
402        {
403            Some(("cmd.exe".to_string(), vec!["/C".to_string(), "echo".to_string()]))
404        }
405        #[cfg(not(windows))]
406        {
407            Some(("echo".to_string(), vec![]))
408        }
409    }
410
411    #[tokio::test]
412    async fn test_spawn_process_echo() -> Result<()> {
413        let Some((program, mut base_args)) = find_echo_command() else {
414            return Ok(());
415        };
416
417        base_args.push("hello".to_string());
418
419        let env: HashMap<String, String> = std::env::vars().collect();
420        let spawned = spawn_process(&program, &base_args, Path::new("."), &env, &None).await?;
421
422        let exit_code = spawned.exit_rx.await.unwrap_or(-1);
423        assert_eq!(exit_code, 0);
424
425        Ok(())
426    }
427
428    #[tokio::test]
429    async fn test_spawn_options_builder() {
430        let opts = PipeSpawnOptions::new("echo", ".")
431            .args(["hello", "world"])
432            .stdin_mode(PipeStdinMode::Null);
433
434        assert_eq!(opts.program, "echo");
435        assert_eq!(opts.args, vec!["hello", "world"]);
436        assert!(matches!(opts.stdin_mode, PipeStdinMode::Null));
437    }
438
439    #[cfg(unix)]
440    #[tokio::test]
441    async fn test_spawn_process_detaches_from_tty() {
442        let dir = TempDir::new().expect("tempdir");
443        let env: HashMap<String, String> = HashMap::new();
444
445        let spawned = spawn_process("sh", &["-c".into(), "echo ok".into()], dir.path(), &env, &None)
446            .await
447            .expect("spawn");
448
449        let exit_code = spawned.exit_rx.await.unwrap_or(-1);
450        assert_eq!(exit_code, 0);
451    }
452}