Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/shell-stdin-write-nonblocking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"shell": patch
"shell-js": patch
---

`Child.write` no longer blocks the main thread while the child isn't reading its stdin. The data is queued and written by a background thread per child, in call order. If a write fails (for example because the child exited), the error is returned by the next `write` call.
11 changes: 7 additions & 4 deletions plugins/shell/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,14 @@ pub fn stdin_write<R: Runtime>(
pid: ChildId,
buffer: Buffer,
) -> crate::Result<()> {
let bytes = match buffer {
Buffer::Text(t) => t.into_bytes(),
Buffer::Raw(r) => r,
};
// Only queues the data, so a child that isn't reading its stdin can't block the main
// thread or hold the children lock. Staying sync keeps the writes in call order.
if let Some(child) = shell.children.lock().unwrap().get_mut(&pid) {
match buffer {
Buffer::Text(t) => child.write(t.as_bytes())?,
Buffer::Raw(r) => child.write(&r)?,
}
child.write_queued(bytes)?;
}
Ok(())
}
Expand Down
114 changes: 113 additions & 1 deletion plugins/shell/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
io::{BufRead, BufReader, Write},
path::{Path, PathBuf},
process::{Command as StdCommand, Stdio},
sync::Arc,
sync::{Arc, Mutex, mpsc},
thread::{JoinHandle, spawn},
};

Expand Down Expand Up @@ -71,20 +71,62 @@ pub struct Command {
pub struct CommandChild {
inner: Arc<SharedChild>,
stdin_writer: PipeWriter,
stdin_queue: Option<StdinQueue>,
#[cfg(unix)]
process_group: bool,
#[cfg(windows)]
job: Option<job_object::JobObject>,
kill_on_drop: bool,
}

/// Background writer used by [`CommandChild::write_queued`].
#[derive(Debug)]
struct StdinQueue {
tx: mpsc::Sender<Vec<u8>>,
/// The error that stopped the writer thread, returned by the next queued write.
error: Arc<Mutex<Option<std::io::Error>>>,
}

impl CommandChild {
/// Writes to process stdin.
pub fn write(&mut self, buf: &[u8]) -> crate::Result<()> {
self.stdin_writer.write_all(buf)?;
Ok(())
}

/// Queues `buf` to be written to the process stdin by a background thread and returns
/// without waiting for the child to read it. Queued writes keep their order.
///
/// Write errors happen on the background thread, so they are returned by the next call.
pub(crate) fn write_queued(&mut self, buf: Vec<u8>) -> crate::Result<()> {
if self.stdin_queue.is_none() {
let mut writer = self.stdin_writer.try_clone()?;
let (tx, rx) = mpsc::channel::<Vec<u8>>();
let error = Arc::new(Mutex::new(None));
let error_ = error.clone();
// Exits once the child is dropped and the queue is drained, or on the first error.
spawn(move || {
for buf in rx {
if let Err(e) = writer.write_all(&buf) {
*error_.lock().unwrap() = Some(e);
break;
}
}
});
self.stdin_queue = Some(StdinQueue { tx, error });
}

let queue = self.stdin_queue.as_ref().unwrap();
if let Some(e) = queue.error.lock().unwrap().take() {
return Err(e.into());
}
queue
.tx
.send(buf)
.map_err(|_| std::io::Error::from(std::io::ErrorKind::BrokenPipe))?;
Ok(())
}

/// Sends a kill signal to the child and waits for it to exit.
/// With `process_group` enabled this kills the whole process group (POSIX) or job object (Windows).
pub fn kill(mut self) -> crate::Result<()> {
Expand Down Expand Up @@ -459,6 +501,7 @@ impl Command {
CommandChild {
inner: child,
stdin_writer,
stdin_queue: None,
#[cfg(unix)]
process_group,
#[cfg(windows)]
Expand Down Expand Up @@ -639,6 +682,75 @@ mod tests {
);
}

#[cfg(not(windows))]
#[test]
fn write_queued_does_not_wait_for_reader_and_keeps_order() {
// The child only starts reading stdin after a delay, and the first write is
// bigger than a pipe buffer, so a blocking write would wait for the sleep.
let cmd = Command::new("sh")
.args(["-c", "sleep 1; cat"])
.set_raw_out(true);
let (mut rx, mut child) = cmd.spawn().unwrap();

// Drain stdout on another thread so `cat` never stalls on a full stdout pipe.
let collector = std::thread::spawn(move || {
tauri::async_runtime::block_on(async move {
let mut out = Vec::new();
while let Some(event) = rx.recv().await {
match event {
CommandEvent::Stdout(line) => out.extend(line),
CommandEvent::Terminated(_) => break,
_ => {}
}
}
out
})
});

let big = vec![b'a'; 1024 * 1024];
let start = std::time::Instant::now();
child.write_queued(big.clone()).unwrap();
child.write_queued(b"second".to_vec()).unwrap();
let elapsed = start.elapsed();
// Closing stdin (by dropping the child) must still deliver everything queued first.
drop(child);
let out = collector.join().unwrap();

assert!(
elapsed < std::time::Duration::from_millis(500),
"write_queued blocked for {elapsed:?}"
);
let mut expected = big;
expected.extend_from_slice(b"second");
assert_eq!(out.len(), expected.len());
assert!(out == expected, "stdin data arrived out of order");
}

#[cfg(not(windows))]
#[test]
fn write_queued_reports_error_after_child_exits() {
let (mut rx, mut child) = Command::new("true").spawn().unwrap();
tauri::async_runtime::block_on(async move {
while let Some(event) = rx.recv().await {
if let CommandEvent::Terminated(_) = event {
break;
}
}
});

// The first write is queued; the writer thread then fails with a broken pipe,
// which a later write reports.
let mut result = child.write_queued(vec![b'x'; 1024 * 1024]);
for _ in 0..50 {
if result.is_err() {
break;
}
std::thread::sleep(std::time::Duration::from_millis(20));
result = child.write_queued(b"x".to_vec());
}
assert!(result.is_err());
}

#[cfg(not(windows))]
#[test]
fn test_cmd_spawn_output() {
Expand Down
Loading