Skip to content

Commit

Permalink
Abort cancelled async Rust imports (#1176)
Browse files Browse the repository at this point in the history
This commit is the implementation of #1175 where async imports generate
an abort when cancelled in Rust (e.g. a Rust `panic!`) instead of
allowing it to naturally get cancelled.
  • Loading branch information
alexcrichton authored Feb 28, 2025
1 parent 81ae4c0 commit e0d2b88
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/guest-rust/rt/src/async_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::boxed::Box;
use std::collections::{hash_map, HashMap};
use std::fmt::{self, Debug, Display};
use std::future::Future;
use std::mem;
use std::pin::Pin;
use std::ptr;
use std::string::String;
Expand Down Expand Up @@ -164,6 +165,8 @@ pub async unsafe fn await_result(
(*CURRENT).todo += 1;
}

let trap_on_drop = TrapOnDrop;

match status {
STATUS_STARTING => {
let (tx, rx) = oneshot::channel();
Expand All @@ -182,6 +185,26 @@ pub async unsafe fn await_result(
}
_ => unreachable!("unrecognized async call status"),
}

mem::forget(trap_on_drop);

struct TrapOnDrop;

impl Drop for TrapOnDrop {
fn drop(&mut self) {
trap_because_of_future_drop();
}
}
}

#[cold]
fn trap_because_of_future_drop() {
panic!(
"an imported function is being dropped/cancelled before being fully \
awaited, but that is not sound at this time so the program is going \
to be aborted; for more information see \
https://github.com/bytecodealliance/wit-bindgen/issues/1175"
);
}

/// stream/future read/write results defined by the Component Model ABI.
Expand Down

0 comments on commit e0d2b88

Please sign in to comment.