Repository navigation
cksum: report a write error on a closed stdout - #16234
ChihweiLHBird wants to merge 1 commit into
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 improve performance by 3.5%3 warnings, 1 note
Performance Changes
Tip Curious why performance improved? Comment 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()) | ||
| } | ||
|
|
e058e8e to
e9dc222
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
10 open issues
With the newstdout_writer()behavior, the Unix closed-stdout case may rely onLineWriter…stdout_writer()allocates aBox<dyn Write>each time, and these call sites create a fresh… · Newstdout_writer()allocates aBox<dyn Write>each time, and these call sites create a fresh… · Newstdout_writer()allocates aBox<dyn Write>each time, and these call sites create a fresh… · New The API relies on callers remembering toflush()and handle the result; however… · New The doc comment states Rust reopens stdout as/dev/nullwhen closed at startup, which is… · New The redirection>&-is not as explicit/portable as1>&-for closing stdout (fd 1). Using1>&-… Thesh -cscript string is difficult to read/maintain due to layered quoting and inline… · Newstdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function…stdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function…
Review effort: Lite
|
Binary size comparison: |
|
GNU testsuite comparison: |
e9dc222 to
953d978
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
8 open issues
With the newstdout_writer()behavior, the Unix closed-stdout case may rely onLineWriter… Makingchecksumimplysignalschanges the feature’s dependency surface and may force-build… · Newstdout_writer()returns a boxed trait object, so calling it for each report line can add repeated… · New UsingErrno::EBADFunqualified makes this function depend on an in-scopeErrnoimport and on… · New The redirection>&-is not as explicit/portable as1>&-for closing stdout (fd 1). Using1>&-… This doc comment currently describes Unix-specific behavior (reopen as/dev/null, EBADF) but… · Newstdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function…stdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function…
6 resolved since last review
The doc comment states Rust reopens stdout as/dev/nullwhen closed at startup, which is… The API relies on callers remembering toflush()and handle the result; however…stdout_writer()allocates aBox<dyn Write>each time, and these call sites create a fresh…stdout_writer()allocates aBox<dyn Write>each time, and these call sites create a fresh…stdout_writer()allocates aBox<dyn Write>each time, and these call sites create a fresh… Thesh -cscript string is difficult to read/maintain due to layered quoting and inline…
Review effort: Lite
| colors = [] | ||
| diagnostics = ["dep:ariadne"] | ||
| checksum = ["base64-simd", "quoting-style", "rustix/fs", "sum"] | ||
| checksum = ["base64-simd", "quoting-style", "rustix/fs", "signals", "sum"] |
| write_file_report( | ||
| io::stdout(), | ||
| stdout_writer(), | ||
| filename, | ||
| FileChecksumResult::CantOpen, |
|
|
||
| impl std::io::Write for ClosedStdout { | ||
| fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> { | ||
| Err(Errno::EBADF.into()) |
| /// 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. |
953d978 to
3f7fa69
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
12 open issues
WrappingClosedStdoutinLineWritercan still silently lose the EBADF error when the last write… · New With the newstdout_writer()behavior, the Unix closed-stdout case may rely onLineWriter…stdout_writer()returns a boxed trait object, which implies a heap allocation and dynamic… · New The doc comment implies this behavior generally, but the EBADF behavior is only active under… · New This assertion hard-codes the OS error stringBad file descriptor, which can be locale-dependent… · New UsingErrno::EBADFunqualified makes this function depend on an in-scopeErrnoimport and on…stdout_writer()returns a boxed trait object, so calling it for each report line can add repeated… Makingchecksumimplysignalschanges the feature’s dependency surface and may force-build… The redirection>&-is not as explicit/portable as1>&-for closing stdout (fd 1). Using1>&-… This doc comment currently describes Unix-specific behavior (reopen as/dev/null, EBADF) but…stdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function…stdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function…
Review effort: Lite
3f7fa69 to
7ed5723
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
12 open issues
With the newstdout_writer()behavior, the Unix closed-stdout case may rely onLineWriter…stdout_writer()explicitly documents a requirement to flush at the end to avoid losing errors,… · New Makingchecksumimplysignalscouples two features and can forcesignalsdependencies into… · NewLineWriterbuffers until a newline/flush; if a caller writes a final partial line (no\\n) and… · New UsingErrno::EBADFunqualified makes this function depend on an in-scopeErrnoimport and on…stdout_writer()returns a boxed trait object, so calling it for each report line can add repeated… Makingchecksumimplysignalschanges the feature’s dependency surface and may force-build… The redirection>&-is not as explicit/portable as1>&-for closing stdout (fd 1). Using1>&-…stdout_writer()is constructed anew at eachwrite_file_reportcall site (there are multiple in… · New This doc comment currently describes Unix-specific behavior (reopen as/dev/null, EBADF) but…stdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function…stdout_writer()always heap-allocates and uses dynamic dispatch (Box<dyn Write>). This function…
4 resolved since last review
WrappingClosedStdoutinLineWritercan still silently lose the EBADF error when the last write… This assertion hard-codes the OS error stringBad file descriptor, which can be locale-dependent… The doc comment implies this behavior generally, but the EBADF behavior is only active under…stdout_writer()returns a boxed trait object, which implies a heap allocation and dynamic…
Review effort: Lite
| }; | ||
|
|
||
| perform_checksum_computation(io::stdout(), opts, files)?; | ||
| perform_checksum_computation(stdout_writer(), opts, files)?; |
| colors = [] | ||
| diagnostics = ["dep:ariadne"] | ||
| checksum = ["base64-simd", "quoting-style", "rustix/fs", "sum"] | ||
| checksum = ["base64-simd", "quoting-style", "rustix/fs", "signals", "sum"] |
| /// 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> { | ||
| #[cfg(all(has_signals, unix))] | ||
| if signals::stdout_was_closed() { | ||
| /// A writer that fails like a closed file descriptor. | ||
| struct ClosedStdout; | ||
|
|
||
| impl std::io::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()) | ||
| } |
| let failed_open = || { | ||
| write_file_report( | ||
| io::stdout(), | ||
| stdout_writer(), |
7ed5723 to
eff3473
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.
|
Hi @sylvestre, I don't think Copilot lite is making much sense here. I believe I addressed all your comments but let me know if I missed anything. The Copilot Lite's flood of comments made this thread harder to read... |
eff3473 to
14302ec
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>
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.



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: