Repository navigation
Tracking issue for the OOM hook (alloc_error_hook) #51245
Copy link
Copy link
Open
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsB-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
PR #50880 added an API to override the std OOM handler, similarly to the panic hook. This was discussed previously in issue #49668, after PR #50144 moved OOM handling out of the Alloc/GlobalAlloc traits. The API is somewhat similar to what existed before PR #42727 removed it without an explanation. This issue tracks the stabilization of this API.
Defined in the std::alloc module:
pub fn set_oom_hook(hook: fn(Layout) -> !);
pub fn take_oom_hook() -> fn(Layout) -> !;pub fn set_alloc_error_hook(hook: fn(Layout));
pub fn take_alloc_error_hook() -> fn(Layout);CC @rust-lang/libs, @SimonSapin
Unresolved questions
-
Name of the functions. The API before rustc: Implement the #[global_allocator] attribute #42727 usedMake the OOM hook return_handler, I made it_hookin OOM handling changes #50880 because that's the terminology used for the panic hook (OTOH, the panic hook returns, contrary to the OOM hook).()rather than!#51264 - Should this move to its own module, or stay in
std::alloc? - Interaction with unwinding.
alloc::alloc::oomis marked#[rustc_allocator_nounwind], so theoretically, the hook shouldn't panic (except when panic=abort). Yet if the hook does panic, unwinding seems to happen properly except it doesn't. - Should we have this, or Tracking issue for the #[alloc_error_handler] attribute (for no_std + liballoc) #51540, or both?
- Who is responsible for preventing reentrancy issues through the allocator? unsafe-code-guidelines#506
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsB-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
SimonSapin commentedon May 31, 2018
We have an accepted RFC #48043 that includes adding a way to opt into of panicking instead of aborting on OOM (so that
catch_unwindcould be used to recover from OOM at coarse granularity). Perhaps this could be the mechanism for that?glandium commentedon May 31, 2018
Oh right, I had forgotten about that. And in fact, it works:
cargo runwith last nightly:glandium commentedon May 31, 2018
(despite the
#[rustc_allocator_nounwind]annotation onalloc::alloc::oom)glandium commentedon May 31, 2018
Added item about interaction with unwinding.
nikic commentedon May 31, 2018
@glandium While this example works, the
#[rustc_allocator_nounwind]annotation will (likely) prevent some drops from running during unwinding (in particular those that would have to be run inside the function that performs the oom call).glandium commentedon May 31, 2018
Picture me puzzled.
glandium commentedon May 31, 2018
Or
glandium commentedon May 31, 2018
Ah, I forgot
--release, and with it, in both cases, theFooinstance in the closure is not dropped (the one inoomis, though).alexcrichton commentedon May 31, 2018
With panic hooks they notably return
()instead of!which allows you to chain them (if necessary). I think if we want to use the terminology "hook" for OOM we probably want the same (not returning!) as that'll allow us to also justifytake_oom_hookas otherwise I think you can't actually call that and/or delegate in an order where you don't go first?OOM hooks could always panic/abort themselves of course but the signature may just want to be that by default we don't require it and then the fallback of OOM is to abort as OOM does today
glandium commentedon May 31, 2018
take_oom_hookis still useful albeit annoying, since hooks can't be closures. So you'd have to store the value in a global, but then you can call it from your own function. I can see how returning()would make things more flexible, though.Make the OOM hook return `()` rather than `!`
Auto merge of #51264 - glandium:oom, r=alexcrichton
30 remaining items
Auto merge of rust-lang#109507 - Amanieu:panic-oom-payload, r=davidtwco
Auto merge of rust-lang#109507 - Amanieu:panic-oom-payload, r=davidtwco
Auto merge of rust-lang#109507 - Amanieu:panic-oom-payload, r=davidtwco
Auto merge of rust-lang#109507 - Amanieu:panic-oom-payload, r=davidtwco
SimonSapin commentedon Dec 9, 2023
This tracking issue was closed when PR #109507 merged to remove the feature, but that was reverted in PR #110782. Reopening to reflect that.
PR #112331 is open to remove the feature again, and is already marked as closing this.
[-]Tracking issue for the OOM hook[/-][+]Tracking issue for the OOM hook (`alloc_error_hook`)[/+]Amanieu commentedon Dec 9, 2025
We discussed this in the @rust-lang/libs-api meeting today. Considering the recent progress towards externally implementable items (#146348), we think that the best way forward would be to get rid of the OOM hooks entirely and instead just allow users to override the standard library's
#[alloc_error_handler].docs(alloc): explain why ReportingAllocator wraps mimalloc on stable …
nia-e commentedon Jul 22, 2026
Per yesterday's libs-api meeting, we'd like to split
set_alloc_error_hookin twain so that setters can explicitly opt-in to unwinding semantics. There have been several soundness issues resulting from unwinds on OOM, and the effective safety requirements for it are incredibly strict (auditing one's entire dependency graph, since the vast majority of unsafe code is unsound in the presence of any spurious unwinds at all), so we'd like to have a version ofset_alloc_error_hookthat wraps the hook in an abort-on-unwind guard. I'll send a PR implementing this, but naming is open to bikeshedding ^^Rollup merge of rust-lang#159702 - nia-e:alloc-error-unwind-hook, r=j…
RalfJung commentedon Jul 22, 2026
With #159702, the docs for
set_alloc_error_hook_unwindingnow sayThis is unprecedented and IMO plain inacceptable. No other safety comment in std or anywhere else that I have seen says "audit the entire codebase". Has anyone even given a plausible argument for how that could be practically done? I doubt it can be practically done so I think this safety comment is equivalent to saying "this is not safe, there will probably be UB later, good luck have fun". This safety-comment is unformalizable in verification tools as it does not actually have a meaning (not in the same sense that our other safety comments do). This isn't a safety comment, it's an admission of defeat.
I am very strongly opposed to creating an ecosystem precedent for such "safety" reasoning (it doesn't deserve that name IMO). If other crates follow this example, the social contract that keeps unsafe Rust usable at scale will start eroding and risks destroying one of the key values of Rust -- an ecosystem of sound libraries that one can reason about in isolation and compose to large applications without worrying about that composition introducing UB.