Skip to content
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

Don't panic if output stream is closed early #69

Open
Qubasa opened this issue Apr 10, 2019 · 1 comment
Open

Don't panic if output stream is closed early #69

Qubasa opened this issue Apr 10, 2019 · 1 comment
Labels
bug c: querying Features/bugs in nix-locate, the program to query the database

Comments

@Qubasa
Copy link

Qubasa commented Apr 10, 2019

Issue description

nix-locate crashes if you try to pipe the results in bash.
Only breaks if there is more then one result returned by nix-locate.

Steps to reproduce

nix-locate libQt5Xml.so.5 | head -n 1

Technical details

$ nix-locate libQt5Xml.so.5 | head -1
robomongo.out 0 s /nix/store/arnj25h57nqjixwc4nx12ad6fsz8jls1-robo3t-1.1.1/lib/robo3t/lib/libQt5Xml.so.5
thread 'main' panicked at 'failed printing to stdout: Broken pipe (os error 32)', libstd/io/stdio.rs:692:9
note: Run with RUST_BACKTRACE=1 for a backtrace.

Please run nix-shell -p nix-info --run "nix-info -m" and paste the
results.

  • system: "x86_64-linux"
  • host os: Linux 4.19.17, NixOS, 18.09.1985.749a3a0d00b (Jellyfish)
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.1.3
  • channels(root): "nixos-18.09.1985.749a3a0d00b, nixos-unstable-19.03pre166987.bc41317e243"
  • nixpkgs: /nix/var/nix/profiles/per-user/root/channels/nixos
@jameysharp
Copy link
Contributor

This symptom was reported to rust-lang/rust#46016 1+ year ago, and although Rust still doesn't provide a way to change this behavior, there are some suggested solutions there for applications which are affected like this.

The portable way to make the error go away is to replace println! with writeln! (similarly for print!) and check the returned Result. In this case the failing calls are in fn locate(). First get a handle for stdout:

    let stdout = std::io::stdout();
    let mut handle = stdout.lock();

Then replace each println!(...) with something like:

if let Err(_) = writeln!(handle, ...) { return Ok(()); }

(because nix-locate should just silently exit in this case; it isn't really an error that should be propagated up with ?.)

Would you like to write a patch for this? I'm not the maintainer so I can't promise it would be merged, but personally, I certainly think this is worth fixing.

@bennofs bennofs added bug c: querying Features/bugs in nix-locate, the program to query the database labels Apr 11, 2020
@bennofs bennofs changed the title Failed printing to stdout: Broken pipe (os error 32) Don't panic if output stream is closed early Apr 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug c: querying Features/bugs in nix-locate, the program to query the database
Projects
None yet
Development

No branches or pull requests

3 participants