From b0048cde742ecdef36b36d4a18eec97af99254df Mon Sep 17 00:00:00 2001 From: Ben Brandt Date: Thu, 8 Oct 2026 13:15:32 +0000 Subject: [PATCH] gpui_web: Avoid blocking Fetch response-body sends (#65334) Replace the bounded `futures::mpsc` response-body channel with `async-channel`, using the workspace's default-features-disabled configuration. The browser Fetch pump runs on the main thread while response bodies can be consumed by workers. `futures::mpsc` locks an internal sender mutex during `poll_flush`; contention can attempt an unsupported `Atomics.wait` on the browser main thread. Release Notes: - [GPUI] Fixed a browser crash when streaming Fetch response bodies to background workers. --- Cargo.lock | 1 + crates/gpui_web/Cargo.toml | 1 + crates/gpui_web/examples/hello_web/Cargo.toml | 7 + .../examples/hello_web/http_client_test.rs | 48 ++++++ crates/gpui_web/examples/hello_web/index.html | 1 + .../examples/hello_web/test_http_client.mjs | 142 ++++++++++++++++++ crates/gpui_web/src/http_client.rs | 16 +- 7 files changed, 208 insertions(+), 8 deletions(-) create mode 100644 crates/gpui_web/examples/hello_web/http_client_test.rs create mode 100644 crates/gpui_web/examples/hello_web/test_http_client.mjs diff --git a/Cargo.lock b/Cargo.lock index 84ab4d002f7..7d4927114a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7646,6 +7646,7 @@ name = "gpui_web" version = "0.1.0" dependencies = [ "anyhow", + "async-channel 2.5.0", "console_error_panic_hook", "futures 0.3.34", "gpui", diff --git a/crates/gpui_web/Cargo.toml b/crates/gpui_web/Cargo.toml index af712b00b05..4018a96329b 100644 --- a/crates/gpui_web/Cargo.toml +++ b/crates/gpui_web/Cargo.toml @@ -29,6 +29,7 @@ parking_lot = { workspace = true, features = ["nightly"] } gpui_wgpu.workspace = true http_client.workspace = true anyhow.workspace = true +async-channel.workspace = true futures.workspace = true log.workspace = true uuid.workspace = true diff --git a/crates/gpui_web/examples/hello_web/Cargo.toml b/crates/gpui_web/examples/hello_web/Cargo.toml index 85450a97f52..4266cb4e61b 100644 --- a/crates/gpui_web/examples/hello_web/Cargo.toml +++ b/crates/gpui_web/examples/hello_web/Cargo.toml @@ -42,14 +42,21 @@ path = "queue_test.rs" name = "dynamic_fonts" path = "dynamic_fonts.rs" +[[bin]] +name = "http_client_test" +path = "http_client_test.rs" + [dependencies] anyhow = "1" futures = "0.3" gpui = { path = "../../../gpui" } gpui_platform = { path = "../../../gpui_platform" } gpui_util = { path = "../../../gpui_util" } +gpui_web = { path = "../.." } +http_client = { path = "../../../http_client" } unicode-segmentation = "1" wasm-bindgen = "0.2" +wasm-bindgen-futures = "0.4" wasm_thread = { git = "https://github.com/zed-industries/wasm_thread", rev = "0cf96c7708dfb97ccf3da50347e25edcf75d6937", features = ["es_modules"] } web-sys = { version = "0.3", features = ["console", "Location", "Window"] } web-time = "1" diff --git a/crates/gpui_web/examples/hello_web/http_client_test.rs b/crates/gpui_web/examples/hello_web/http_client_test.rs new file mode 100644 index 00000000000..70a8d7d1327 --- /dev/null +++ b/crates/gpui_web/examples/hello_web/http_client_test.rs @@ -0,0 +1,48 @@ +//! Exercises Fetch response-body contention between the browser and real workers. +//! +//! The browser harness supplies chunked Fetch streams while the production HTTP +//! client pumps them on the main thread and background workers consume them. + +use futures::AsyncReadExt as _; +use gpui::Platform as _; +use gpui_web::WebPlatform; +use http_client::HttpClient as _; +use std::sync::Arc; +use wasm_bindgen::prelude::*; + +fn main() {} + +#[wasm_bindgen] +pub async fn test_worker_fetch(url: String) -> Result<(), JsValue> { + let platform = WebPlatform::new(true); + let client = Arc::new(platform.fetch_http_client()); + let background = platform.background_executor(); + let main_thread = std::thread::current().id(); + let readers = (0..8).map(|_| { + let client = client.clone(); + let url = url.clone(); + background.spawn(async move { + anyhow::ensure!( + std::thread::current().id() != main_thread, + "response body must be read on a real worker" + ); + let mut response = client.get(&url, Default::default(), true).await?; + let mut bytes = Vec::new(); + response.body_mut().read_to_end(&mut bytes).await?; + anyhow::ensure!(bytes.len() == 32768 * 3, "response did not reach EOF"); + for (index, chunk) in bytes.chunks_exact(3).enumerate() { + let expected = [ + (index % 251) as u8, + (index % 239) as u8, + (index % 227) as u8, + ]; + anyhow::ensure!(chunk == expected, "response chunks must remain ordered"); + } + anyhow::Ok(()) + }) + }); + futures::future::try_join_all(readers) + .await + .map(|_| ()) + .map_err(|error| JsValue::from_str(&error.to_string())) +} diff --git a/crates/gpui_web/examples/hello_web/index.html b/crates/gpui_web/examples/hello_web/index.html index 62680135dae..ef47840cef3 100644 --- a/crates/gpui_web/examples/hello_web/index.html +++ b/crates/gpui_web/examples/hello_web/index.html @@ -16,6 +16,7 @@ +