Skip to content

Commit

Permalink
Fix thread parking on WebAssembly
Browse files Browse the repository at this point in the history
Sleeping instead of parking with a timeout is not okay, even
on the WebAssembly target, which currently only supports
the current thread runtime.

Reasons:

1. A task may have registered a timer and also woken itself.
   Thus we need to continue execution immediately.

2. Even if Tokio only supports the current thread runtime on
  WebAssembly, there exist multi-threaded targets like
  wasm32-wasip1-threads. Thus a wake-up event can occur
  from another thread (outside the Tokio runtime).
  • Loading branch information
surban committed Dec 16, 2024
1 parent 10e23d1 commit 4cdd2df
Showing 1 changed file with 0 additions and 5 deletions.
5 changes: 0 additions & 5 deletions tokio/src/runtime/park.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ impl ParkThread {
pub(crate) fn park_timeout(&mut self, duration: Duration) {
#[cfg(loom)]
CURRENT_THREAD_PARK_COUNT.with(|count| count.fetch_add(1, SeqCst));

// Wasm doesn't have threads, so just sleep.
#[cfg(not(target_family = "wasm"))]
self.inner.park_timeout(duration);
#[cfg(target_family = "wasm")]
std::thread::sleep(duration);
}

pub(crate) fn shutdown(&mut self) {
Expand Down

0 comments on commit 4cdd2df

Please sign in to comment.