Skip to content

Commit e0d2b88

Browse files
authored
Abort cancelled async Rust imports (#1176)
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.
1 parent 81ae4c0 commit e0d2b88

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

crates/guest-rust/rt/src/async_support.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::boxed::Box;
88
use std::collections::{hash_map, HashMap};
99
use std::fmt::{self, Debug, Display};
1010
use std::future::Future;
11+
use std::mem;
1112
use std::pin::Pin;
1213
use std::ptr;
1314
use std::string::String;
@@ -164,6 +165,8 @@ pub async unsafe fn await_result(
164165
(*CURRENT).todo += 1;
165166
}
166167

168+
let trap_on_drop = TrapOnDrop;
169+
167170
match status {
168171
STATUS_STARTING => {
169172
let (tx, rx) = oneshot::channel();
@@ -182,6 +185,26 @@ pub async unsafe fn await_result(
182185
}
183186
_ => unreachable!("unrecognized async call status"),
184187
}
188+
189+
mem::forget(trap_on_drop);
190+
191+
struct TrapOnDrop;
192+
193+
impl Drop for TrapOnDrop {
194+
fn drop(&mut self) {
195+
trap_because_of_future_drop();
196+
}
197+
}
198+
}
199+
200+
#[cold]
201+
fn trap_because_of_future_drop() {
202+
panic!(
203+
"an imported function is being dropped/cancelled before being fully \
204+
awaited, but that is not sound at this time so the program is going \
205+
to be aborted; for more information see \
206+
https://github.com/bytecodealliance/wit-bindgen/issues/1175"
207+
);
185208
}
186209

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

0 commit comments

Comments
 (0)