Skip to content

Commit

Permalink
[Fix] impl Drop for FiberFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
L-jasmine committed Aug 21, 2023
1 parent 0eeb695 commit aabc8a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
21 changes: 21 additions & 0 deletions crates/wasmedge-sys/src/async/fiber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ impl<'a> FiberFuture<'a> {

Ok(slot.unwrap())
}

fn resume(&mut self, val: Result<(), ()>) -> 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<(), ()>;
Expand All @@ -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<(), ()>, (), Result<(), ()>>;

scoped_tls::scoped_thread_local!(static ASYNC_CX: AsyncCx);
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmedge-sys/src/async/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ extern "C" fn wrap_async_wasi_fn<T: 'static>(
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
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmedge-sys/src/instance/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aabc8a9

Please sign in to comment.