Skip to content

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

Limit active fibers in a store by default (#14971)

Alex Crichton4d3cd09f0284
diff --git a/crates/cli-flags/src/lib.rs b/crates/cli-flags/src/lib.rs
index f19fd3aed9..5678c00cf6 100644
--- a/crates/cli-flags/src/lib.rs
+++ b/crates/cli-flags/src/lib.rs
@@ -635,6 +635,8 @@ wasmtime_option_group! {
/// resources, streams, futures) per store; allocating past it traps.
/// (default: 100000)
pub max_component_handles: Option<usize>,
+ /// Maximum number of simultaneously-live fibers per store.
+ pub max_fibers: Option<u32>,
/// Maximum value, in bytes, for a wasi-random 0.2
/// `get{,-insecure}-random-bytes` `len` parameter. Calls with a value
/// exceeding this limit will trap.
diff --git a/crates/wasmtime/src/runtime/fiber.rs b/crates/wasmtime/src/runtime/fiber.rs
index e335d78e9d..3f8676d83d 100644
--- a/crates/wasmtime/src/runtime/fiber.rs
+++ b/crates/wasmtime/src/runtime/fiber.rs
@@ -92,6 +92,11 @@ pub(crate) struct AsyncState {
// stack at a time and making the number tunable via `Config`.
last_fiber_stack: Option<wasmtime_fiber::FiberStack>,
+ // Bookkeeping for keeping track of the number of fiber stacks (aka fibers)
+ // actively in use within this store.
+ pub(crate) live_fibers: u32,
+ pub(crate) max_fibers: u32,
+
/// Whether or not this store has async host functions defined somewhere
/// within it or some other store-related configuration (e.g. epochs
/// yielding) which requires that wasm is executed on a fiber, thus async
@@ -115,6 +120,8 @@ impl Default for AsyncState {
current_suspend: None,
current_future_cx: None,
last_fiber_stack: None,
+ live_fibers: 0,
+ max_fibers: 64,
async_required: false,
}
}
@@ -745,6 +752,12 @@ fn resume_fiber<'a>(
fn drop(&mut self) {
self.fiber.state =
Some(unsafe { self.state.take().unwrap().replace(self.store).into() });
+
+ // Return a fiber's stack back to the store once it has completed.
+ if self.fiber.fiber().unwrap().done() {
+ let stack = self.fiber.take_fiber_stack().unwrap();
+ self.store.deallocate_fiber_stack(stack);
+ }
}
}
let result = unsafe {
@@ -763,13 +776,8 @@ fn resume_fiber<'a>(
};
match &result {
- // The fiber has finished, so recycle its stack by disposing of the
- // underlying fiber itself.
- Ok(_) => {
- if let Some(stack) = fiber.take_fiber_stack() {
- store.deallocate_fiber_stack(stack);
- }
- }
+ // The fiber has finished, nothing more to do.
+ Ok(_) => {}
// The fiber has not yet finished, so it stays as-is.
Err(_) => {
@@ -869,7 +877,7 @@ where
Ok(fiber) => fiber,
Err((e, stack)) => {
unsafe {
- engine.allocator().deallocate_fiber_stack(stack);
+ (*store).as_store_opaque().deallocate_fiber_stack(stack);
}
return Err(e);
}
diff --git a/crates/wasmtime/src/runtime/store/async_.rs b/crates/wasmtime/src/runtime/store/async_.rs
index f458234dfa..4cb4439647 100644
--- a/crates/wasmtime/src/runtime/store/async_.rs
+++ b/crates/wasmtime/src/runtime/store/async_.rs
@@ -4,7 +4,7 @@ use crate::fiber::{self};
use crate::prelude::*;
#[cfg(feature = "gc")]
use crate::runtime::vm::VMStore;
-use crate::store::{Asyncness, ResourceLimiterInner, StoreInner, StoreOpaque};
+use crate::store::{AsContextMut, Asyncness, ResourceLimiterInner, StoreInner, StoreOpaque};
use crate::{Store, StoreContextMut, UpdateDeadline};
/// An object that can take callbacks when the runtime enters or exits hostcalls.
@@ -141,9 +141,42 @@ impl<T> Store<T> {
pub fn epoch_deadline_async_yield_and_update(&mut self, delta: u64) {
self.inner.epoch_deadline_async_yield_and_update(delta);
}
+
+ /// Sets the maximum number of simultaneously-live fibers permitted in this
+ /// store.
+ ///
+ /// Async entrypoints in Wasmtime, such as [`Func::call_async`] will
+ /// allocate fibers to execute WebAssembly code on. Fibers are additionally
+ /// used for implementing component model async-related primitives. Fibers
+ /// represent an allocated chunk of the host process's virtual address space
+ /// for use as a temporary native stack. Due to the cost of a fiber
+ /// allocation they're limited by default per-store with the ability for
+ /// embedders to tune limits as desired.
+ ///
+ /// Attempts to exceed this limit will be met with an immediate error. For
+ /// example [`Func::call_async`] will return an error when this limit is
+ /// reached.
+ ///
+ /// The limit counts live fibers, so a fiber returns budget once it
+ /// completes. Lowering it below the current live count does not affect
+ /// existing fibers, only blocks new ones until the count falls back under
+ /// it. A limit of 0 makes every async entry point fail.
+ ///
+ /// The default value for this is 64 fibers.
+ ///
+ /// [`Engine`]: crate::Engine
+ /// [`Func::call_async`]: crate::Func::call_async
+ pub fn set_max_fibers(&mut self, limit: u32) {
+ self.as_context_mut().set_max_fibers(limit)
+ }
}
impl<'a, T> StoreContextMut<'a, T> {
+ /// See [`Store::set_max_fibers`].
+ pub fn set_max_fibers(&mut self, limit: u32) {
+ self.0.async_state.max_fibers = limit;
+ }
+
/// Perform garbage collection of `ExternRef`s.
///
/// Same as [`Store::gc`].
@@ -203,13 +236,19 @@ impl<T> StoreInner<T> {
#[doc(hidden)]
impl StoreOpaque {
pub(crate) fn allocate_fiber_stack(&mut self) -> Result<wasmtime_fiber::FiberStack> {
- if let Some(stack) = self.async_state.last_fiber_stack().take() {
- return Ok(stack);
+ if self.async_state.live_fibers >= self.async_state.max_fibers {
+ bail!("store limit of fibers has been reached");
}
- self.engine().allocator().allocate_fiber_stack()
+ let stack = match self.async_state.last_fiber_stack().take() {
+ Some(stack) => stack,
+ None => self.engine().allocator().allocate_fiber_stack()?,
+ };
+ self.async_state.live_fibers += 1;
+ Ok(stack)
}
pub(crate) fn deallocate_fiber_stack(&mut self, stack: wasmtime_fiber::FiberStack) {
+ self.async_state.live_fibers -= 1;
self.flush_fiber_stack();
*self.async_state.last_fiber_stack() = Some(stack);
}
diff --git a/src/common.rs b/src/common.rs
index 0d572da932..aa718ebe96 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -520,6 +520,9 @@ impl RunCommon {
if let Some(max) = self.common.wasi.max_component_handles {
store.set_max_component_handles(max);
}
+ if let Some(max) = self.common.wasi.max_fibers {
+ store.set_max_fibers(max);
+ }
*limits(store.data_mut()) = self.store_limits();
store.limiter(move |t| limits(t));
diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rs
index ef30bf6e0f..02d65bfc47 100644
--- a/tests/all/component_model/async.rs
+++ b/tests/all/component_model/async.rs
@@ -3773,3 +3773,79 @@ async fn resource_drop_async_dtor_yields() -> Result<()> {
r.resource_drop_async(&mut store).await?;
Ok(())
}
+
+#[tokio::test]
+#[cfg_attr(miri, ignore)]
+async fn max_fibers() -> Result<()> {
+ let mut config = Config::new();
+ config.wasm_component_model_async_stackful(true);
+ let engine = Engine::new(&config)?;
+
+ let component = Component::new(
+ &engine,
+ r#"(component
+ (component $C
+ (core module $libc (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core module $m
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))
+ (func (export "wait-forever")
+ (drop (call $waitable-set.wait (call $waitable-set.new) (i32.const 0))))
+ )
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core func $waitable-set.wait (canon waitable-set.wait (memory (core memory $libc "memory"))))
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "waitable-set.new" (func $waitable-set.new))
+ (export "waitable-set.wait" (func $waitable-set.wait))
+ ))
+ ))
+ (func (export "wait-forever") async
+ (canon lift (core func $i "wait-forever") async))
+ )
+
+ (component $D
+ (import "c" (instance $c (export "wait-forever" (func async))))
+ (core module $libc (memory (export "memory") 1))
+ (core instance $libc (instantiate $libc))
+ (core module $m
+ (import "" "wait-forever" (func $wait-forever (result i32)))
+ (func (export "run") (param $n i32)
+ (loop $l
+ (if (local.get $n)
+ (then
+ (drop (call $wait-forever))
+ (local.set $n (i32.sub (local.get $n) (i32.const 1)))
+ (br $l)))))
+ )
+ (core func $wait-forever
+ (canon lower (func $c "wait-forever") async (memory (core memory $libc "memory"))))
+ (core instance $i (instantiate $m
+ (with "" (instance (export "wait-forever" (func $wait-forever))))
+ ))
+ (func (export "run") async (param "n" u32)
+ (canon lift (core func $i "run")))
+ )
+
+ (instance $c (instantiate $C))
+ (instance $d (instantiate $D (with "c" (instance $c))))
+ (func (export "run") (alias export $d "run"))
+ )"#,
+ )?;
+ let linker = Linker::new(&engine);
+
+ let run = async |n: u32| -> Result<()> {
+ let mut store = Store::new(&engine, ());
+ store.set_max_fibers(10);
+ let instance = linker.instantiate_async(&mut store, &component).await?;
+ let run = instance.get_typed_func::<(u32,), ()>(&mut store, "run")?;
+ store
+ .run_concurrent(async |a| run.call_concurrent(a, (n,)).await)
+ .await?
+ };
+
+ run(9).await?;
+ assert!(run(10).await.is_err());
+ Ok(())
+}