-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Rust's stdio should not ignore EBADF error on non-windows platforms #47271
Copy link
Copy link
Open
Labels
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`Area: `std::io`, `std::fs`, `std::net` and `std::path`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`Area: `std::io`, `std::fs`, `std::net` and `std::path`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
I've discovered, that
std::io::stdin/out/err()streams unconditionally ignoreEBADF-like IO errors on all platforms. This is done by checking the read/write error in ahandle_ebadf()function.rust/src/libstd/io/stdio.rs
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
EBADFin 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 happilyprintln!()-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 ignoreEBADF, but to panic(), before something else unwittingly allocated it with likely disastrous consequences.