Skip to content

Conversation

pchickey
Copy link
Contributor

@pchickey pchickey commented Sep 4, 2025

Add tasks to the wstd runtime, provided by the async-task crate.

runtime changes

The Runtime's inner fields have been switched from an outer refcell to multiple inner refcells in order to properly
split the (runtime-checked) borrows, because a task's Waker::wake now requires a mutation of the Runtime's ready_list. The async task driven ready_list machinery replaces prior uses of block_on's "root" Waker machinery, so the internals of block_on look a little different than before, but all of the improvements we figured out in #78 and #71 still work.

Unfortunately, integrating async-task with wstd does require one single use of unsafe Rust. Its easy to reason about the unsoundness of this use, but this does break the crate-wide #[forbid(unsafe_code)], which is unfortunate. async-task does not provide a safe variant of its schedule function that lacks Send/Sync bounds. Because wstd will only run in single-threaded contexts, this Send/Sync restriction is not relevant to our use case.

test changes

The tcp echo server example has been changed to spawn tasks, and used as the test case to demonstrate that tasks are
implemented properly. The test runner now exercises that many sockets can be accepted while echoes are in flight, which was not possible without adding concurrency to that test case. Tasks aren't the only way to add concurrency for this example, but they are appropriate because the accept loop is not interested in whether any particular connection fails.

When I was updating the tcp echo server example, I took care of replacing library use of wasmtime with spawning a wasmtime-cli process instead (Closes #43), and I went and implemented some conversion TODOs in wstd::net::tcp_listener so that the guest could more easily tell the host where it was listening.

All of these changes are broken out into a separate commit, to make it simpler to review them relative to the runtime changes.

…listener supports v6

The tcp-echo-server example has been rewritten to spawn the echo part of
each accept into a task, which  means it can accept new connections
while other echos are in flight.

The tcp-echo-server test has been rewritten to test that connections can
be accepted while other echoes are in flight.

The tcp-echo-server test has been rewritten to use wasmtime cli as a
process, rather than use wasmtime as a crate. This drops wasmtime from
the dev-dependencies of the workspace, which is good because it was
running a quite out-of-date wasmtime.

Fix up the missing conversions to/from std::net::SocketAddr in
wstd::net::tcp_listener, so that we can send a Display impl of the
listening address from the guest to host, and parse it out in the host
(see get_listening_address)
@pchickey pchickey merged commit 8158a83 into main Sep 11, 2025
5 checks passed
@pchickey pchickey deleted the pch/async_task branch September 11, 2025 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite tcp_echo_server test to shell out to wasmtime
2 participants