Skip to content

Commit 6cacb52

Browse files
committed
Auto merge of #108980 - Enselic:println-and-broken-pipe, r=workingjubilee
Regression test `println!()` panic message on `ErrorKind::BrokenPipe` No existing test (that I could find) failed if the `panic!()` of the `println!()` family of functions was removed, or if its message was changed: https://github.com/rust-lang/rust/blob/104f4300cfddbd956e32820ef202a732f06ec848/library/std/src/io/stdio.rs#L1007-L1009 So add such a test. This is in preparation of adding a hint about the existence of [`unix_sigpipe`](#97889) if that is the reason for the panic. Even if we don't end up adding a hint, this is still a sensible test to have, I think. `@rustbot` label +A-testsuite +A-io +T-libs +O-unix
2 parents 500647f + 075a6bb commit 6cacb52

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

Diff for: tests/ui/process/println-with-broken-pipe.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// run-pass
2+
// check-run-results
3+
// ignore-windows
4+
// ignore-emscripten
5+
// ignore-fuchsia
6+
// ignore-horizon
7+
// ignore-android
8+
// normalize-stderr-test ".rs:\d+:\d+" -> ".rs:LL:CC"
9+
10+
// Test what the error message looks like when `println!()` panics because of
11+
// `std::io::ErrorKind::BrokenPipe`
12+
13+
#![feature(unix_sigpipe)]
14+
15+
use std::env;
16+
use std::process::{Command, Stdio};
17+
18+
#[unix_sigpipe = "sig_ign"]
19+
fn main() {
20+
let mut args = env::args();
21+
let me = args.next().unwrap();
22+
23+
if let Some(arg) = args.next() {
24+
// More than enough iterations to fill any pipe buffer. Normally this
25+
// loop will end with a panic more or less immediately.
26+
for _ in 0..65536 * 64 {
27+
println!("{arg}");
28+
}
29+
unreachable!("should have panicked because of BrokenPipe");
30+
}
31+
32+
// Set up a pipeline with a short-lived consumer and wait for it to finish.
33+
// This will produce the `println!()` panic message on stderr.
34+
let mut producer = Command::new(&me)
35+
.arg("this line shall appear exactly once on stdout")
36+
.env("RUST_BACKTRACE", "0")
37+
.stdout(Stdio::piped())
38+
.spawn()
39+
.unwrap();
40+
let mut consumer =
41+
Command::new("head").arg("-n1").stdin(producer.stdout.take().unwrap()).spawn().unwrap();
42+
consumer.wait().unwrap();
43+
producer.wait().unwrap();
44+
}

Diff for: tests/ui/process/println-with-broken-pipe.run.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
thread 'main' panicked at 'failed printing to stdout: Broken pipe (os error 32)', library/std/src/io/stdio.rs:LL:CC
2+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Diff for: tests/ui/process/println-with-broken-pipe.run.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this line shall appear exactly once on stdout

0 commit comments

Comments
 (0)