Skip to content

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

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

RenjiSann wants to merge 6 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, and csplit

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 Outdated
fn err_if_stdin_closed() -> io::Result<()> {
#[cfg(feature = "signals")]
if signals::stdin_was_closed() {
return Err(io::Error::other(translate!("error-bad-file-descriptor")));

@oech3 oech3 Oct 25, 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.

rustix::io::Errno::BADF.into()

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'd agree, but the error is not localized if I do this :(

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 was considering that OS provides translated error message corresponding with errno via glibc.

@xtqqczze

Copy link
Copy Markdown
Collaborator

Relates to: #12210.

Comment thread src/uucore/src/lib/features/stdio.rs
Comment thread src/uucore/src/lib/features/stdio.rs
Comment thread src/uucore/src/lib/features/stdio.rs
}
}

pub fn ustdin() -> UStdin {

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'd probably just name this stdin, and likewise for UStdin

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.

why not follow the naming of other wrappers of uucore (like UResult, UError, etc...) ?

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.

why not follow the naming of other wrappers of uucore (like UResult, UError, etc...) ?

I’m not particularly fond of the naming of those types either, but they’re very common return types, so disambiguation may be necessary. That doesn’t apply in this case, though.

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.

The name ustdin at least should be stdin.

@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

Merging this PR will improve performance by 3.41%

1 warning, 1 note

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

⚡ 2 improved benchmarks
✅ 399 untouched benchmarks
⏩ 70 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ CPU Simulation sort_long_line[10000] 1.1 ms 1 ms +3.74%
⚡ CPU Simulation cksum_crc32b 33.3 ms 32.3 ms +3.08%

Tip

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


Comparing RenjiSann:feature-ustdin (63ba0b3) with main (1d6ca65)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 (0776385) during the generation of this report, so 1d6ca65 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@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 Outdated
Comment thread src/uucore/src/lib/features/stdio.rs Outdated
Comment thread src/uucore/Cargo.toml
selinux = ["dep:selinux", "libc"]
smack = ["xattr"]
signals = ["nix/fs", "nix/signal", "nix/poll"]
stdio = []

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.

Suggested change
stdio = []
stdio = ["signals"]

we should enable the signals feature here, and not directly in the checksum feature

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.

Not sure about that. I made the stdio feature be usable without signals, in case we ever need to further extend the capacities of the wrapper. The idea is to be able to use ustdin() everywhere in the uutils, no matter the availability of signals

fs::{File, remove_file},
io::{BufRead, BufWriter, Write},
};
use std::fs::{File, remove_file};

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.

Going forward, it would be better to keep unrelated style changes in a separate commit.

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