Description
Update: This now the tracking issue for a mechanism to make OOM / memory allocation errors panic (and so by default unwind the thread) instead of aborting the process. This feature was accepted in RFC rust-lang/rfcs#2116.
It was separated from #48043, which tracks another feature of the same RFC, since the two features will likely be stabilized at different times.
Blockers
This should be blocked until we can audit all places in the code that allocate to ensure they safely handle unwinding on OOM.
-
VecDeque::shrink_to
leads to UB ifhandle_alloc_error
unwinds. #123369 - Who is responsible for preventing reentrancy issues through the allocator? unsafe-code-guidelines#506
Steps:
- Implement the RFC
-
std::alloc::set_allocation_error_hook
(tracked at Tracking issue for the OOM hook (alloc_error_hook
) #51245) could potentially be this mechanism, but that hook is currently document as not allowed to unwind.
-
- Adjust documentation (see instructions on forge)
- Stabilization PR (see instructions on forge)
Original feature request:
Several users who are invested in the "thread is a task" model that Rust was originally designed around have expressed a desire to unwind in the case of OOM. Specifically each task has large and unpredictable memory usage, and tearing down the first task that encounters OOM is considered a reasonable strategy.
Aborting on OOM is considered unacceptable because it would be expensive to discard all the work that the other tasks have managed to do.
We already (accidentally?) specified that all allocators can panic on OOM, including the global one, with the introduction of generic allocators on collections. So in theory no one "should" be relying on the default oom=abort
semantics. This knob would only affect the default global allocator. Presumably there would just be a function somewhere in the stdlib which is cfg'd to be a panic or abort based on this flag?
This is not a replacement for proper fallible allocation handling routines requested in #29802 because most of the potential users of that API build with panic=abort
. Also the requesters of this API are unwilling to go through the effort to ensure all their dependents are using fallible allocations everywhere.
I am dubious on this API but I'm filling an issue so that it's written down and everyone who wants it has a place to go and assert that it is in fact good and desirable.