Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Unconditionally set SO_REUSEADDR (#12597)

Dave Bakker52f8f1986eba
diff --git a/crates/test-programs/src/bin/p3_sockets_tcp_bind.rs b/crates/test-programs/src/bin/p3_sockets_tcp_bind.rs
index 3202849084..b7b4cf05f0 100644
--- a/crates/test-programs/src/bin/p3_sockets_tcp_bind.rs
+++ b/crates/test-programs/src/bin/p3_sockets_tcp_bind.rs
@@ -56,31 +56,43 @@ async fn test_tcp_bind_reuseaddr(ip: IpAddress) {
let bind_addr = {
let listener1 = TcpSocket::create(ip.family()).unwrap();
- let bind_addr = attempt_random_port(ip, |bind_addr| listener1.bind(bind_addr)).unwrap();
-
- let mut accept = listener1.listen().unwrap();
-
- let connect_addr =
- IpSocketAddress::new(IpAddress::new_loopback(ip.family()), bind_addr.port());
- join!(
- async {
- client.connect(connect_addr).await.unwrap();
- },
- async {
- let sock = accept.next().await.unwrap();
- let (mut data_tx, data_rx) = wit_stream::new();
- join!(
- async {
- sock.send(data_rx).await.unwrap();
- },
- async {
- let remaining = data_tx.write_all(vec![0; 10]).await;
- assert!(remaining.is_empty());
- drop(data_tx);
- }
- );
- },
- );
+ listener1
+ .bind(IpSocketAddress::new(
+ IpAddress::new_loopback(ip.family()),
+ 0,
+ ))
+ .unwrap();
+
+ let bind_addr = listener1.get_local_address().unwrap();
+
+ // The listener socket must have at least one connection for the TIME_WAIT
+ // mechanism to kick in. So we'll create & accept a dummy connection
+ // before closing the listener:
+ {
+ let mut accept = listener1.listen().unwrap();
+
+ let connect_addr =
+ IpSocketAddress::new(IpAddress::new_loopback(ip.family()), bind_addr.port());
+ join!(
+ async {
+ client.connect(connect_addr).await.unwrap();
+ },
+ async {
+ let sock = accept.next().await.unwrap();
+ let (mut data_tx, data_rx) = wit_stream::new();
+ join!(
+ async {
+ sock.send(data_rx).await.unwrap();
+ },
+ async {
+ let remaining = data_tx.write_all(vec![0; 10]).await;
+ assert!(remaining.is_empty());
+ drop(data_tx);
+ }
+ );
+ },
+ );
+ }
bind_addr
};
diff --git a/crates/wasi/src/sockets/util.rs b/crates/wasi/src/sockets/util.rs
index 42fe011e7a..9623403807 100644
--- a/crates/wasi/src/sockets/util.rs
+++ b/crates/wasi/src/sockets/util.rs
@@ -337,13 +337,15 @@ pub fn tcp_bind(
socket: &tokio::net::TcpSocket,
local_address: SocketAddr,
) -> Result<(), ErrorCode> {
- // Automatically bypass the TIME_WAIT state when binding to a specific port
- // Unconditionally (re)set SO_REUSEADDR, even when the value is false.
- // This ensures we're not accidentally affected by any socket option
- // state left behind by a previous failed call to this method.
+ // From the WASI spec:
+ // > The bind operation shouldn't be affected by the TIME_WAIT state of a
+ // > recently closed socket on the same local address. In practice this
+ // > means that the SO_REUSEADDR socket option should be set implicitly on
+ // > all platforms, except on Windows where this is the default behavior
+ // > and SO_REUSEADDR performs something different.
#[cfg(not(windows))]
- if let Err(err) = sockopt::set_socket_reuseaddr(&socket, local_address.port() > 0) {
- return Err(err.into());
+ {
+ _ = sockopt::set_socket_reuseaddr(&socket, true);
}
// Perform the OS bind call.