What this thread does
Do first: set [profile.release] panic = "unwind" for a target supporting unwinding, then put catch_unwind inside each exported extern "C" wrapper.
Short answer
As of November 2026, current Rust docs distinguish recoverable unwinding from process aborts. C-unwind was stabilized in Rust 1.71.0, July 13, 2023; that historical release is not the current non-unwinding ABI contract. Translate expected Result errors into an ABI-safe status and out-parameter; contain unexpected Rust panics before returning to C (catch contract, FFI).
Choose / don't choose
- Result translation: choose for invalid input, I/O and other ordinary failures; don't use panics as the public error protocol (contract).
- C ABI plus containment: choose when callers cannot unwind; don't mistake the boundary's automatic abort for recoverable error handling (FFI).
- Unwinding ABI: choose only when unwinding must cross an FFI boundary; don't choose it when the requirement is that no panic escapes (FFI).
Not for: Rust command-line argument parsing; see clap-argh-bpaf-or-pico-args-for-a-rust-cli-with-anyhow-and-dist.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
panic strategy | "unwind"; "abort" cannot be recovered; some targets always abort | Current Cargo docs, November 2026 | profiles |
| Export ABI | extern "C" for a boundary that must not unwind; catch inside it to return an error | Current Rust FFI docs | FFI |
| Export ABI allowing unwind | extern "C-unwind" permits unwinding C++ stack frames with panic=unwind | Stabilization boundary linked above; current FFI docs | FFI |
std::panic::set_hook | Install once during initialization, not during panic recovery; global hook precedes the panic runtime | Current std docs | hook |
forget | Takes ownership without running the value’s destructor; use only under an explicit application leak policy | Current std docs; application policy | forget, payload report |
Order of operations
1. Validate pointers, lengths and ownership before using foreign memory (FFI). 2. Within the wrapper, catch the Rust operation and translate both ordinary Result::Err and a caught panic into distinct documented status values. Audit captured state for unwind safety (catch contract). 3. The panic hook runs before the panic runtime is invoked; a panic log can therefore appear even when the wrapper returns a failure (hook). 4. On Err(payload), choose an explicit disposal policy before returning. forget takes ownership of the value without running its destructor; an explicit process-abort policy is another option. A second catch around dropping needs a policy for its own payload too (forget, m-ou-se report).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
Footgun with catch_unwind when catching panic-on-drop types | A captured payload can panic when dropped | Do not blindly drop it at the FFI exit; use an explicit leak or abort policy | m-ou-se, June 5, 2021 |
| Panic aborts despite recovery boundary (symptom) | Abort strategy or non-unwinding target; catch only handles unwinding | Select an unwind-capable target/profile or document process termination | profiles, catch |
| Foreign exception reaches Rust (symptom) | Wrong ABI or unsupported recovery assumption | Contain foreign exceptions in their originating language; Rust catch is not a portable foreign-exception handler | FFI, catch |
Missing
- Concrete foreign status values, allocation ownership and post-panic object validity depend on the library contract. No universal recoverable state follows from catching a panic.
Checked
2026-11-12; current Rust/Cargo docs, linked stabilization release and dated m-ou-se report read. No Rust compilation or FFI calls executed.
Started from a post by @Rust project, packaged by the editor. Original post