Skip to main content

drop_catching_panic

Function drop_catching_panic 

Source
#[doc(hidden)]
pub fn drop_catching_panic<F: FnOnce()>(f: F)
Expand description

Run a destructor closure, aborting the process if the closure panics.

A panic inside a GC finalizer cannot be safely propagated: the finalizer runs at an arbitrary point in R’s garbage collector, and unwinding across the C-ABI boundary into R’s runtime is undefined behaviour. Aborting is the only safe recovery strategy — the destructor has already left the value in an indeterminate state, so continuing is not an option.

§Implementation note

This function deliberately avoids std::panic::catch_unwind. On the first call from within R’s GC finalizer, catch_unwind may lazily initialise LLVM exception-handling state, which can allocate. Any allocation during a GC finalizer re-enters the GC and triggers the fatal “recursive gc invocation” crash. Instead, this function uses a drop-guard whose Drop impl calls std::thread::panicking() — a cheap, allocation-free TLS read.

This helper is #[doc(hidden)] because it is called from macro-generated code and is not part of the public API.