Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Document some known async pitfalls (#11871)

Alex Crichtond6e7841fce94
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index abfb131480..30aea75e7b 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -946,6 +946,43 @@ impl<T> StoreContextMut<'_, T> {
/// This function can be used to invoke [`Func::call_concurrent`] for
/// example within the async closure provided here.
///
+ /// # Store-blocking behavior
+ ///
+ ///
+ ///
+ /// At this time there are certain situations in which the `Future` returned
+ /// by the `AsyncFnOnce` passed to this function will not be polled for an
+ /// extended period of time, despite one or more `Waker::wake` events having
+ /// occurred for the task to which it belongs. This can manifest as the
+ /// `Future` seeming to be "blocked" or "locked up", but is actually due to
+ /// the `Store` being held by e.g. a blocking host function, preventing the
+ /// `Future` from being polled. A canonical example of this is when the
+ /// `fun` provided to this function attempts to set a timeout for an
+ /// invocation of a wasm function. In this situation the async closure is
+ /// waiting both on (a) the wasm computation to finish, and (b) the timeout
+ /// to elapse. At this time this setup will not always work and the timeout
+ /// may not reliably fire.
+ ///
+ /// This function will not block the current thread and as such is always
+ /// suitable to run in an `async` context, but the current implementation of
+ /// Wasmtime can lead to situations where a certain wasm computation is
+ /// required to make progress the closure to make progress. This is an
+ /// artifact of Wasmtime's historical implementation of `async` functions
+ /// and is the topic of [#11869] and [#11870]. In the timeout example from
+ /// above it means that Wasmtime can get "wedged" for a bit where (a) must
+ /// progress for a readiness notification of (b) to get delivered.
+ ///
+ /// This effectively means that it's not possible to reliably perform a
+ /// "select" operation within the `fun` closure, which timeouts for example
+ /// are based on. Fixing this requires some relatively major refactoring
+ /// work within Wasmtime itself. This is a known pitfall otherwise and one
+ /// that is intended to be fixed one day. In the meantime it's recommended
+ /// to apply timeouts or such to the entire `run_concurrent` call itself
+ /// rather than internally.
+ ///
+ /// [#11869]: https://github.com/bytecodealliance/wasmtime/issues/11869
+ /// [#11870]: https://github.com/bytecodealliance/wasmtime/issues/11870
+ ///
/// # Example
///
/// ```
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rs
index 497134c45a..32823dc289 100644
--- a/crates/wasmtime/src/runtime/component/func.rs
+++ b/crates/wasmtime/src/runtime/component/func.rs
@@ -341,6 +341,48 @@ impl Func {
/// This returns a [`TaskExit`] representing the completion of the guest
/// task and any transitive subtasks it might create.
///
+ /// # Progress
+ ///
+ /// For the wasm task being created in `call_concurrent` to make progress it
+ /// must be run within the scope of [`run_concurrent`]. If there are no
+ /// active calls to [`run_concurrent`] then the wasm task will appear as
+ /// stalled. This is typically not a concern as an [`Accessor`] is bound
+ /// by default to a scope of [`run_concurrent`].
+ ///
+ /// One situation in which this can arise, for example, is that if a
+ /// [`run_concurrent`] computation finishes its async closure before all
+ /// wasm tasks have completed, then there will be no scope of
+ /// [`run_concurrent`] anywhere. In this situation the wasm tasks that have
+ /// not yet completed will not make progress until [`run_concurrent`] is
+ /// called again.
+ ///
+ /// Embedders will need to ensure that this future is `await`'d within the
+ /// scope of [`run_concurrent`] to ensure that the value can be produced
+ /// during the `await` call.
+ ///
+ /// # Cancellation
+ ///
+ /// Cancelling an async task created via `call_concurrent`, at this time, is
+ /// only possible by dropping the store that the computation runs within.
+ /// With [#11833] implemented then it will be possible to request
+ /// cancellation of a task, but that is not yet implemented. Hard-cancelling
+ /// a task will only ever be possible by dropping the entire store and it is
+ /// not possible to remove just one task from a store.
+ ///
+ /// This async function behaves more like a "spawn" than a normal Rust async
+ /// function. When this function is invoked then metadata for the function
+ /// call is recorded in the store connected to the `accessor` argument and
+ /// the wasm invocation is from then on connected to the store. If the
+ /// future created by this function is dropped it does not cancel the
+ /// in-progress execution of the wasm task. Dropping the future
+ /// relinquishes the host's ability to learn about the result of the task
+ /// but the task will still progress and invoke callbacks and such until
+ /// completion.
+ ///
+ /// [`run_concurrent`]: crate::Store::run_concurrent
+ /// [#11833]: https://github.com/bytecodealliance/wasmtime/issues/11833
+ /// [`Accessor`]: crate::component::Accessor
+ ///
/// # Panics
///
/// Panics if the store that the [`Accessor`] is derived from does not own
diff --git a/crates/wasmtime/src/runtime/component/func/typed.rs b/crates/wasmtime/src/runtime/component/func/typed.rs
index 35e338f981..590491b503 100644
--- a/crates/wasmtime/src/runtime/component/func/typed.rs
+++ b/crates/wasmtime/src/runtime/component/func/typed.rs
@@ -259,6 +259,14 @@ where
/// representing the completion of the guest task and any transitive
/// subtasks it might create.
///
+ /// # Progress and Cancellation
+ ///
+ /// For more information about how to make progress on the wasm task or how
+ /// to cancel the wasm task see the documentation for
+ /// [`Func::call_concurrent`].
+ ///
+ /// [`Func::call_concurrent`]: crate::component::Func::call_concurrent
+ ///
/// # Panics
///
/// Panics if the store that the [`Accessor`] is derived from does not own