Skip to content

Commit

Permalink
fix: Prevent deadlocks due to filled pipe on stderr (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Nov 30, 2021
1 parent 066a591 commit 30a144a
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,18 @@ impl CommandExt for Command {
.stderr(Stdio::piped())
.spawn()?;

let status = process.wait()?;

// Read up to 256 bytes from stderr.
// Consume all stderr - it's open just for a few programs which can't handle it being closed.
use std::io::Read;
let mut stderr = vec![0; 256];
let len = process
.stderr
.take()
.and_then(|mut err| err.read(&mut stderr).ok())
.unwrap_or(0);
let mut stderr_src = process.stderr.take().expect("piped stderr");

let len = stderr_src.read(&mut stderr).unwrap_or(0);
stderr.truncate(len);

// consume the rest to avoid blocking
std::io::copy(&mut stderr_src, &mut std::io::sink()).ok();

let status = process.wait()?;
Ok(Output {
status,
stderr,
Expand Down

0 comments on commit 30a144a

Please sign in to comment.