Skip to content

Write to Closed stdout Succeeds #83904

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
5hir0kur0 opened this issue Apr 5, 2021 · 3 comments
Closed

Write to Closed stdout Succeeds #83904

5hir0kur0 opened this issue Apr 5, 2021 · 3 comments
Labels
C-bug Category: This is a bug.

Comments

@5hir0kur0
Copy link

I tried this code:

use std::io::Write;
use std::io;

fn main() {
    let stdout = io::stdout();
    let mut stdout = stdout.lock();
    let res = writeln!(&mut stdout, "Hello, World!");
    match res {
        Ok(_)  => eprintln!("success"),
        Err(_) => eprintln!("failure")
    }
}

I ran it with a closed stdout file descriptor from bash:

rustc test.rs
./test >&-

I expected to see this happen:
The program should print "failure" to stderr (and nothing to stdout).
Note that this behavior is actually what happens in other programs, such as /usr/bin/echo:

/usr/bin/echo asdf >&-
# prints: /usr/bin/echo: write error: Bad file descriptor

Instead, this happened:
The program prints "success" to stderr. Note that the writeln does in fact not succeed as the "Hello, World!" is not visible to the user on the terminal.

In my opinion this behavior is a bug since the Result of the writeln is Ok even though the user does not see the text.
Although it is somewhat of an edge case, other programs such as echo seem to be able to correctly detect this and report an error.

Meta

rustc --version --verbose:

rustc 1.50.0 (cb75ad5db 2021-02-10)
binary: rustc
commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
commit-date: 2021-02-10
host: x86_64-unknown-linux-gnu
release: 1.50.0
@5hir0kur0 5hir0kur0 added the C-bug Category: This is a bug. label Apr 5, 2021
@38
Copy link

38 commented Apr 5, 2021

I think this is by design. See this RFC https://rust-lang.github.io/rfcs/1014-stdout-existential-crisis.html

@5hir0kur0
Copy link
Author

Thank you. Ah, OK. I see. Then I will close this issue.

I think this is unfortunate, though. Maybe it would make sense to make this configurable or to have the macros that do not return a result such as println ignore this kind of error and only report it in write calls/macros.

@masklinn
Copy link
Contributor

masklinn commented May 9, 2021

@38 seems to me this is more of a side-effect of the RFC's implementation detail: the RFC only mentions output methods which do not return results: println!, print!, panic!, and assert! don't return a Result and will panic on IO errors. Making them return IO errors was judged inconvenient at the time, and thus them ignoring IO errors was more practical and convenient.

That's specifically not the case for write! though, or any explicit method of the Write traits. Getting proper error reporting is pretty much the entire point of using write! with standard streams, otherwise [e]print[ln]! is a strictly terser way to do the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants