Repository navigation
Description
I've been adding WASIp2 support to mio and triaging failures in its test suite. This test is failing, which I tracked down to wasmtime-wasi spuriously changing the local address of a UDP socket when wasi:sockets/udp#udp-socket.stream is called a second time on the same socket (equivalent to calling connect(2) a second time in POSIX). The test fails because it uses the old local address, not realizing the socket suddenly has a new local address.
Here's a minimal test case:
// udp.rs
use std::{error::Error, net::UdpSocket};
fn main() -> Result<(), Box<dyn Error>> {
let socket1 = UdpSocket::bind("127.0.0.1:0")?;
let socket2 = UdpSocket::bind("127.0.0.1:0")?;
let address1 = socket1.local_addr()?;
let address2 = socket2.local_addr()?;
println!(
"before:\n \
socket1 local address: {address1:?}\n \
socket2 local address: {address2:?}"
);
socket1.connect(address1)?;
socket1.connect(address2)?;
socket2.connect(address1)?;
println!(
"after:\n \
socket1 local address: {address1:?} remote address: {remote1:?}\n \
socket2 local address: {address2:?} remote address: {remote2:?}",
address1 = socket1.local_addr()?,
address2 = socket2.local_addr()?,
remote1 = socket1.peer_addr()?,
remote2 = socket2.peer_addr()?
);
let data = b"foobar";
socket1.send(data)?;
let mut buffer = [0u8; 20];
let count = socket2.recv(&mut buffer)?;
assert_eq!(data, &buffer[..count]);
println!("success!");
Ok(())
}If you build and run that natively on e.g. Linux using rustc udp.rs && ./udp, you'll see it succeed, with socket1's local address remaining unchanged before and after the UdpSocket::connect calls.
If you build and run for WASIp2 using rustc --target=wasm32-wasip2 udp.rs && wasmtime run -Sudp,inherit-network udp.wasm, you'll see it hang indefinitely due to socket1's local address changing due to the second UdpSocket::connect call.
While digging into this, I found that wasmtime-wasi proactively disconnects the socket by calling rustix::net::connect_unspec prior to reconnecting. When I comment out that code, the issue is fixed:
diff --git a/crates/wasi/src/p2/host/udp.rs b/crates/wasi/src/p2/host/udp.rs
index 1ef8350399..5dd426e736 100644
--- a/crates/wasi/src/p2/host/udp.rs
+++ b/crates/wasi/src/p2/host/udp.rs
@@ -71,9 +71,9 @@ impl udp::HostUdpSocket for WasiSocketsCtxView<'_> {
// if there isn't a disconnect in between.
// Step #1: Disconnect
- if socket.is_connected() {
- socket.disconnect()?;
- }
+ // if socket.is_connected() {
+ // socket.disconnect()?;
+ // }
// Step #2: (Re)connect
if let Some(connect_addr) = remote_address {
diff --git a/crates/wasi/src/sockets/udp.rs b/crates/wasi/src/sockets/udp.rs
index 482c6aa090..ba1e93f18d 100644
--- a/crates/wasi/src/sockets/udp.rs
+++ b/crates/wasi/src/sockets/udp.rs
@@ -162,10 +162,10 @@ impl UdpSocket {
// if there isn't a disconnect in between.
// Step #1: Disconnect
- if let UdpState::Connected(..) = self.udp_state {
- udp_disconnect(&self.socket)?;
- self.udp_state = UdpState::Bound;
- }
+ // if let UdpState::Connected(..) = self.udp_state {
+ // udp_disconnect(&self.socket)?;
+ // self.udp_state = UdpState::Bound;
+ // }
// Step #2: (Re)connect
connect(&self.socket, &addr).map_err(|error| match error {
Errno::AFNOSUPPORT => ErrorCode::InvalidArgument, // See `udp_bind` implementation.However, per the surrounding comments, that code is there for a reason, so I assume that removing it isn't the right solution. @badeend this seems to be related to #7411, so perhaps you have thoughts?
Activity
[-][wasmtime-wasi] reconnecting UDP socket unexpectedly changes local address[/-][+][wasi-sockets] reconnecting UDP socket unexpectedly changes local address[/+]badeend commentedon Feb 13, 2026
Every call to
udp-socket::connectis allowed to change the local socket address. This is documented on the method itself: https://github.com/WebAssembly/WASI/blob/41d0de898ec7568e032129156a3bc567d7ebea5d/proposals/sockets/wit-0.3.0-draft/types.wit#L547-L550AFAIK, this is standard POSIX behavior. Are you sure you're testing exactly the same code as the mio test? Because the mio test you linked reads: (no local address rebinding)
whereas the code snippet you posted rebinds
socket1:dicej commentedon Feb 13, 2026
No, it's not exactly the same; I reduced the
miotest to a minimal reproduction. Note that themiotest does callconnecttwice onsocket1: scroll down to https://github.com/tokio-rs/mio/blob/66ac9fab79bf191218488c4f35c99d13935b7e12/tests/udp_socket.rs#L442dicej commentedon Feb 13, 2026
BTW, if POSIX allows
connectto change the local address, then themiotest is just wrong, and I can submit a PR to fix it (e.g. by callingsocket3.connect(socket1.local_addr().unwrap()).unwrap()after thesocket1.connect(address3).unwrap()line). It would help to have a document to reference to justify that, though, since apparently WASI is the only platform where the test currently fails.dicej commentedon Feb 13, 2026
Oof, actually not sure that would work, because then
socket3's local address could change. So I guess I'm not even sure how I would fix it, since every call toconnectcould invalidate the local address previously used.alexcrichton commentedon Feb 13, 2026
@badeend do you have further links for where the clause you linked in the WASI docs come from? Naively to me that seems pretty surprising that
connectcan arbitrarily change the locally bound address, so I'd be curious to read up more there. Is this something, for example, where platforms disagree on behavior and WASI is specifying a sort of least-common-denominator semantics?badeend commentedon Feb 14, 2026
Yes, it is surprising, and Yes it does happen :P But only if the socket wasn't bound to a specific address/port.
Let's say, the first connect is to
127.0.0.1. The socket's local address will become some address on the loopback interface. Most likely, also127.0.0.1.Then, the same socket is reused to connect to
123.234.12.34. The loopback interface has no network path to that remote, so the local binding has to change in order to do something useful. On my machine (example below) that is192.168.178.13(windows),172.22.83.10(linux)I was not able to find any authoritative documentation describing this in detail, or: at all. The current WASI spec is purely based on observed behavior.
To make sure I'm not going mad, I just ran this test twice on Windows and Linux; once with wasmtime as-is (with the explicit disconnect), and once with the disconnect commented out:
Windows with disconnect
Windows without disconnect
Linux with disconnect
Linux without disconnect
Notes:
EINVALif the existing local binding is not suitable for the new remote. This appears to be Linux-specific and is the reason wasmtime currently performs the explicit disconnect.Their test consistently passes on major platforms, so it is not obviously incorrect. However, it does rely on behavior that is not strictly guaranteed. With the specific parameters they use, that behavior just happens to be stable across current platforms.
My takeaways:
bindthe socket instead of relying on undocumented behavior. That sidesteps all these problems.This likely restores the behavior expected by the mio test while still handling the Linux EINVAL case when necessary.
I've opened #12595 as a potential fix.
alexcrichton commentedon Feb 14, 2026
Oh wow that's fascinating! Thanks so much for taking the time to test and investigate this, and I agree that #12595 is the way to go here. Definitely learn something new every day :)
badeend commentedon Feb 16, 2026
For posterity; the PR passed CI without special considerations for MacOS. This implies that it indeed behaves the same as Windows.
badeend commentedon Feb 16, 2026
So... fun fact. The same thing happens on TCP sockets as well. 🧐
prints:
I've opened #12601 to test this
Update sockets documentation:
sockets: Update documentation (#884)