Skip to content

Commit 3eb95a4

Browse files
guybedfordDarksonn
authored andcommitted
wasm: use sleep on emscripten without JSPI (#8528)
1 parent b22c802 commit 3eb95a4

3 files changed

Lines changed: 32 additions & 25 deletions

File tree

‎tokio/src/lib.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,7 @@
483483
//!
484484
//! When the build links WebAssembly JavaScript Promise Integration (JSPI), a
485485
//! wait that would block suspends on the host event loop rather than blocking.
486-
//! JSPI builds require Emscripten 6.0.10 or later; builds without JSPI link
487-
//! against any Emscripten the Rust target supports, and such a wait panics.
486+
//! Builds without JSPI will spin on timed parks, and panic otherwise.
488487
//!
489488
//! Suspension requires the current export to have been wrapped with
490489
//! `WebAssembly.promising`. A wait from any other activation throws

‎tokio/src/runtime/park.rs‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,22 @@ impl Inner {
147147
return;
148148
}
149149

150-
// Without JSPI a real wait is impossible: suspending would trap and
151-
// busy-waiting would starve the host loop the wake depends on. A
152-
// zero-duration park returns immediately as on native.
150+
// If JSPI is disabled, then we cannot park on JSPI, so instead use a
151+
// spin loop for a timed park, and a panic for any other suspension.
152+
// A zero-duration park returns immediately as on native.
153153
if !crate::runtime::jspi::jspi_enabled() {
154-
if dur == Some(Duration::ZERO) {
155-
return;
154+
match dur {
155+
Some(Duration::ZERO) => return,
156+
Some(dur) => {
157+
std::thread::sleep(dur);
158+
return;
159+
}
160+
None => panic!(
161+
"cannot block on wasm32-unknown-emscripten: this wait would \
162+
suspend on the host event loop, which needs the build to \
163+
link `-sJSPI`"
164+
),
156165
}
157-
panic!(
158-
"cannot block on wasm32-unknown-emscripten: this wait would \
159-
suspend on the host event loop, which needs the build to \
160-
link `-sJSPI`"
161-
);
162166
}
163167

164168
// Suspend until a host timer fires or a later activation (a host

‎tokio/tests/rt_emscripten_block_on.rs‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! `Runtime::block_on` drives the scheduler synchronously to a fixed point:
22
//! immediate futures return their value. A wait that would block suspends on
3-
//! the host loop when the build linked `-sJSPI` (see `rt_emscripten_jspi`)
4-
//! and panics when it did not. Both CI lanes run this file.
3+
//! the host loop when the build linked `-sJSPI` (see `rt_emscripten_jspi`).
4+
//! Without JSPI a timed wait spins until its deadline and an untimed wait
5+
//! panics. Both CI lanes run this file.
56
67
#![cfg(all(
78
target_os = "emscripten",
@@ -84,18 +85,21 @@ fn block_on_drives_many_ready_spawned_tasks() {
8485
}
8586

8687
#[test]
87-
fn timer_wait_needs_jspi() {
88-
let sleep = || {
89-
rt().block_on(async {
90-
tokio::time::sleep(Duration::from_millis(10)).await;
91-
});
92-
};
88+
fn timer_wait_completes_without_jspi() {
89+
// Without JSPI the park spins until the deadline rather than suspending,
90+
// so timer-only workloads complete as they did before JSPI support.
91+
let start = std::time::Instant::now();
92+
rt().block_on(async {
93+
tokio::time::sleep(Duration::from_millis(10)).await;
94+
});
95+
assert!(start.elapsed() >= Duration::from_millis(10));
96+
}
9397

94-
if jspi_linked() {
95-
sleep();
96-
} else {
97-
assert_panics_cannot_block_on(sleep);
98-
}
98+
#[test]
99+
fn timeout_elapses_without_jspi() {
100+
let (_tx, rx) = tokio::sync::oneshot::channel::<u32>();
101+
let res = rt().block_on(async { tokio::time::timeout(Duration::from_millis(10), rx).await });
102+
assert!(res.is_err());
99103
}
100104

101105
#[test]

0 commit comments

Comments
 (0)