Repository navigation
cksum: report a write error on a closed stdout - #16234
Conversation
d39ba06 to
4d94285
Compare
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
7 open issues
The docs don’t mention that the closed-stdout behavior is conditional onfeature = \"signals\"… · New ReturningBox<dyn Write>introduces dynamic dispatch and a heap allocation for a hot writer used… · New ReturningBox<dyn Write>introduces dynamic dispatch and a heap allocation for a hot writer used… · New Same platform-gating issue as intest_md5sum: the production logic excludes Fuchsia, but this… · New Same platform-gating issue as intest_md5sum: the production logic excludes Fuchsia, but this… · New The implementation intentionally excludestarget_os = \"fuchsia\"for the closed-stdout… · New This pulls innixAPIs just to construct an EBADFio::Error. If you want to keep this module’s… · New
What changed in this PR
Align checksum utilities’ behavior with GNU when stdout (fd 1) is closed at process start, ensuring the tools detect EBADF and exit non-zero instead of silently discarding output.
Changes:
- Add a
stdout_writer()helper inuucore::checksumthat returns a writer which fails with EBADF when startup capture recorded stdout as closed. - Route checksum output in
checksum_commonthroughstdout_writer(). - Add regression tests for
cksumandmd5sumto verify correct error message and exit code when stdout is closed.
| File | Description |
|---|---|
| tests/by-util/test_md5sum.rs | Adds a regression test covering closed-stdout behavior for md5sum. |
| tests/by-util/test_cksum.rs | Adds parameterized regression tests covering closed-stdout behavior across multiple cksum modes. |
| src/uucore/src/lib/features/checksum/output.rs | Introduces stdout_writer() that can intentionally fail writes when stdout was closed at startup. |
| src/uucore/src/lib/features/checksum/mod.rs | Exposes the new output module. |
| src/uu/checksum_common/src/lib.rs | Switches checksum output to use stdout_writer() instead of io::stdout(). |
Review effort: Lite
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the change addressing silently discarded checksum output when stdout is closed.
7 open issues
The implementation intentionally excludestarget_os = \"fuchsia\"for the closed-stdout… Same platform-gating issue as intest_md5sum: the production logic excludes Fuchsia, but this… Same platform-gating issue as intest_md5sum: the production logic excludes Fuchsia, but this… ReturningBox<dyn Write>introduces dynamic dispatch and a heap allocation for a hot writer used… ReturningBox<dyn Write>introduces dynamic dispatch and a heap allocation for a hot writer used… The docs don’t mention that the closed-stdout behavior is conditional onfeature = \"signals\"… This pulls innixAPIs just to construct an EBADFio::Error. If you want to keep this module’s…
Review effort: Balanced
4d94285 to
2884ae0
Compare
Merging this PR will not alter performance1 warning, 1 note
Comparing Footnotes
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the checksum change that reports write errors when stdout is closed at startup.
7 open issues
The implementation intentionally excludestarget_os = \"fuchsia\"for the closed-stdout… Same platform-gating issue as intest_md5sum: the production logic excludes Fuchsia, but this… Same platform-gating issue as intest_md5sum: the production logic excludes Fuchsia, but this… ReturningBox<dyn Write>introduces dynamic dispatch and a heap allocation for a hot writer used… ReturningBox<dyn Write>introduces dynamic dispatch and a heap allocation for a hot writer used… The docs don’t mention that the closed-stdout behavior is conditional onfeature = \"signals\"… This pulls innixAPIs just to construct an EBADFio::Error. If you want to keep this module’s…
Review effort: Balanced
2884ae0 to
a702d21
Compare
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the checksum stdout error-handling changes.
1 open issue
6 resolved since last review
The implementation intentionally excludestarget_os = \"fuchsia\"for the closed-stdout… Same platform-gating issue as intest_md5sum: the production logic excludes Fuchsia, but this… ReturningBox<dyn Write>introduces dynamic dispatch and a heap allocation for a hot writer used… ReturningBox<dyn Write>introduces dynamic dispatch and a heap allocation for a hot writer used… The docs don’t mention that the closed-stdout behavior is conditional onfeature = \"signals\"… This pulls innixAPIs just to construct an EBADFio::Error. If you want to keep this module’s…
Review effort: Balanced
a702d21 to
9ba4cbe
Compare
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
4 open issues
WrappingClosedStdoutinLineWriterplus havingflush()returnOk(())can suppress the… · New The doc comment claims this writer has “the same line buffer as stdout,” butstd::io::stdout()… · New ReturningBox<dyn Write>introduces a heap allocation and virtual dispatch on every write call… · New ReturningBox<dyn Write>introduces a heap allocation and virtual dispatch on every write call… · New
1 resolved since last review
Review effort: Lite
|
clippy is sad :), and a bunch of jobs are failing |
9ba4cbe to
e058e8e
Compare
|
@sylvestre Thanks! Both suggestions are done in now. |
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
3 open issues
stdout_writer()can be a buffered writer (per its doc comment), and currentlychecksum_main… · Newstdout_writer()returns a boxed trait object on Unix, which implies at least a heap allocation +… · New WrappingClosedStdoutinLineWritercan mask write errors for writes that do not include a… · New
4 resolved since last review
WrappingClosedStdoutinLineWriterplus havingflush()returnOk(())can suppress the… ReturningBox<dyn Write>introduces a heap allocation and virtual dispatch on every write call… ReturningBox<dyn Write>introduces a heap allocation and virtual dispatch on every write call… The doc comment claims this writer has “the same line buffer as stdout,” butstd::io::stdout()…
Review effort: Lite
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
4 open issues
With the newstdout_writer()behavior, the Unix closed-stdout case may rely onLineWriter… · New The redirection>&-is not as explicit/portable as1>&-for closing stdout (fd 1). Using1>&-… · Newstdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function… · Newstdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function… · New
3 resolved since last review
Review effort: Lite
| }; | ||
|
|
||
| perform_checksum_computation(io::stdout(), opts, files)?; | ||
| perform_checksum_computation(stdout_writer(), opts, files)?; |
| &format!("SHA256 (missing) = {}\n", "0".repeat(64)), | ||
| ); | ||
| ts.cmd("sh") | ||
| .args(&["-c", &format!("\"$0\" {} {args} >&-", ts.util_name)]) |
| /// Return `io::stdout()`, or, if stdout was closed at startup (Rust then | ||
| /// reopens it as `/dev/null`), a line-buffered writer that fails with EBADF. | ||
| /// Flush it at the end, or the error of a partial last line is lost. | ||
| pub fn stdout_writer() -> Box<dyn Write> { | ||
| #[cfg(unix)] | ||
| if stdout_was_closed() { | ||
| /// A writer that fails like a closed file descriptor. | ||
| struct ClosedStdout; | ||
|
|
||
| impl Write for ClosedStdout { | ||
| fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> { | ||
| Err(Errno::EBADF.into()) | ||
| } | ||
|
|
||
| // With no output, a closed stdout is not an error. | ||
| fn flush(&mut self) -> std::io::Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| return Box::new(std::io::LineWriter::new(ClosedStdout)); | ||
| } | ||
| Box::new(std::io::stdout()) |
| /// Return `io::stdout()`, or, if stdout was closed at startup (Rust then | ||
| /// reopens it as `/dev/null`), a line-buffered writer that fails with EBADF. | ||
| /// Flush it at the end, or the error of a partial last line is lost. | ||
| pub fn stdout_writer() -> Box<dyn Write> { | ||
| #[cfg(unix)] | ||
| if stdout_was_closed() { | ||
| /// A writer that fails like a closed file descriptor. | ||
| struct ClosedStdout; | ||
|
|
||
| impl Write for ClosedStdout { | ||
| fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> { | ||
| Err(Errno::EBADF.into()) | ||
| } | ||
|
|
||
| // With no output, a closed stdout is not an error. | ||
| fn flush(&mut self) -> std::io::Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| return Box::new(std::io::LineWriter::new(ClosedStdout)); | ||
| } | ||
| Box::new(std::io::stdout()) | ||
| } | ||
|
|
14302ec to
a781aee
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
I'm planning to deprecate all of |
a781aee to
d2e67a6
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
d2e67a6 to
18c2874
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
18c2874 to
d5d550a
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
d5d550a to
39c9234
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
39c9234 to
0bbc583
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
Rust opens /dev/null on a closed fd 1 before main, so the checksum utils lose their output and exit 0, also in --check mode. Add uucore::stdout_writer(), which fails with EBADF in that case where the signals module is built, and use it for the checksum lines and the check report. Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
0bbc583 to
5ea8608
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
|
Thanks for your PR |
| /// Return `io::stdout()`, or, if stdout was closed at startup (Rust then | ||
| /// reopens it as `/dev/null`), a line-buffered writer that fails with EBADF. | ||
| /// Flush it at the end, or the error of a partial last line is lost. | ||
| pub fn stdout_writer() -> Box<dyn std::io::Write> { |
There was a problem hiding this comment.
I really don't want to spread this hack. There is a cleaner solution at #16234 (comment) .
Is it able to drop this fn from uucore?
There was a problem hiding this comment.
Also note that stdi*_was_closed raises EBADF before actual read/write. Foe example,
command >&- should not raise until you type something, but we does.
There was a problem hiding this comment.
Thanks for the feedback. Feel free to adapt it to the newer way in your PR or let me know if you need any help for that.
This was for the cksum bug fix, and the regession test serves that purpose, so you can check if the test will pass in your fix in the new way.
There was a problem hiding this comment.
To be honestly, I think we should not fix this until Rust itself was fixed.
There was a problem hiding this comment.
I totally understand your concern and appreciate it, and I can port this fix to the new stdlib as soon as they release it.
|
This broke (individual) build: |
This seems to be caused by removal of the import in a later commit. I can work on a fix later today when I get back to home if it's still not fixed at the time. Thanks for pointing it out! |



Summary
When a process starts with fd 1 closed, Rust opens
/dev/nullon it beforemain, so cksum, md5sum, sha*sum and b2sum lose their output and exit 0, also with--check. GNU reportswrite error: Bad file descriptorand exits 1. This PR addsuucore::signals::stdout_writer()next tostdout_was_closed(), which fails with EBADF when stdout was closed at startup, and uses it for the checksum lines and the--checkreport. Other utils can use it in separate PRs.Example
Before:
After, same as GNU: