Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
extension_host: Run extensions on the tokio threadpool (#40936)
Lukas Wirth278032c6b815
diff --git a/Cargo.lock b/Cargo.lockindex 48db1977efa..eb5df527243 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -5883,6 +5883,7 @@ dependencies = ["fs","futures 0.3.31","gpui",+ "gpui_tokio","http_client","language","language_extension",diff --git a/crates/extension_host/Cargo.toml b/crates/extension_host/Cargo.tomlindex 42dcdd3a6f3..16cbd9ac0c0 100644--- a/crates/extension_host/Cargo.toml+++ b/crates/extension_host/Cargo.toml@@ -27,6 +27,7 @@ extension.workspace = truefs.workspace = truefutures.workspace = truegpui.workspace = true+gpui_tokio.workspace = truehttp_client.workspace = truelanguage.workspace = truelog.workspace = truediff --git a/crates/extension_host/benches/extension_compilation_benchmark.rs b/crates/extension_host/benches/extension_compilation_benchmark.rsindex 6f0897af6ed..309e089758e 100644--- a/crates/extension_host/benches/extension_compilation_benchmark.rs+++ b/crates/extension_host/benches/extension_compilation_benchmark.rs@@ -19,6 +19,7 @@ use util::test::TempTree;fn extension_benchmarks(c: &mut Criterion) {let cx = init();+ cx.update(gpui_tokio::init);let mut group = c.benchmark_group("load");@@ -37,7 +38,7 @@ fn extension_benchmarks(c: &mut Criterion) {|wasm_bytes| {let _extension = cx.executor()- .block(wasm_host.load_extension(wasm_bytes, &manifest, cx.executor()))+ .block(wasm_host.load_extension(wasm_bytes, &manifest, &cx.to_async())).unwrap();},BatchSize::SmallInput,diff --git a/crates/extension_host/src/extension_store_test.rs b/crates/extension_host/src/extension_store_test.rsindex 855077bcf87..509edc6845c 100644--- a/crates/extension_host/src/extension_store_test.rs+++ b/crates/extension_host/src/extension_store_test.rs@@ -868,5 +868,6 @@ fn init_test(cx: &mut TestAppContext) {Project::init_settings(cx);ExtensionSettings::register(cx);language::init(cx);+ gpui_tokio::init(cx);});}diff --git a/crates/extension_host/src/wasm_host.rs b/crates/extension_host/src/wasm_host.rsindex f77258e8957..22d11732a74 100644--- a/crates/extension_host/src/wasm_host.rs+++ b/crates/extension_host/src/wasm_host.rs@@ -591,11 +591,12 @@ impl WasmHost {self: &Arc<Self>,wasm_bytes: Vec<u8>,manifest: &Arc<ExtensionManifest>,- executor: BackgroundExecutor,+ cx: &AsyncApp,) -> Task<Result<WasmExtension>> {let this = self.clone();let manifest = manifest.clone();- executor.clone().spawn(async move {+ let executor = cx.background_executor().clone();+ let load_extension_task = async move {let zed_api_version = parse_wasm_extension_version(&manifest.id, &wasm_bytes)?;let component = Component::from_binary(&this.engine, &wasm_bytes)@@ -632,20 +633,29 @@ impl WasmHost {.context("failed to initialize wasm extension")?;let (tx, mut rx) = mpsc::unbounded::<ExtensionCall>();- executor- .spawn(async move {- while let Some(call) = rx.next().await {- (call)(&mut extension, &mut store).await;- }- })- .detach();+ let extension_task = async move {+ while let Some(call) = rx.next().await {+ (call)(&mut extension, &mut store).await;+ }+ };- Ok(WasmExtension {- manifest: manifest.clone(),- work_dir: this.work_dir.join(manifest.id.as_ref()).into(),- tx,- zed_api_version,- })+ anyhow::Ok((+ extension_task,+ WasmExtension {+ manifest: manifest.clone(),+ work_dir: this.work_dir.join(manifest.id.as_ref()).into(),+ tx,+ zed_api_version,+ },+ ))+ };+ cx.spawn(async move |cx| {+ let (extension_task, extension) = load_extension_task.await?;+ // we need to run run the task in an extension context as wasmtime_wasi may+ // call into tokio, accessing its runtime handle+ gpui_tokio::Tokio::spawn(cx, extension_task)?.detach();++ Ok(extension)})}@@ -747,7 +757,7 @@ impl WasmExtension {.context("failed to read wasm")?;wasm_host- .load_extension(wasm_bytes, manifest, cx.background_executor().clone())+ .load_extension(wasm_bytes, manifest, cx).await.with_context(|| format!("failed to load wasm extension {}", manifest.id))}