Skip to content

future_to_promise Promise is not rejected if rust panics asynchronously #2724

@mstange

Description

@mstange

Describe the Bug

From the future_to_promise documentation:

If the future provided panics then the returned Promise will not resolve. Instead it will be a leaked promise. This is an unfortunate limitation of wasm currently that’s hoped to be fixed one day!

This behavior is very undesirable, so I think it would be good to have a bug on file for it.

I'm also curious about what the exact wasm limitation is, and where I can follow plans to fix it.

Steps to Reproduce

Rust:

use js_sys::Promise;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::{future_to_promise, JsFuture};

#[wasm_bindgen]
pub fn mywasmfun() -> Promise {
    future_to_promise(mywasmfun_impl())
}

async fn mywasmfun_impl() -> Result<JsValue, JsValue> {
    panic!("Please catch this panic");
}

JS:

import init, { mywasmfun } from './pkg/wasm_bindgen_async_panic.js';

async function run() {
  await init();

  try {
    await mywasmfun();
  } catch (e) {
    console.log("Caught exception:", e);
  }
}

run();

Expected Behavior

Panics should be propagated upwards the "async stack" so that any awaits on the JS side can catch panics and don't keep waiting forever.

Actual Behavior

The panic never propagates, the await never completes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions