Skip to content

cksum: report a write error on a closed stdout - #16234

Merged
sylvestre merged 1 commit into
uutils:mainfrom
ChihweiLHBird:zhiwei/cksum-stdout-closed
Nov 27, 2026
Merged

sylvestre merged 1 commit into
uutils:mainfrom
ChihweiLHBird:zhiwei/cksum-stdout-closed

Conversation

@ChihweiLHBird

@ChihweiLHBird ChihweiLHBird commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a process starts with fd 1 closed, Rust opens /dev/null on it before main, so cksum, md5sum, sha*sum and b2sum lose their output and exit 0, also with --check. GNU reports write error: Bad file descriptor and exits 1. This PR adds uucore::signals::stdout_writer() next to stdout_was_closed(), which fails with EBADF when stdout was closed at startup, and uses it for the checksum lines and the --check report. Other utils can use it in separate PRs.

Example

echo hi | cksum >&-; echo $?

Before:

0

After, same as GNU:

cksum: write error: Bad file descriptor
1

Copilot AI balanced review requested due to automatic review settings November 7, 2026 03:13
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from d39ba06 to 4d94285 Compare November 7, 2026 03:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
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 in uucore::checksum that returns a writer which fails with EBADF when startup capture recorded stdout as closed.
  • Route checksum output in checksum_common through stdout_writer().
  • Add regression tests for cksum and md5sum to 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

Comment thread tests/by-util/test_cksum.rs Resolved
Comment thread tests/by-util/test_cksum.rs Resolved
Comment thread tests/by-util/test_md5sum.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 7, 2026 03:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from 4d94285 to 2884ae0 Compare November 7, 2026 03:44
Copilot AI balanced review requested due to automatic review settings November 7, 2026 03:44
@codspeed

codspeed Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning, 1 note

⚠️ 122 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

ℹ️ 1 benchmark has no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

✅ 415 untouched benchmarks
⏩ 70 skipped benchmarks1


Comparing ChihweiLHBird:zhiwei/cksum-stdout-closed (5ea8608) with main (932efe9)2

Open in CodSpeed

Footnotes

  1. 70 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (6dcc23e) during the generation of this report, so 932efe9 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from 2884ae0 to a702d21 Compare November 7, 2026 03:57
Copilot AI balanced review requested due to automatic review settings November 7, 2026 03:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Comment thread src/uu/checksum_common/src/lib.rs Resolved
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from a702d21 to 9ba4cbe Compare November 7, 2026 19:12
Copilot AI balanced review requested due to automatic review settings November 7, 2026 19:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
1 resolved since last review

Review effort: Lite

@sylvestre

Copy link
Copy Markdown
Contributor

clippy is sad :), and a bunch of jobs are failing

@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from 9ba4cbe to e058e8e Compare November 8, 2026 01:47
Copilot AI balanced review requested due to automatic review settings November 8, 2026 01:47
@ChihweiLHBird

Copy link
Copy Markdown
Contributor Author

@sylvestre Thanks! Both suggestions are done in now. --check now writes its report lines through the same writer, so md5sum -c sums >&- prints write error: Bad file descriptor and exits 1, with two new test cases. The writer is now uucore::signals::stdout_writer(), next to stdout_was_closed(), so other utils can use it in separate PRs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/uu/checksum_common/src/lib.rs Resolved
Comment thread src/uucore/src/lib/features/signals.rs Resolved
@ChihweiLHBird
ChihweiLHBird requested review from sylvestre and a balanced review from Copilot November 8, 2026 02:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

};

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)])
Comment thread src/uucore/src/lib/features/signals.rs Outdated
Comment on lines +825 to +847
/// 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())
Comment thread src/uucore/src/lib/features/signals.rs Outdated
Comment on lines +825 to +849
/// 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())
}

@sylvestre
sylvestre force-pushed the zhiwei/cksum-stdout-closed branch from 14302ec to a781aee Compare November 20, 2026 20:56
Copilot AI balanced review requested due to automatic review settings November 20, 2026 20:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oech3

oech3 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

I'm planning to deprecate all of std*_was_closed after #16299 to reduce hacks when new std was released. Would you close this PR?

@sylvestre
sylvestre force-pushed the zhiwei/cksum-stdout-closed branch from a781aee to d2e67a6 Compare November 21, 2026 14:33
Copilot AI balanced review requested due to automatic review settings November 21, 2026 14:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sylvestre
sylvestre force-pushed the zhiwei/cksum-stdout-closed branch from d2e67a6 to 18c2874 Compare November 22, 2026 07:17
Copilot AI balanced review requested due to automatic review settings November 22, 2026 07:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from 18c2874 to d5d550a Compare November 24, 2026 08:09
Copilot AI balanced review requested due to automatic review settings November 24, 2026 08:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sylvestre
sylvestre force-pushed the zhiwei/cksum-stdout-closed branch from d5d550a to 39c9234 Compare November 26, 2026 07:09
Copilot AI balanced review requested due to automatic review settings November 26, 2026 07:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from 39c9234 to 0bbc583 Compare November 26, 2026 07:36
Copilot AI balanced review requested due to automatic review settings November 26, 2026 07:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from 0bbc583 to 5ea8608 Compare November 27, 2026 11:15
Copilot AI balanced review requested due to automatic review settings November 27, 2026 11:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@sylvestre
sylvestre merged commit 309828d into uutils:main Nov 27, 2026
197 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

Comment thread src/uucore/src/lib/lib.rs
/// 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> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@oech3 oech3 Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ChihweiLHBird ChihweiLHBird Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honestly, I think we should not fix this until Rust itself was fixed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally understand your concern and appreciate it, and I can port this fix to the new stdlib as soon as they release it.

@ChihweiLHBird
ChihweiLHBird deleted the zhiwei/cksum-stdout-closed branch November 27, 2026 21:11
@oech3

oech3 commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

This broke (individual) build:

error[E0433]: cannot find type `Errno` in this scope
   --> src/uucore/src/lib/lib.rs:448:21
    |
448 |                 Err(Errno::EBADF.into())
    |                     ^^^^^ use of undeclared type `Errno`

@ChihweiLHBird

Copy link
Copy Markdown
Contributor Author

This broke (individual) build:

error[E0433]: cannot find type `Errno` in this scope
   --> src/uucore/src/lib/lib.rs:448:21
    |
448 |                 Err(Errno::EBADF.into())
    |                     ^^^^^ use of undeclared type `Errno`

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!

@oech3

oech3 commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

#16913

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants