Skip to main content

Module unwind_protect

Module unwind_protect 

Source
Expand description

Safe API for R’s R_UnwindProtect

This module provides with_r_unwind_protect for handling R errors with Rust cleanup. It automatically runs Rust destructors when R errors occur.

Important: R uses longjmp for error handling, which normally bypasses Rust destructors. Use this API to ensure cleanup happens even when R errors occur.

§Log drain

Every call to with_r_unwind_protect (and its variants) drains the cross-thread log queue via drain_log_queue_if_available before returning or re-raising an R error. This ensures that records buffered by worker threads are flushed to R’s console on every FFI exit — including error paths.

Statics§

R_CONTINUATION_TOKEN 🔒
Global continuation token for R_UnwindProtect.

Functions§

drain_log_queue_if_available 🔒
Drain the cross-thread log queue if the log feature is enabled.
get_continuation_token 🔒
Get or create the global continuation token.
panic_payload_to_string
Extract a message from a panic payload.
raise_rust_condition_via_stop 🔒
Raise an R condition with rust_* class layering by evaluating stop(structure(list(message = msg, call = call), class = c(...))).
run_r_unwind_protect 🔒
Core R_UnwindProtect wrapper. Returns Ok(result) on success, Err(payload) on Rust panic, or diverges via R_ContinueUnwind on R longjmp.
stop_sym 🔒
Cached stop symbol (permanently interned via Rf_install).
with_r_unwind_protect
Execute a closure with R unwind protection (non-error_in_r path).
with_r_unwind_protect_error_in_r
Like with_r_unwind_protect, but returns a tagged error SEXP on Rust panics instead of raising an R error via Rf_errorcall.
with_r_unwind_protect_shim
Like with_r_unwind_protect, but returns a tagged error SEXP on Rust panics instead of raising an R error via Rf_errorcall.
with_r_unwind_protect_sourced 🔒
Like with_r_unwind_protect, but reports panics with a custom [PanicSource].