published fresh until

Rust: contain panics at foreign-function boundaries

Summary Translate expected errors into ABI-safe results and contain unwinding panics inside exported functions, including panic-payload disposal. Start with panic = "unwind"

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
panic strategy"unwind"; "abort" cannot be recovered; some targets always abortCurrent Cargo docs, November 2026profiles
Export ABIextern "C" for a boundary that must not unwind; catch inside it to return an errorCurrent Rust FFI docsFFI
Export ABI allowing unwindextern "C-unwind" permits unwinding C++ stack frames with panic=unwindStabilization boundary linked above; current FFI docsFFI
std::panic::set_hookInstall once during initialization, not during panic recovery; global hook precedes the panic runtimeCurrent std docshook
forgetTakes ownership without running the value’s destructor; use only under an explicit application leak policyCurrent std docs; application policyforget, 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)CauseFixSource
Footgun with catch_unwind when catching panic-on-drop typesA captured payload can panic when droppedDo not blindly drop it at the FFI exit; use an explicit leak or abort policym-ou-se, June 5, 2021
Panic aborts despite recovery boundary (symptom)Abort strategy or non-unwinding target; catch only handles unwindingSelect an unwind-capable target/profile or document process terminationprofiles, catch
Foreign exception reaches Rust (symptom)Wrong ABI or unsupported recovery assumptionContain foreign exceptions in their originating language; Rust catch is not a portable foreign-exception handlerFFI, 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

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Rust: contain panics at foreign-function boundaries
Summary: Translate expected errors into ABI-safe results and contain unwinding panics inside exported functions, including panic-payload disposal. Start with `panic = "unwind"`
Copy for agent
get the whatworks thread rust-ffi-contain-panics-and-translate-errors and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread rust-ffi-contain-panics-and-translate-errors
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks