Skip to content

env: preserve closed standard streams when starting a command - #17130

Open
limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/shared-version-output
Open

limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/shared-version-output

Conversation

@limadog9

@limadog9 limadog9 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

When the caller closes stdout, Rust can reopen it before env starts. A child command can then write successfully and silently lose its output. Mark the runtime's replacement standard descriptors close-on-exec so the child inherits the same closed streams as its caller.

The regression tests cover all three standard streams. With stdout closed, a shell can still read input and exit successfully if it never writes. If it does write, the shell gets the write error. This matches GNU env 9.12 on Linux, including when uutils is built with Rust 1.102 nightly.

All 130 env tests passed on Linux. Ten checksum cases in GNU 9.12's misc/io-errors.sh now pass when launched through env, bringing the failure count for that script from 142 to 132.

Closes #17129.

@codspeed

codspeed Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by ×2

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

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

⚡ 4 improved benchmarks
✅ 419 untouched benchmarks
⏩ 78 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Memory socket_addrs_direct[100000] 68 KB 8.7 KB ×7.9
⚡ Memory hostname_ip_lookup[100000] 160.3 KB 100.9 KB +58.88%
⚡ CPU Simulation socket_addrs_direct[100000] 132.2 µs 105.2 µs +25.62%
⚡ CPU Simulation hostname_ip_lookup[100000] 574.9 µs 545.1 µs +5.48%

Tip

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


Comparing limadog9:fix/shared-version-output (57dbdea) with main (4e94937)

Open in CodSpeed

Footnotes

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

@oech3

oech3 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Please don't fix closed stdout things. we auto-achieve this after std was updated.
Also this is not compatible with GNU. See #16929 and rust-lang/rust#166117 .

Comment thread src/uu/env/src/env.rs
});

#[cfg(not(target_os = "fuchsia"))]
uucore::signals::preserve_closed_stdio_on_exec()

@oech3 oech3 Dec 4, 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.

I don't know how test on the CI, but this approach raises EBADF before actual I?O happens e.g. cat >&- (input is terminal and you did not type anything yet). std itself's fix is absolutely needed.

@limadog9
limadog9 force-pushed the fix/shared-version-output branch from 5f15449 to ad30cbf Compare December 4, 2026 04:04
@limadog9 limadog9 changed the title uucore: report closed stdout when printing the version env: preserve closed standard streams when starting a command Dec 4, 2026
@limadog9

limadog9 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for pointing this out. I dropped the --version workaround and narrowed the PR to env. Even on the latest Rust nightly, env can still give a child writable stdout after the caller closed it.

I checked the timing too. With the fix, the child can read input and exit successfully without writing. If it does write, it gets an error at that point. That matches what I saw with GNU env.

I’ve squashed and pushed the update.

@oech3

This comment was marked as outdated.

@oech3

oech3 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Does it mean #16929 + nightly , or nightly only?

@limadog9
limadog9 force-pushed the fix/shared-version-output branch from ad30cbf to 4b82351 Compare December 4, 2026 04:27
Mark the Rust runtime's replacement descriptors close-on-exec so that the child inherits closed standard streams and handles its own I/O errors.
@limadog9
limadog9 force-pushed the fix/shared-version-output branch from 4b82351 to 57dbdea Compare December 4, 2026 04:43

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.

env: child commands can write to stdout after the caller closed it

2 participants