From 6a7bf17e6e0f8c8a0490507c3eb643fd6f187f80 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Tue, 1 Sep 2026 08:18:26 +0700 Subject: [PATCH] fix multithread --- web/Cargo.toml | 2 +- web/rust-toolchain.toml | 2 +- web/script/build-wasm.sh | 8 +++++--- web/src/lib.rs | 35 +++++++---------------------------- 4 files changed, 14 insertions(+), 33 deletions(-) diff --git a/web/Cargo.toml b/web/Cargo.toml index 9d79bd89..f63bed84 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -32,7 +32,7 @@ console_log = "1.0" wasm-bindgen = "0.2" wasm-bindgen-futures = "0.4" js-sys = "0.3" -web-sys = { version = "0.3", features = ["Window", "Performance", "Location"] } +web-sys = { version = "0.3", features = ["Window", "Performance"] } universal-time = { git = "https://github.com/shadowylab/universal-time" } [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/web/rust-toolchain.toml b/web/rust-toolchain.toml index 86e8f38a..5fe1a593 100644 --- a/web/rust-toolchain.toml +++ b/web/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] channel = "nightly" -components = ["rustfmt", "clippy", "rust-src"] +components = ["rustfmt", "clippy"] targets = ["wasm32-unknown-unknown"] diff --git a/web/script/build-wasm.sh b/web/script/build-wasm.sh index db954625..a9b34152 100755 --- a/web/script/build-wasm.sh +++ b/web/script/build-wasm.sh @@ -37,12 +37,14 @@ fi # Step 1: Build WASM # -# Target features and link args (atomics, shared memory, TLS exports) come -# from `web/.cargo/config.toml`, which also enables `-Z build-std` so std is -# rebuilt with atomics support. +# Single-threaded build: `+bulk-memory` only. The multithreaded web backend +# is disabled in `web/src/lib.rs` (gpui's wasm workers freeze their JS event +# loop in `Atomics.wait`, which breaks nostr-sdk's spawn_local-driven client +# and the WebSocket transport), so no atomics/shared-memory flags here. echo -e "${GREEN}Step 1: Building WASM...${NC}" cd "$PROJECT_ROOT" export CARGO_TARGET_DIR="$PROJECT_ROOT/target" +RUSTFLAGS='-C target-feature=+bulk-memory -C link-arg=--max-memory=4294967296' \ cargo build --target wasm32-unknown-unknown $RELEASE_FLAG # Determine the build directory diff --git a/web/src/lib.rs b/web/src/lib.rs index 5f890977..973da21a 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -31,30 +31,6 @@ impl MonotonicClock for CustomTimeProvider { define_time_provider!(CustomTimeProvider); -/// Returns the preferred WebGPU/WebGL backend, selected with -/// `?backend=webgpu` / `?backend=webgl` in the page URL. -#[cfg(target_family = "wasm")] -fn requested_backend() -> gpui_platform::WebBackendPreference { - let search = web_sys::window() - .and_then(|window| window.location().search().ok()) - .unwrap_or_default(); - if search - .trim_start_matches('?') - .split('&') - .any(|parameter| parameter == "backend=webgpu") - { - gpui_platform::WebBackendPreference::WebGpu - } else if search - .trim_start_matches('?') - .split('&') - .any(|parameter| parameter == "backend=webgl") - { - gpui_platform::WebBackendPreference::WebGl - } else { - gpui_platform::WebBackendPreference::Auto - } -} - thread_local! { static APPLICATION: RefCell> = const { RefCell::new(None) }; } @@ -120,10 +96,13 @@ pub async fn run() -> Result<(), JsValue> { // immediately instead of waiting for a repaint. assets.preload().await; - // Multithreaded web: background work (image decoding, network - // fetches, CPU-heavy tasks) runs on real worker threads instead of - // the main thread, so the UI stays responsive. - gpui_platform::application_with_web_backend(requested_backend()).with_assets(assets) + // NOTE: the multithreaded web backend (application_with_web_backend) + // cannot host this app's backend. gpui's wasm background workers + // block on `Atomics.wait` while idle, freezing their JS event loop, + // so `spawn_local`-driven tasks (nostr-sdk's client actor, the + // WebSocket transport) and fetch promises never make progress on a + // worker thread. Everything must run on the main thread. + gpui_platform::single_threaded_web().with_assets(assets) }; let launch = move |cx: &mut App| {