Description
I've discovered, that std::io::stdin/out/err()
streams unconditionally ignore EBADF
-like IO errors on all platforms. This is done by checking the read/write error in a handle_ebadf()
function.
Lines 123 to 128 in 1ccb50e
It appears, that this behavior was first introduced here a7bbd7d
The commit clearly has Windows in mind, where it appears the standard streams may be unavailable. But on Linux, the streams are expected to be always present, so there's no reason to ignore EBADF
in the first place, as it indicates that something is very wrong.
Not only that, but due to file descriptor reuse behavior on Unixes, if descriptors 0/1/2 are not open, sometimes the very next calls to open()
will allocate them. This means, that a program running without properly preallocated 0/1/2 descriptors may start happily println!()
-ing over its own sqlite database, or send private execution logs across a tcp connection.
So, if std::io::stdout/err()
happens to discover that something yanked the descriptors from under program's feet, the proper response is not to silently ignore EBADF
, but to panic(), before something else unwittingly allocated it with likely disastrous consequences.