mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
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.
This commit is contained in:
Generated
+1
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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()))
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
<link data-trunk rel="rust" data-type="worker" data-bin="dynamic_fonts" data-bindgen-target="web" data-keep-debug data-wasm-opt="0" />
|
||||
<link data-trunk rel="rust" data-type="worker" data-bin="font_fallback_test" data-bindgen-target="web" data-keep-debug data-wasm-opt="0" />
|
||||
<link data-trunk rel="rust" data-type="worker" data-bin="queue_test" data-bindgen-target="web" data-keep-debug data-wasm-opt="0" />
|
||||
<link data-trunk rel="rust" data-type="worker" data-bin="http_client_test" data-bindgen-target="web" data-keep-debug data-wasm-opt="0" />
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
// This standalone example has an ignored lockfile. Before building, match
|
||||
// event-listener to the repository Cargo.lock (currently 5.4.1) so the test
|
||||
// exercises the production synchronization backend:
|
||||
// cargo update -p event-listener --precise 5.4.1
|
||||
// cargo tree --target wasm32-unknown-unknown -i event-listener -e features
|
||||
// The Wasm feature tree must not enable event-listener/std (a blocking mutex).
|
||||
// Then, with the hello_web gallery served by Trunk:
|
||||
// node test_http_client.mjs http://127.0.0.1:8080
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { spawn } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const origin = new URL(process.argv[2] ?? "http://127.0.0.1:8080").origin;
|
||||
const chrome = process.env.CHROME ??
|
||||
(process.platform === "darwin"
|
||||
? "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
|
||||
: "chromium");
|
||||
const output = path.join(path.dirname(fileURLToPath(import.meta.url)), "target", "http-client-test", String(Date.now()));
|
||||
fs.mkdirSync(output, { recursive: true });
|
||||
const log = fs.openSync(path.join(output, "chrome.log"), "w");
|
||||
const browser = spawn(chrome, [
|
||||
"--headless=new", "--no-first-run", "--no-default-browser-check",
|
||||
"--remote-debugging-port=0", `--user-data-dir=${path.join(output, "profile")}`,
|
||||
"about:blank",
|
||||
], { stdio: ["ignore", log, log] });
|
||||
const delay = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
|
||||
let launchError;
|
||||
browser.on("error", error => { launchError = error; });
|
||||
let socket;
|
||||
try {
|
||||
let port;
|
||||
for (let attempt = 0; attempt < 100; attempt++) {
|
||||
if (launchError) throw launchError;
|
||||
if (browser.exitCode !== null) throw new Error(`Chrome exited; see ${output}/chrome.log`);
|
||||
const portFile = path.join(output, "profile", "DevToolsActivePort");
|
||||
if (fs.existsSync(portFile)) {
|
||||
port = fs.readFileSync(portFile, "utf8").split("\n")[0];
|
||||
break;
|
||||
}
|
||||
await delay(100);
|
||||
}
|
||||
assert.ok(port, "Chrome did not start");
|
||||
const pages = await (await fetch(`http://127.0.0.1:${port}/json/list`)).json();
|
||||
socket = new WebSocket(pages.find(page => page.type === "page").webSocketDebuggerUrl);
|
||||
await new Promise((resolve, reject) => {
|
||||
socket.addEventListener("open", resolve, { once: true });
|
||||
socket.addEventListener("error", reject, { once: true });
|
||||
});
|
||||
let nextId = 0;
|
||||
const pending = new Map();
|
||||
const errors = [];
|
||||
socket.addEventListener("message", ({ data }) => {
|
||||
const message = JSON.parse(data);
|
||||
if (message.id) {
|
||||
const request = pending.get(message.id);
|
||||
if (!request) return;
|
||||
pending.delete(message.id);
|
||||
clearTimeout(request.timer);
|
||||
if (message.error) request.reject(new Error(JSON.stringify(message.error)));
|
||||
else request.resolve(message.result);
|
||||
} else if (message.method === "Runtime.exceptionThrown") {
|
||||
errors.push(message.params);
|
||||
for (const request of pending.values()) {
|
||||
clearTimeout(request.timer);
|
||||
request.reject(new Error(JSON.stringify(message.params.exceptionDetails)));
|
||||
}
|
||||
pending.clear();
|
||||
}
|
||||
});
|
||||
const call = (method, params = {}) => new Promise((resolve, reject) => {
|
||||
const id = ++nextId;
|
||||
// This deadline is outside the page so a main-thread trap or hang cannot hide.
|
||||
const timer = setTimeout(() => {
|
||||
pending.delete(id);
|
||||
reject(new Error(`Timed out: ${method}; see ${output}/chrome.log`));
|
||||
}, 120_000);
|
||||
timer.unref();
|
||||
pending.set(id, { resolve, reject, timer });
|
||||
socket.send(JSON.stringify({ id, method, params }));
|
||||
});
|
||||
await call("Runtime.enable");
|
||||
await call("Page.enable");
|
||||
const loaded = new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error("Page did not load")), 10_000);
|
||||
socket.addEventListener("message", function onLoad({ data }) {
|
||||
if (JSON.parse(data).method === "Page.loadEventFired") {
|
||||
clearTimeout(timer);
|
||||
socket.removeEventListener("message", onLoad);
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
await call("Page.navigate", { url: origin });
|
||||
await loaded;
|
||||
const result = await call("Runtime.evaluate", {
|
||||
awaitPromise: true, returnByValue: true,
|
||||
expression: `(async () => {
|
||||
const check = (condition, message) => { if (!condition) throw new Error(message); };
|
||||
check(crossOriginIsolated, "Shared-memory isolation headers are required");
|
||||
const fixture = await import("/http_client_test.js");
|
||||
const bindings = await fixture.default();
|
||||
check(bindings.memory.buffer instanceof SharedArrayBuffer, "Wasm memory must be shared");
|
||||
let waitForbidden = false;
|
||||
try { Atomics.wait(new Int32Array(bindings.memory.buffer), 0, 0, 0); }
|
||||
catch (error) { waitForbidden = error instanceof TypeError; }
|
||||
check(waitForbidden, "Blocking waits must be forbidden on the browser main thread");
|
||||
const originalFetch = globalThis.fetch;
|
||||
let responses = 0;
|
||||
globalThis.fetch = async () => {
|
||||
responses++;
|
||||
let pulled = 0;
|
||||
return new Response(new ReadableStream({
|
||||
pull(controller) {
|
||||
if (pulled === 32768) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
const index = pulled++;
|
||||
controller.enqueue(new Uint8Array([index % 251, index % 239, index % 227]));
|
||||
},
|
||||
}, { highWaterMark: 0 }));
|
||||
};
|
||||
try {
|
||||
await fixture.test_worker_fetch(location.origin + "/stream");
|
||||
check(responses === 8, "All worker requests must use the production Fetch pump");
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
return { chunksPerResponse: 32768, workerResponses: responses };
|
||||
})()`,
|
||||
});
|
||||
assert.equal(result.exceptionDetails, undefined, JSON.stringify(result.exceptionDetails));
|
||||
assert.deepEqual(errors, [], JSON.stringify(errors));
|
||||
assert.equal(result.result.value.workerResponses, 8);
|
||||
console.log("PASS: Fetch response-body contention between browser main thread and workers", result.result.value);
|
||||
} finally {
|
||||
socket?.close();
|
||||
browser.kill();
|
||||
fs.closeSync(log);
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
use crate::WebDispatcher;
|
||||
use anyhow::{Context as _, anyhow};
|
||||
use futures::{
|
||||
AsyncRead, AsyncReadExt as _, FutureExt as _, SinkExt as _, TryStreamExt as _,
|
||||
channel::{mpsc, oneshot},
|
||||
};
|
||||
use futures::{AsyncRead, AsyncReadExt as _, FutureExt as _, TryStreamExt as _, channel::oneshot};
|
||||
use http_client::{AsyncBody, HttpClient, RedirectPolicy};
|
||||
use std::{
|
||||
io,
|
||||
@@ -211,7 +208,7 @@ async fn read_body_to_bytes(mut body: AsyncBody) -> anyhow::Result<Option<Vec<u8
|
||||
const RESPONSE_BODY_CHANNEL_CAPACITY: usize = 8;
|
||||
|
||||
struct ReadableStreamBody {
|
||||
chunks: futures::stream::IntoAsyncRead<mpsc::Receiver<io::Result<Vec<u8>>>>,
|
||||
chunks: futures::stream::IntoAsyncRead<Pin<Box<async_channel::Receiver<io::Result<Vec<u8>>>>>>,
|
||||
// Dropping this sender resolves the pump's cancellation future, which
|
||||
// cancels the browser-side `ReadableStream`.
|
||||
_cancellation: oneshot::Sender<()>,
|
||||
@@ -219,7 +216,10 @@ struct ReadableStreamBody {
|
||||
|
||||
impl ReadableStreamBody {
|
||||
fn new(reader: web_sys::ReadableStreamDefaultReader) -> Self {
|
||||
let (chunks_sender, chunks_receiver) = mpsc::channel(RESPONSE_BODY_CHANNEL_CAPACITY);
|
||||
// The pump runs on the browser main thread while a worker may read the
|
||||
// body. futures::mpsc's internal mutex can park the main thread.
|
||||
let (chunks_sender, chunks_receiver) =
|
||||
async_channel::bounded(RESPONSE_BODY_CHANNEL_CAPACITY);
|
||||
let (cancellation, cancellation_receiver) = oneshot::channel();
|
||||
wasm_bindgen_futures::spawn_local(pump_response_body(
|
||||
reader,
|
||||
@@ -227,7 +227,7 @@ impl ReadableStreamBody {
|
||||
cancellation_receiver,
|
||||
));
|
||||
Self {
|
||||
chunks: chunks_receiver.into_async_read(),
|
||||
chunks: Box::pin(chunks_receiver).into_async_read(),
|
||||
_cancellation: cancellation,
|
||||
}
|
||||
}
|
||||
@@ -245,7 +245,7 @@ impl AsyncRead for ReadableStreamBody {
|
||||
|
||||
async fn pump_response_body(
|
||||
reader: web_sys::ReadableStreamDefaultReader,
|
||||
mut chunks: mpsc::Sender<io::Result<Vec<u8>>>,
|
||||
chunks: async_channel::Sender<io::Result<Vec<u8>>>,
|
||||
cancellation: oneshot::Receiver<()>,
|
||||
) {
|
||||
let cancellation = cancellation.fuse();
|
||||
|
||||
Reference in New Issue
Block a user