Skip to content

Commit

Permalink
Use a multi-threaded Tokio runtime as a fallback (#8229)
Browse files Browse the repository at this point in the history
This commit fixes a bug that was introduced in #8190 and brought up on
[Zulip]. In #8190 there was a subtle change in how Tokio and the CLI are
managed, namely that a Tokio runtime is installed at the start of the
CLI to avoid entering/exiting the runtime on each blocking call. This
had a small performance improvement relative to entering/exiting on each
blocking call. This meant, though, that calls previously blocked with
`Runtime::block_on` and now block with `Handle::block_on`. The
[documentation of `Handle::block_on`][doc] has a clause that says that
for single-threaded runtimes I/O and timers won't work.

To fix this issue I've switch the fallback runtime to a multi-threaded
runtime to ensure that I/O and timers continue to work.

[Zulip]: https://bytecodealliance.zulipchat.com/#narrow/stream/219900-wasi/topic/Wasi-http.20requests.20hang/near/429187256
[doc]: https://docs.rs/tokio/latest/tokio/runtime/struct.Handle.html#method.block_on
  • Loading branch information
alexcrichton committed Mar 25, 2024
1 parent 5715ff9 commit 28d2990
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/test-programs/src/bin/cli_sleep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std::time::Duration;

fn main() {
std::thread::sleep(Duration::from_nanos(100));
}
2 changes: 1 addition & 1 deletion crates/wasi/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::task::{Context, Poll};

pub(crate) static RUNTIME: once_cell::sync::Lazy<tokio::runtime::Runtime> =
once_cell::sync::Lazy::new(|| {
tokio::runtime::Builder::new_current_thread()
tokio::runtime::Builder::new_multi_thread()
.enable_time()
.enable_io()
.build()
Expand Down
7 changes: 7 additions & 0 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,13 @@ mod test_programs {
assert!(output.status.success());
Ok(())
}

#[test]
fn cli_sleep() -> Result<()> {
run_wasmtime(&["run", CLI_SLEEP])?;
run_wasmtime(&["run", CLI_SLEEP_COMPONENT])?;
Ok(())
}
}

#[test]
Expand Down

0 comments on commit 28d2990

Please sign in to comment.