Skip to content

platform-wallet-ffi: panic in block_on_worker unwinds across the extern "C" boundary (host-app process abort) #3805

Open

Description

@QuantumExplorer

Summary

platform-wallet-ffi's shared blocking primitive, block_on_worker, propagates a panic from the tokio worker back onto the FFI thread with .expect(...). If the panic occurs inside an async wallet operation, it unwinds out of the extern "C" FFI function. Unwinding across an extern "C" boundary aborts the process (a guaranteed abort since Rust 1.81, previously UB) — i.e. any deep panic in the build / prove / broadcast / sync pipeline hard-crashes the host app (iOS, etc.) instead of returning a recoverable error.

There is no memory corruption, no fund loss, and no consensus impact — this is a host-app robustness / crash-resilience issue.

Location

packages/rs-platform-wallet-ffi/src/runtime.rs:49-56

pub(crate) fn block_on_worker<F>(future: F) -> F::Output
where
    F: std::future::Future + Send + 'static,
    F::Output: Send + 'static,
{
    let rt = runtime();
    rt.block_on(async move { rt.spawn(future).await.expect("tokio worker panicked") })
}

rt.spawn(future).await returns Err(JoinError) if the worker future panics; the .expect("tokio worker panicked") then re-panics on the thread driving block_on — which is the caller's FFI thread inside an extern "C" function.

Scope / impact

block_on_worker is the shared blocking entry point for every async FFI operation in platform-wallet-ffi — 46 call sites across 28 files (shielded send/fund/resume, wallet sync, broadcast, etc.). Any unhandled panic anywhere in those async paths (e.g. an unwrap/expect/arithmetic panic deep in proving, GroveDB, or networking) propagates as a process abort on the host rather than a PlatformWalletFFIResult error the host can handle and surface to the user.

Severity

Low — robustness only. No UB (the abort is defined behavior on current toolchains), no fund/consensus risk. But a host-app crash on any deep panic is a poor failure mode for a mobile SDK, where a recoverable error is strongly preferable.

Provenance

Pre-existing; not introduced by any single change. Surfaced during the adversarial audit of the shield-fees PR (finding L6), which explicitly confirmed the PR does not aggravate it — the new parse_optional_surplus_output helper returns Result and never panics into the worker. Filed separately so it can be fixed as shared FFI hardening rather than bundled into a consensus-fee change.

Recommended fix

Catch the panic at the FFI boundary and convert it to a recoverable error instead of letting it unwind:

  • Wrap the worker future in std::panic::catch_unwind (e.g. AssertUnwindSafe(future).catch_unwind().await from futures::FutureExt, or catch the JoinError::is_panic() in block_on_worker and map it), and on a caught panic return PlatformWalletFFIResult::err(ErrorInternal, <message>) from each wrapper.
  • Verify the crate (and any consuming build profile) is not compiled with panic = "abort", since catch_unwind cannot catch under abort. (No panic = "abort" is set in packages/rs-platform-wallet-ffi/Cargo.toml or the workspace root today — keep it that way for the FFI crate.)
  • Consider centralizing this so all 46 call sites benefit without per-wrapper changes (e.g. have block_on_worker itself return a Result/sentinel on worker panic, or add a block_on_worker_catching variant used by the FFI wrappers).

Acceptance criteria

  • A panic inside an async wallet FFI operation returns a PlatformWalletFFIResult error (e.g. ErrorInternal) to the caller instead of aborting the process.
  • The FFI crate is confirmed to use panic = "unwind" (not abort).
  • A regression test induces a worker-future panic and asserts the FFI returns an error rather than crashing the test process.

Activity

  1. added
    bug
    priority: low
    on Jun 7, 2026
  2. Aakilahamed006 commented on Jun 16, 2026

    @Aakilahamed006

    Hi, I'd like to work on this issue if it's still available.

    My understanding is that the fix would involve preventing worker panics from unwinding across the FFI boundary, converting them into a recoverable PlatformWalletFFIResult error, and adding regression tests to verify the process does not abort.

    Please let me know if there are any preferred implementation details before I start working on a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugplatform-walletpriority: low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions