Open maxwellflitton opened 6 months ago
What version of wasmtime are you using? I only know more recent versions where the "tcplisten" parameter is paired with "-S" and has to be given immediately after the "run" argument, like so:
wasmtime \
run \
-S tcplisten=127.0.0.1:8080 \
./wasi-server.wasm
@FrankReh thanks for the response. I am using the following version:
wasmtime-cli 20.0.0 (9e1084ffa 2024-04-22)
I've now switched to the following command:
wasmtime run -S tcplisten=127.0.0.1:8080 ./wasi-server.wasm
And I get the following error: Error: components do not support --tcplisten
Yes, that's as far as I get now too and that is puzzling. Now I'm looking into the commit that introduced that code to understand things better. Coincidentally, I had asked why this was happening a day or two ago myself with no response yet.
Okay. What you and I were missing is another -S
option. This is working for me now.
wasmtime run -S preview2=n -S tcplisten=127.0.0.1:8080 ./wasi-server.wasm
The transition from the Rust Wasm world only being modules to now having both Wasm modules and Wasm components has created some confusion - at least for me. I incorrectly assumed if wasmtime
were passed a Wasm module, it would use the file's type embedded in the header to run the module setup but that was wrong. There is a flag that can be set on the command line to make the choice explicit.
With that new version of the command, your example works and prints out the line you were expecting. Note that the actual set
your main was doing isn't needed. The TCP listener is automatically set to non blocking by the wasmtime
CLI code like this.
pub fn compute_preopen_sockets(&self) -> Result<Vec<TcpListener>> {
let mut listeners = vec![];
for address in &self.common.wasi.tcplisten {
let stdlistener = std::net::TcpListener::bind(address)
.with_context(|| format!("failed to bind to address '{}'", address))?;
let _ = stdlistener.set_nonblocking(true)?;
listeners.push(stdlistener)
}
Ok(listeners)
}
The transition from the Rust Wasm world only being modules to now having both Wasm modules and Wasm components has created some confusion - at least for me. I incorrectly assumed if wasmtime were passed a Wasm module, it would use the file's type embedded in the header to run the module setup but that was wrong. There is a flag that can be set on the command line to make the choice explicit.
Sorry this has been confusing, I think that our error messages and the choice of -S preview2=n
for this option made it extra confusing. There are two host implementations of WASI P1 in the wasmtime tree, and in the wasmtime-cli
executable.
wasi-common
crate that supports WASI P1 only. It has support for the tcp listener preopen. For wasmtime-cli
to use the legacy implementation, you must pass the -S preview2=n
flag. It would have been a much better choice if we had made this flag -S implementation=legacy
, and I'll discuss with the team whether we can change it!wasmtime-wasi
crate supports WASI P1 when used with modules, and WASI P2 when used with components. We recommend everyone use this implementation, however, it does not support the tcp socket preopen when used with WASI P1 modules, because we consider that feature of the legacy implementation a historical wart that never got adoption. Instead, in P2 we use the interfaces defined in the wasi-sockets proposal, which provides much more functionality, has a great set of docs and tests, and many folks actively developing and maintaining it. Wasi-sockets doesn't piggyback on the descriptor type like P1 sockets did. For programs that need it, wasi-libc
provides a posix API to the underlying wasi-sockets interface, thanks to @dicej in https://github.com/WebAssembly/wasi-libc/pull/481 https://github.com/WebAssembly/wasi-libc/pull/482 https://github.com/WebAssembly/wasi-libc/pull/488 .@pchickey Wow! That's a lot of interesting background. Thanks for that.
There was probably a busy loop making the mio
demo work anyway.
I don't have much more to add over what @pchickey already mentioned, but otherwise can confirm that this is working as expected. Some things I can say are:
From the OP @maxwellflitton you're using:
wasmtime run ./wasi-server.wasm -W tcp-listen=127.0.0.1:8080
That's actually passing the -W
argument to wasi-server.wasm
as a CLI argument, not to Wasmtime itself. I believe you've already discovered this as the next attempt was:
wasmtime run -S tcplisten=127.0.0.1:8080 ./wasi-server.wasm
where the error Error: components do not support --tcplisten
popped out. That's a bad error message since the flag has been renamed (it used to be --tcplisten
), but the "correct fix" for this is what @FrankReh pointed out with -Spreview2=n
which, as @pchickey pointed out, is a terrible name for a flag (and also difficult to find)
To me the action items here are:
-Spreview2=n
to -S implementation=...
-S tcplisten=...
when using wasmtime-wasi
's implementation of WASI
I have the following code:
It compiles to the target
wasm32-wasi
fine, but when I try and run it with the following command:and I get the following error: