Skip to content

update the std::io::pipe() example to comment deadlocks #1611

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

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 13 additions & 8 deletions content/Rust-1.87.0/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,25 @@ or platform-specific functions.
use std::process::Command;
use std::io::Read;

let (mut recv, send) = std::io::pipe()?;
let (mut reader, writer) = std::io::pipe()?;

let mut command = Command::new("path/to/bin")
// Both stdout and stderr will write to the same pipe, combining the two.
.stdout(send.try_clone()?)
.stderr(send)
.spawn()?;
let mut command = Command::new("path/to/bin");
// Both stdout and stderr will write to the same pipe, combining the two.
command.stdout(writer.try_clone()?);
command.stderr(writer);
let mut child = command.spawn()?;

// .read_to_end() will block until all pipe writers are closed, but the Command
// object is still holding two of them. Dropping it closes those writers and
// prevents a deadlock.
drop(command);

let mut output = Vec::new();
recv.read_to_end(&mut output)?;
reader.read_to_end(&mut output)?;

// It's important that we read from the pipe before the process exits, to avoid
// filling the OS buffers if the program emits too much output.
assert!(command.wait()?.success());
assert!(child.wait()?.success());
```

### Safe architecture intrinsics
Expand Down