jrgerber / dmidecode-rs

dmidecode command written in Rust
MIT License
16 stars 8 forks source link

Panic when piping output to head #60

Open mulark opened 2 years ago

mulark commented 2 years ago
sudo ./target/debug/dmidecode | head
# dmidecode-rs 0.2.0
Getting SMBIOS data from sysfs.
SMBIOS 3.2.0 present.
Table at 0x000E8D60.

Handle 0x0000, DMI type 0, 26 bytes
BIOS Information
        Vendor: American Megatrends Inc.
        Version: F6b
        Release Date: 09/17/2019
thread 'main' panicked at 'failed printing to stdout: Broken pipe (os error 32)', library/std/src/io/stdio.rs:935:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
mulark commented 2 years ago

https://github.com/rust-lang/rust/issues/46016

jrgerber commented 2 years ago

rust-lang/rust#46016

Interesting. Does this reproduce with head every time for you? I was just reading the discussion and noted these responses which sound simple enough? "Until someone implements and merges unignore_sigpipe(), your best bet will be to use the write!() macro instead of the print*!() family of macros, and then handle errors appropriately."

write! isn't hard: see here for one example of how to use it in production. Note that literally the only difference as far as the app is concerned is the gratuitous use of ? to handle potential errors.

mulark commented 2 years ago

A potential alternative is to remove the sigpipe handler using libc, which is already a part of our dependency tree.

    unsafe {
        libc::signal(libc::SIGPIPE, libc::SIG_DFL);
    }