Skip to content

fix(cmd): Don't deadlock between pipes #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2020
Merged
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
56 changes: 44 additions & 12 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::ffi;
use std::io;
use std::io::Write;
use std::io::{Read, Write};
use std::path;
use std::process;

Expand Down Expand Up @@ -416,7 +416,48 @@ impl Command {
/// assert!(output.status.success());
/// ```
pub fn output(&mut self) -> io::Result<process::Output> {
self.spawn()?.wait_with_output()
let spawn = self.spawn()?;
Self::wait_with_input_output(spawn, self.stdin.clone())
}

/// If `input`, write it to `child`'s stdin while also reading `child`'s
/// stdout and stderr, then wait on `child` and return its status and output.
///
/// This was lifted from `std::process::Child::wait_with_output` and modified
/// to also write to stdin.
fn wait_with_input_output(
mut child: process::Child,
input: Option<Vec<u8>>,
) -> io::Result<process::Output> {
let stdin = input.and_then(|i| {
child
.stdin
.take()
.map(|mut stdin| std::thread::spawn(move || stdin.write_all(&i)))
});
fn read<R>(mut input: R) -> std::thread::JoinHandle<io::Result<Vec<u8>>>
where
R: Read + Send + 'static,
{
std::thread::spawn(move || {
let mut ret = Vec::new();
input.read_to_end(&mut ret).map(|_| ret)
})
}
let stdout = child.stdout.take().map(read);
let stderr = child.stderr.take().map(read);

// Finish writing stdin before waiting, because waiting drops stdin.
stdin.and_then(|t| t.join().unwrap().ok());
let status = child.wait()?;
let stdout = stdout.and_then(|t| t.join().unwrap().ok());
let stderr = stderr.and_then(|t| t.join().unwrap().ok());

Ok(process::Output {
status: status,
stdout: stdout.unwrap_or_default(),
stderr: stderr.unwrap_or_default(),
})
}

fn spawn(&mut self) -> io::Result<process::Child> {
Expand All @@ -425,16 +466,7 @@ impl Command {
self.cmd.stdout(process::Stdio::piped());
self.cmd.stderr(process::Stdio::piped());

let mut spawned = self.cmd.spawn()?;

if let Some(buffer) = self.stdin.as_ref() {
spawned
.stdin
.as_mut()
.expect("Couldn't get mut ref to command stdin")
.write_all(&buffer)?;
}
Ok(spawned)
self.cmd.spawn()
}
}

Expand Down