bytecodealliance / bytecodealliance/wasmtime
Add API for cancelling tasks started using `[Typed]Func::call_concurrent`
- Dominant language
- Rust
- Stars
- 18.6k
- Forks
- 1.8k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 121
Description
The component model async ABI supports cancelling subtasks, and this is implemented in Wasmtime for guests via the `subtask.cancel` intrinsic. However, there's not yet any host-level API for cancelling tasks created using `call_concurrent`.
Note that subtask cancellation is both cooperative and asynchronous such that the guest may require arbitrary time and/or I/O to handle a cancellation request; in extreme cases, it may be busy looping and not even receive the request, or it might receive it and ignore it. Therefore, the host needs some way to know when the task has actually exited after sending it a cancellation request. In addition, since a task may continue running after it has returned a result, the host may need to cancel it either before it returns or after.
Currently, `call_concurrent` already returns a `TaskExit` object which may be used to wait (asynchronously) for the task to exit. However, the caller must wait until the `call_concurrent` `Future` resolves before it can get access to it.
Here's a rough sketch of an API that might work:
- `[Typed]Func::call_concurrent` returns a custom `Future` implementation with an `async fn cancel(self, accessor: impl AsAccessor) -> Result, TaskExit>` function, where `R` is the return type and `Option` represents the possibility that the task calls `task.return` instead of `task.cancel` after receiving the cancel request.
- `TaskExit` gets a new `async fn cancel(self, accessor: impl AsAccessor) -> Result<()>` function which allows the caller to cancel a task (or wait just wait for it to exit if we've already sent it a cancel request) after it has returned a value.
Note that the host can always simply drop the instance if the guest refuses to cooperate promptly with a cancel request.
Contributor guide
Assessment
This issue has not been assessed yet.