Skip to content

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

Open
ChihweiLHBird wants to merge 1 commit into
uutils:mainfrom
ChihweiLHBird:zhiwei/cksum-stdout-closed
Open

ChihweiLHBird wants to merge 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 improve performance by 3.5%

3 warnings, 1 note

⚠️ 121 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

⚠️ 3 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

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

⚡ 3 improved benchmarks
✅ 410 untouched benchmarks
⏩ 70 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ CPU Simulation df_deep_directory 930.1 µs 891 µs +4.39%
⚡ CPU Simulation dd_copy_with_seek 5 ms 4.9 ms +3.1%
⚡ CPU Simulation dd_copy_with_skip 4.9 ms 4.8 ms +3.01%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ChihweiLHBird:zhiwei/cksum-stdout-closed (a781aee) with main (2775292)

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. ↩

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())
}

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

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/uucore/src/lib/lib.rs Resolved
Comment thread src/uucore/src/lib/lib.rs Resolved
Comment thread tests/by-util/test_cksum.rs Resolved
@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.56 MB (+176 KB, +0.11%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/tail/follow-name is no longer failing!
Congrats! The gnu test tests/tail/pid-pipe is no longer failing!

@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from e9dc222 to 953d978 Compare November 9, 2026 05:00
Copilot AI balanced review requested due to automatic review settings November 9, 2026 05:00

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

8 open issues
6 resolved since last review

Review effort: Lite

Comment thread src/uucore/Cargo.toml
colors = []
diagnostics = ["dep:ariadne"]
checksum = ["base64-simd", "quoting-style", "rustix/fs", "sum"]
checksum = ["base64-simd", "quoting-style", "rustix/fs", "signals", "sum"]
Comment on lines 603 to 606
write_file_report(
io::stdout(),
stdout_writer(),
filename,
FileChecksumResult::CantOpen,
Comment thread src/uucore/src/lib/lib.rs

impl std::io::Write for ClosedStdout {
fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {
Err(Errno::EBADF.into())
Comment thread src/uucore/src/lib/lib.rs
Comment on lines +450 to +452
/// 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.
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from 953d978 to 3f7fa69 Compare November 18, 2026 04:46
Copilot AI balanced review requested due to automatic review settings November 18, 2026 04:46

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

12 open issues

Review effort: Lite

Comment thread src/uucore/src/lib/lib.rs Resolved
Comment thread src/uucore/src/lib/lib.rs Resolved
Comment thread tests/by-util/test_cksum.rs Resolved
@sylvestre
sylvestre force-pushed the zhiwei/cksum-stdout-closed branch from 3f7fa69 to 7ed5723 Compare November 19, 2026 11:57
Copilot AI balanced review requested due to automatic review settings November 19, 2026 11:57

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

12 open issues
4 resolved since last review

Review effort: Lite

};

perform_checksum_computation(io::stdout(), opts, files)?;
perform_checksum_computation(stdout_writer(), opts, files)?;
Comment thread src/uucore/Cargo.toml
colors = []
diagnostics = ["dep:ariadne"]
checksum = ["base64-simd", "quoting-style", "rustix/fs", "sum"]
checksum = ["base64-simd", "quoting-style", "rustix/fs", "signals", "sum"]
Comment thread src/uucore/src/lib/lib.rs
Comment on lines +450 to +473
/// 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(),
@ChihweiLHBird
ChihweiLHBird force-pushed the zhiwei/cksum-stdout-closed branch from 7ed5723 to eff3473 Compare November 19, 2026 17:23
Copilot AI balanced review requested due to automatic review settings November 19, 2026 17: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.

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

Copy link
Copy Markdown
Contributor Author

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...

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

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>
@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.

This branch has not been deployed

No deployments
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.

3 participants