Skip to content

uucore: add allocation-failure handling - #14480

Draft
anastygnome wants to merge 1 commit into
uutils:mainfrom
anastygnome:tsort3
Draft

uucore: add allocation-failure handling#14480
anastygnome wants to merge 1 commit into
uutils:mainfrom
anastygnome:tsort3

Conversation

@anastygnome

@anastygnome anastygnome commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Currently, an allocation failure in the utilities can bubble into Rust’s default failure path and panic or abort with a generic message.

memory allocation of ... bytes failed
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Aborted                    (core dumped) ./tsort /dev/full

std::alloc::set_alloc_error_hook would allow uutils to customize allocation-failure handling, but it is still unstable and tracked in rust-lang/rust#51245.

This PR provides the stable equivalent by installing a wrapper around System.
It mimics gnu error behaviour, exiting with error code 1 like GNU coreutils.

this PR also provides for localisation, and

  #[uucore::main(alloc_error_exit_code = N)]

which supports custom shell return codes.

to simulate such an example, try

 prlimit --as=67108864 --rss=67108864 tsort /dev/full

@codspeed-hq

codspeed-hq Bot commented Sep 9, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 10.88%

⚡ 2 improved benchmarks
✅ 350 untouched benchmarks
⏩ 65 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation true_consecutive_calls 350.2 ns 294.7 ns +18.85%
Simulation hostname_ip_lookup[100000] 178 µs 172.1 µs +3.44%

Tip

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


Comparing anastygnome:tsort3 (8db7cae) with main (855e80a)2

Open in CodSpeed

Footnotes

  1. 65 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 (2c0e1b9) during the generation of this report, so 855e80a was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@lhecker

lhecker commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Is there another tool prone to OOM aside from sort? Why doesn't it use try_reserve, etc.?

@anastygnome

anastygnome commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Is there another tool prone to OOM aside from sort? Why doesn't it use try_reserve, etc.?

Any allocating util can fall to this.
This goes deeper than try_reserve, this is for all allocations ever made by any util, (or any std-linking rust code really.) ofc, one should try to handle them locally, this is just a global backstop.

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skip an intermittent issue tests/date/resolution (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/tail/tail-n0f (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/seq/seq-epipe is now being skipped but was previously passing.

@anastygnome

Copy link
Copy Markdown
Contributor Author

@sylvestre can you run the failing check? We're getting rate limited.

@anastygnome

Copy link
Copy Markdown
Contributor Author

Well that's a flaky CI run.

@oech3

oech3 commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

I don't want to mask issues by this way which can be fixd by try_reserve.

@oech3

oech3 commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Can we patch uutils/coreutils to use different allocatior (e.g. mimalloc) easily at a fork after this PR was merged?

@anastygnome

Copy link
Copy Markdown
Contributor Author

I don't want to mask issues by this way which can be fixd by try_reserve.

This cannot be done if you use some struct like btreeset (see ptx for example) that do not allow for try_reserve.
Hence the proposal.

I could make it to be usable with mimalloc (or another one really) but it would become complicated.
I propose we defer this for now

@anastygnome
anastygnome marked this pull request as draft September 10, 2026 02:34
@oech3

oech3 commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

This cannot be done if you use some struct like btreeset (see ptx for example) that do not allow for try_reserve.

should be fixed at std.

@xtqqczze

Copy link
Copy Markdown
Contributor

This cannot be done if you use some struct like btreeset (see ptx for example) that do not allow for try_reserve.

should be fixed at std.

That would be blocked on rust-lang/rust#82766.

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