sort: handle compression subprocess failures #14945
<!-- gnu-comment-bot -->
GNU testsuite comparison:
Skip an intermittent issue tests/misc/usage_vs_getopt (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/pr/options is no longer failing!
@mattsu2020 You need to open an issue to prevent duplication of work.
same block as above, could be dedup in a small helper, no?
please extract that into a function rather than an immediately called closure
could you please move this to uucore::signals and use nix like the other helpers there? it doesn't really belong in merge.rs
this ignores SIGPIPE for the whole process, so sort --compress-program=gzip big | head -1 now errors on stdout instead of dying quietly like GNU, no?
also gzip inherits the ignored SIGPIPE through exec
please also check stderr, only checking the exit code is weak
the sleep makes this timing dependent, do we need it?
I'll take care of it.
I'll fix that code.
I'll move that code to uucore::signals.
I've updated the implementation to avoid ignoring SIGPIPE process-wide.
I've updated the code to check for errors.
I deleted it.
<!-- __CODSPEED_PERFORMANCE_REPORT_COMMENT__ -->
Merging this PR will degrade performance by 46.73%
<details>
<summary><b>2 warnings, 1 note</b></summary>
:warning: 127 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
:warning: 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
:information_source: 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
</details>
⚡ 1 improved benchmark ❌ 2 regressed benchmarks ✅ 412 untouched benchmarks ⏩ 74 skipped benchmarks[^skipped]
[!WARNING]
Please fix the performance issues or acknowledge them on CodSpeed.
Performance Changes
| | Mode | Benchmark | BASE | HEAD | Efficiency |
| --- | ---- | --------- | ------ | ------ | ---------- |
| ❌ | Memory | [` socket_addrs_direct[100000] ](https://app.codspeed.io/uutils/coreutils/branches/mattsu2020%3Asort-compress-proc.sh?uri=src%2Fuu%2Fhostname%2Fbenches%2Fhostname_bench.rs%3A%3Asocket_addrs_direct%255B100000%255D&runnerMode=Memory&utm_source=github&utm_medium=comment-v2&utm_content=benchmark) | 8.6 KB | 68 KB | -87.29% |
| ❌ | Memory | [ hostname_ip_lookup[100000] ](https://app.codspeed.io/uutils/coreutils/branches/mattsu2020%3Asort-compress-proc.sh?uri=src%2Fuu%2Fhostname%2Fbenches%2Fhostname_bench.rs%3A%3Ahostname_ip_lookup%255B100000%255D&runnerMode=Memory&utm_source=github&utm_medium=comment-v2&utm_content=benchmark) | 100.9 KB | 160.3 KB | -37.06% |
| ⚡ | Memory | timeout_enforced | 167.5 KB | 88.7 KB | +88.88% |
[!TIP]
Investigate this regression by commenting @codspeedbot fix this regression` on this PR, or directly use the CodSpeed MCP with your agent.
---
<sub>Comparing <code>mattsu2020:sort-compress-proc.sh</code> (5e1fe6e) with <code>main</code> (ce58468)[^unexpected-base]</sub>
<a href="https://app.codspeed.io/uutils/coreutils/branches/mattsu2020%3Asort-compress-proc.sh?utm_source=github&utm_medium=comment-v2&utm_content=button">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://codspeed.io/pr-report/open-in-codspeed-dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://codspeed.io/pr-report/open-in-codspeed-light.svg">
<img alt="Open in CodSpeed" src="https://codspeed.io/pr-report/open-in-codspeed-light.svg" width="169" height="32">
</picture>
</a>
[^skipped]: 74 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.
[^unexpected-base]: No successful run was found on <code>main</code> (f02ef8e) during the generation of this report, so ce58468 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.
significant perf regressions here
this does an fcntl on every write, could you please set it once in create() instead?
that's 4+ syscalls per buffered write (mask swap, 2 sigpending, restore), could we block SIGPIPE once around the whole tmp file write instead?
this test is quite long and complex for what it checks, could it be simplified?
write + chmod of the compressor script is repeated 4 times, could be a small helper, no?
Since changes can be made to “create,” I'll handle it.
That code can be consolidated.
I think it's possible to keep it simple.
I'll simplify the implementation
these #!/bin/sh scripts will break on android (no /bin/sh there), test_compress_fail is skipped on android for that, probably the Android CI failure, no?
same three scripts as test_compressor_failure_after_input / _terminated_by_signal / _failure_after_output, could be dedup, no?
re-exec of the test binary, unwraps, env var modes... quite complex for a unit test, could it be an integration test in test_sort.rs instead?
fixed it.
Changed /bin/sh handling for Android.
Consolidated similar logic.
fixed it
sylvestre
mattsu2020
Compression subprocesses that exit early can terminate sort with SIGPIPE, and intermediate merge write failures can return status 1 instead of 2. Handle broken pipes and buffered write errors, reject abnormal child exits, and wait for compression and decompression children on error paths. Restore child waiting when SIGCHLD is inherited as ignored, while preserving normal SIGPIPE behavior for stdout.
Adds regression tests for early exits, failures after consuming all input or producing output, signal termination, and intermediate merge failures. The sort regression tests pass on macOS and Linux. The GNU shell test itself has not been run.
The current CodSpeed comparison mixes Rust 1.98.1 and 1.99.0 between BASE and HEAD. Paired Linux/aarch64 runs using Rust 1.99.0 did not reproduce the reported large sort and split slowdowns; a matched-toolchain CodSpeed comparison is still needed.
Refs #14705 (sort/sort-compress-proc.sh).
Closes #14951