Skip to content

Introduce a wrapper on std::io::Stdin that checks for closed STDIN before reading - #15835

Open
RenjiSann wants to merge 4 commits into
uutils:mainfrom
RenjiSann:feature-ustdin
Open

RenjiSann wants to merge 4 commits into
uutils:mainfrom
RenjiSann:feature-ustdin

Conversation

@RenjiSann

@RenjiSann RenjiSann commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Also uses it in base32 and derivatives, cksum and derivatives

related to #10153

@oech3

oech3 commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

I think something like let stdout = stdout().map_err(|_| EBADF_PROVIDER)? is simpler.

Comment thread src/uucore/src/lib/mods/io.rs Resolved
@xtqqczze

Copy link
Copy Markdown
Collaborator

Relates to: #12210.

Comment thread src/uucore/src/lib/features/stdio.rs Resolved
Comment thread src/uucore/src/lib/features/stdio.rs Resolved
Comment thread src/uucore/src/lib/features/stdio.rs Resolved
Comment thread src/uucore/src/lib/features/stdio.rs Resolved
@RenjiSann
RenjiSann force-pushed the feature-ustdin branch 5 times, most recently from dbdcd7e to 1776a3d Compare October 25, 2026 22:03
@codspeed

codspeed Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 4%

1 warning

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

❌ 5 regressed benchmarks
✅ 110 untouched benchmarks
⏩ 360 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ cksum_default 32.9 ms 34.5 ms -4.51%
❌ cksum_crc32b 33.2 ms 34.7 ms -4.39%
❌ cksum_multiple_files 49.6 ms 51.6 ms -3.8%
❌ cksum_raw_output 33.5 ms 34.7 ms -3.67%
❌ cksum_crc 33.2 ms 34.5 ms -3.61%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing RenjiSann:feature-ustdin (b66c2e7) with main (bbef561)

Open in CodSpeed

Footnotes

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

@RenjiSann
RenjiSann force-pushed the feature-ustdin branch 2 times, most recently from 7d4cfe4 to a4accfa Compare October 25, 2026 23:17
@RenjiSann
RenjiSann removed the request for review from sylvestre October 26, 2026 01:08
Comment thread src/uu/base32/Cargo.toml Resolved
Comment thread src/uucore/src/lib/features/stdio.rs Resolved
Comment thread src/uucore/Cargo.toml Resolved
Comment thread src/uu/csplit/src/csplit.rs Resolved
@xtqqczze

Copy link
Copy Markdown
Collaborator

It might be better to have a complete reimplementation instead of delegating io::Stdin. That would require using a StdinRaw as the inner instead.

@oech3

oech3 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

It is very sad that we need to re-implement std::io, but yes, it is.

@RenjiSann

Copy link
Copy Markdown
Collaborator Author

dang it

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for this PR!
Close, but the Read impl is duplicated between UStdin and UStdinLock, and csplit() now returns Ok on error, which its docs don't say. Details are inline.

Comment thread src/uu/csplit/src/csplit.rs Resolved
Comment thread src/uu/csplit/src/csplit.rs Resolved
Comment thread src/uucore/src/lib/features/stdio.rs Resolved
Comment thread src/uucore/src/lib/features/stdio.rs Resolved

#[cfg(unix)]
if self.close_stdin {
let close_stdin = || {

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.

please add a SAFETY comment, like the other pre_exec calls below

@RenjiSann RenjiSann Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Isn't the comment just below (line 2019) enough ?

@RenjiSann

Copy link
Copy Markdown
Collaborator Author

@sylvestre I removed the fix for csplit, since it requires a bigger rework than only "replace stdin with ustdin", falling a bit outside of the scope of this PR.

}

/// Used for implementing [`std::io::Read`] for [`UStdin`] and [`UStdinLock`].
macro_rules! impl_read_stdin_closed {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Personally, I'd prefer repeated implementation (like the standard library does) over using macro here, especially as it is only repeated twice.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I originally repeated the instrumentation. I introduced the macro in response to sylvestre's comment

I have no strong opinion about this, I'm fine either way.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see. Given that we recommend against using macros in CONTRIBUTING.md, I would have followed the Rule of Three in this case.

@oech3

oech3 commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

I have an simpler idea: if rustix::io::read reports EBADF for write-only stdin, we should reopen stdin as write-only before Rust std's runtime opens /dev/null at constructor and use RawReader always.

Then we don't need to check that stdin was closed.

@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.31 MB (+64 KB, +0.04%)

Significant per-binary changes:
  printenv     1.09 MB ->    1.15 MB  (+60 KB, +5.36%)

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/env/printenv is no longer failing!

@oech3

oech3 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

One more reason we need to discard current approach is #12160 .

@oech3

oech3 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Good news for us! std will start catching EBADF for write-only stdin in the future: rust-lang/rust#47271 (comment) .

The std's change + #15835 (comment) allows to deprecate std*_was_closed(). I think this PR can be closed.

@xtqqczze

Copy link
Copy Markdown
Collaborator

This PR should certainly not be closed on the grounds of functionality that has yet to be implemented in the standard library.

@oech3

oech3 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Closed stdio things has no actual consumers (it is actually non-POSIX). We can wait.

@oech3

oech3 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Upstream (std) merged the change to main.

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.

4 participants