Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extension_host: Improve Wasmtime timeslicing behavior for extensions #24986

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion crates/extension_host/src/wasm_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use semantic_version::SemanticVersion;
use std::{
path::{Path, PathBuf},
sync::{Arc, OnceLock},
thread,
time::Duration,
};
use wasmtime::{
component::{Component, ResourceTable},
Expand Down Expand Up @@ -319,7 +321,32 @@ fn wasm_engine() -> wasmtime::Engine {
let mut config = wasmtime::Config::new();
config.wasm_component_model(true);
config.async_support(true);
wasmtime::Engine::new(&config).unwrap()
// Async support introduces the issue that extension execution happens during `Future::poll`,
// which could block an async thread.
// https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#execution-in-poll
//
// Epoch interruption is a lightweight mechanism to allow the extensions to yield control
// back to the executor at regular intervals.
config.epoch_interruption(true);

let engine = wasmtime::Engine::new(&config).unwrap();

// Need to do this on some non-async thread to make sure it makes progress regardless
// of if extensions are blocking.
let engine_ref = engine.weak();
thread::spawn(move || loop {
// Somewhat arbitrary interval, as it isn't a guaranteed interval.
// But this is a rough upper bound for how long the extension execution can block on
// `Future::poll`.
const EPOCH_INTERVAL: u64 = 100;
thread::sleep(Duration::from_millis(EPOCH_INTERVAL));
// Exit the loop and thread once the engine is dropped.
let Some(engine) = engine_ref.upgrade() else {
break;
};
engine.increment_epoch();
});
engine
})
.clone()
}
Expand Down Expand Up @@ -376,6 +403,10 @@ impl WasmHost {
},
);

// Store will yield after 1 tick, and get a new deadline of 1 tick after each yield.
store.set_epoch_deadline(1);
store.epoch_deadline_async_yield_and_update(1);
Comment on lines +407 to +408
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here there are a few other options:

  1. Change either the epoch interval or deadline to just put an upper bound on component execution (this is what Spin does)
  2. Do the callback version where you can decide if you want to yield or trap

No matter which way, since I believe you all have a timeout further up, the future will eventually get dropped, but any variation should still help.


let mut extension = Extension::instantiate_async(
&mut store,
this.release_channel,
Expand Down
Loading