Skip to content

Conversation

@dicej
Copy link

@dicej dicej commented Oct 9, 2024

Motivation

This adds support for the new wasm32-wasip2 target platform, which includes more extensive support for sockets than wasm32-wasip1 (formerly known as wasm32-wasi).

Solution

The bulk of the changes are in tokio-rs/mio#1836. This patch just tweaks a few cfg directives to indicate wasm32-wasip2's additional capabilities.

In the future, we could consider adding support for tokio::net::lookup_host. WASIp2 natively supports asynchronous DNS lookups and is single threaded, whereas Tokio currently assumes DNS lookups are blocking and require multithreading to emulate async lookups. A WASIp2-specific implementation could do the lookup directly without multithreading.

I've tested this end-to-end using https://github.com/dicej/wasi-sockets-tests, which includes smoke tests for mio, tokio, tokio-postgres, etc. I'd also be happy to add tests to this repo if appropriate; it would require adding a dev-dependency on e.g. wasmtime to actually run the test cases.

Note that this is a draft PR until tokio-rs/mio#1836 is merged, at which point I'll switch the mio dependency back to the upstream project.

@dicej dicej mentioned this pull request Oct 9, 2024
6 tasks
@Darksonn Darksonn added A-tokio Area: The main tokio crate T-wasm Topic: Web Assembly labels Oct 10, 2024
@Darksonn
Copy link
Contributor

Looks ok, but let's wait for the mio PR.

This adds support for the new `wasm32-wasip2` target platform, which includes
more extensive support for sockets than `wasm32-wasip1` (formerly known as
`wasm32-wasi`).

The bulk of the changes are in tokio-rs/mio#1836.  This
patch just tweaks a few `cfg` directives to indicate `wasm32-wasip2`'s
additional capabilities.

In the future, we could consider adding support for `ToSocketAddrs`.  WASIp2
natively supports asynchronous DNS lookups and is single threaded, whereas Tokio
currently assumes DNS lookups are blocking and require multithreading to emulate
async lookups.  A WASIp2-specific implementation could do the lookup directly
without multithreading.

I've tested this end-to-end using https://github.com/dicej/wasi-sockets-tests,
which includes smoke tests for `mio`, `tokio`, `tokio-postgres`, etc.  I'd also
be happy to add tests to this repo if appropriate; it would require adding a
dev-dependency on e.g. `wasmtime` to actually run the test cases.

Signed-off-by: Joel Dice <[email protected]>
#![cfg_attr(docsrs, allow(unused_attributes))]
#![cfg_attr(loom, allow(dead_code, unreachable_pub))]
#![cfg_attr(windows, allow(rustdoc::broken_intra_doc_links))]
#![cfg_attr(all(target_os = "wasi", target_env = "p2"), feature(wasip2))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like the right thing to do:

rust-lang/rust#130323 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate T-wasm Topic: Web Assembly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants