rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
5.85k stars 860 forks source link

rustfmt_diff: normal pager usage sent BrokenPipe causing unwrap() to panic #6172

Closed mhelsley closed 1 month ago

mhelsley commented 1 month ago

I ran fmt --check via cargo anticipating a substantial amount of rustfmt_diff output. Though it's an incorrect usage of the command I believe it should not cause a panic due to, as best I can tell, a broken pipe signal not being anticipated by the diff emitter code. I seem to recall that a broken pipe signal is normal with command line tools when the piped output is "cancelled" by the user of a "pager", like less, for example.

$ clear ; cargo fmt -- --check src/entities/*.rs | less

With my current code it normally produces a lot of rustfmt_diff output. So I then re-ran piping into less and scrolled only partially through. I then scrolled back to the top and, realizing my usage error, I quit -- to rewrite my command line and re-run it -- and triggered the stack dump below. I didn't see more than about half the rustfmt_diff output at most so rustfmt_diff was likely waiting and expecting to send more output buffers through the pipe when I quit less to make some changes to my command line.

thread 'main' panicked at src/tools/rustfmt/src/rustfmt_diff.rs:169:38:
called `Result::unwrap()` on an `Err` value: Os { code: 32, kind: BrokenPipe, message: "Broken pipe" }
stack backtrace:
   0:     0x76125b98c892 - std::backtrace_rs::backtrace::libunwind::trace::he4ee80166a02c846
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x76125b98c892 - std::backtrace_rs::backtrace::trace_unsynchronized::h476faccf57e88641
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x76125b98c892 - std::sys_common::backtrace::_print_fmt::h430c922a77e7a59c
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x76125b98c892 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hffecb437d922f988
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x76125b9dda6c - core::fmt::rt::Argument::fmt::hf3df69369399bfa9
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/fmt/rt.rs:142:9
   5:     0x76125b9dda6c - core::fmt::write::hd9a8d7d029f9ea1a
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/fmt/mod.rs:1153:17
   6:     0x76125b98178f - std::io::Write::write_fmt::h0e1226b2b8d973fe
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/io/mod.rs:1843:15
   7:     0x76125b98c664 - std::sys_common::backtrace::_print::hd2df4a083f6e69b8
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x76125b98c664 - std::sys_common::backtrace::print::he907f6ad7eee41cb
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x76125b98f35b - std::panicking::default_hook::{{closure}}::h3926193b61c9ca9b
  10:     0x76125b98f0b3 - std::panicking::default_hook::h25ba2457dea68e65
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:292:9
  11:     0x76125e82845d - std[e4dfbc2c3f4b09f1]::panicking::update_hook::<alloc[1adba907b9db1888]::boxed::Box<rustc_driver_impl[24a943716c49befe]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x76125b98fac0 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h022ca2c0d8c21c9e
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/alloc/src/boxed.rs:2034:9
  13:     0x76125b98fac0 - std::panicking::rust_panic_with_hook::h0ad14d90dcf5224f
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:783:13
  14:     0x76125b98f802 - std::panicking::begin_panic_handler::{{closure}}::h4a1838a06f542647
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:657:13
  15:     0x76125b98cd66 - std::sys_common::backtrace::__rust_end_short_backtrace::h77cc4dc3567ca904
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x76125b98f534 - rust_begin_unwind
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:645:5
  17:     0x76125b9d9f85 - core::panicking::panic_fmt::h940d4fd01a4b4fd1
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:72:14
  18:     0x76125b9da633 - core::result::unwrap_failed::h5119205a73b72b0d
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/result.rs:1654:5
  19:     0x61c03eeb93b8 - <rustfmt_nightly[dcf68724dbc99c0f]::emitter::diff::DiffEmitter as rustfmt_nightly[dcf68724dbc99c0f]::emitter::Emitter>::emit_formatted_file
  20:     0x61c03edb5fb2 - <rustfmt_nightly[dcf68724dbc99c0f]::Session<std[e4dfbc2c3f4b09f1]::io::stdio::Stdout>>::format_input_inner::{closure#0}
  21:     0x61c03edc9ae1 - rustfmt[d89bf27429b693cc]::format_and_emit_report::<std[e4dfbc2c3f4b09f1]::io::stdio::Stdout>
  22:     0x61c03edc80e4 - rustfmt[d89bf27429b693cc]::execute
  23:     0x61c03edc3ead - rustfmt[d89bf27429b693cc]::main
  24:     0x61c03edaf4e3 - std[e4dfbc2c3f4b09f1]::sys_common::backtrace::__rust_begin_short_backtrace::<fn(), ()>
  25:     0x61c03edb17a9 - std[e4dfbc2c3f4b09f1]::rt::lang_start::<()>::{closure#0}
  26:     0x76125b972203 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h52f5991f9ab8b369
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:284:13
  27:     0x76125b972203 - std::panicking::try::do_call::h0ac4bee9a397a1bf
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
  28:     0x76125b972203 - std::panicking::try::hc005decaf198d0ed
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
  29:     0x76125b972203 - std::panic::catch_unwind::hb0f967d870b2a382
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
  30:     0x76125b972203 - std::rt::lang_start_internal::{{closure}}::hd140b84b0efe534b
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/rt.rs:148:48
  31:     0x76125b972203 - std::panicking::try::do_call::h1ddfaf1d0d576c38
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:552:40
  32:     0x76125b972203 - std::panicking::try::hdd4bdf855547659f
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:516:19
  33:     0x76125b972203 - std::panic::catch_unwind::h276ba91c7706110c
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panic.rs:146:14
  34:     0x76125b972203 - std::rt::lang_start_internal::h103c42a9c4e95084
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/rt.rs:148:20
  35:     0x61c03edcaca5 - main
  36:     0x76125b428150 - __libc_start_call_main
                               at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
  37:     0x76125b428209 - __libc_start_main_impl
                               at ./csu/../csu/libc-start.c:360:3
  38:     0x61c03ed9e269 - <unknown>
  39:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rustfmt/issues/new?labels=bug

note: rustc 1.78.0 (9b00956e5 2024-04-29) running on x86_64-unknown-linux-gnu

query stack during panic:
end of query stack

It looks like the most relevant lines of the stack output are:

 18:     0x76125b9da633 - core::result::unwrap_failed::h5119205a73b72b0d
                               at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/result.rs:1654:5
 19:     0x61c03eeb93b8 - <rustfmt_nightly[dcf68724dbc99c0f]::emitter::diff::DiffEmitter as rustfmt_nightly[dcf68724dbc99c0f]::emitter::Emitter>::emit_formatted_file
ytmimi commented 1 month ago

Thanks for the report. I think this is duplicate of #2926