diff --git a/crates/wasmedge-sys/src/async/fiber.rs b/crates/wasmedge-sys/src/async/fiber.rs index cee0541b7..b44e26cd9 100644 --- a/crates/wasmedge-sys/src/async/fiber.rs +++ b/crates/wasmedge-sys/src/async/fiber.rs @@ -57,6 +57,14 @@ impl<'a> FiberFuture<'a> { Ok(slot.unwrap()) } + + fn resume(&mut self, val: Result<(), ()>) -> Result, ()> { + let async_cx = AsyncCx { + current_suspend: self.current_suspend, + current_poll_cx: self.current_poll_cx, + }; + ASYNC_CX.set(&async_cx, || self.fiber.resume(val)) + } } impl<'a> Future for FiberFuture<'a> { type Output = Result<(), ()>; @@ -79,6 +87,19 @@ impl<'a> Future for FiberFuture<'a> { } unsafe impl Send for FiberFuture<'_> {} +impl Drop for FiberFuture<'_> { + fn drop(&mut self) { + if !self.fiber.done() { + let result = self.resume(Err(())); + // This resumption with an error should always complete the + // fiber. While it's technically possible for host code to catch + // the trap and re-resume, we'd ideally like to signal that to + // callers that they shouldn't be doing that. + debug_assert!(result.is_ok()); + } + } +} + type FiberSuspend = Suspend, (), Result<(), ()>>; scoped_tls::scoped_thread_local!(static ASYNC_CX: AsyncCx); diff --git a/crates/wasmedge-sys/src/async/function.rs b/crates/wasmedge-sys/src/async/function.rs index d5871e6f0..f90cce577 100644 --- a/crates/wasmedge-sys/src/async/function.rs +++ b/crates/wasmedge-sys/src/async/function.rs @@ -208,7 +208,7 @@ extern "C" fn wrap_async_wasi_fn( let result = match unsafe { async_cx.block_on(future.as_mut()) } { Ok(Ok(ret)) => Ok(ret), Ok(Err(err)) => Err(err), - Err(_err) => Err(HostFuncError::User(0x87)), + Err(_err) => Err(HostFuncError::Runtime(0x07)), }; // parse result diff --git a/crates/wasmedge-sys/src/instance/function.rs b/crates/wasmedge-sys/src/instance/function.rs index fe92f894c..ebdb0698c 100644 --- a/crates/wasmedge-sys/src/instance/function.rs +++ b/crates/wasmedge-sys/src/instance/function.rs @@ -134,7 +134,7 @@ extern "C" fn wrap_async_fn( let result = match unsafe { async_cx.block_on(future.as_mut()) } { Ok(Ok(ret)) => Ok(ret), Ok(Err(err)) => Err(err), - Err(_err) => Err(HostFuncError::User(0x87)), + Err(_err) => Err(HostFuncError::Runtime(0x07)), }; // parse result