Skip to content

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

check for and return WASIp2 UDP socket errors when receiving (#13933)

Joel Dice054b803fe281
diff --git a/crates/test-programs/src/bin/p2_udp_send_to_closed_receiver.rs b/crates/test-programs/src/bin/p2_udp_send_to_closed_receiver.rs
new file mode 100644
index 0000000000..8b74539ef5
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_udp_send_to_closed_receiver.rs
@@ -0,0 +1,53 @@
+use test_programs::wasi::clocks::monotonic_clock;
+use test_programs::wasi::io::poll;
+use test_programs::wasi::sockets::network::{
+ ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, Network,
+};
+use test_programs::wasi::sockets::udp::{OutgoingDatagram, UdpSocket};
+
+fn test_send_to_closed_receiver(net: &Network, family: IpAddressFamily) {
+ let unspecified_port = IpSocketAddress::new(IpAddress::new_loopback(family), 0);
+
+ let sender = UdpSocket::new(family).unwrap();
+ sender.blocking_bind(&net, unspecified_port).unwrap();
+
+ let receiver = UdpSocket::new(family).unwrap();
+ receiver.blocking_bind(&net, unspecified_port).unwrap();
+
+ let receiver_address = receiver.local_address().unwrap();
+ drop(receiver);
+ let (rx, tx) = sender.stream(Some(receiver_address)).unwrap();
+ tx.blocking_send(&[OutgoingDatagram {
+ data: b"hello".to_vec(),
+ remote_address: None,
+ }])
+ .unwrap();
+
+ let deadline = monotonic_clock::now() + 5_000_000_000;
+ loop {
+ match rx.receive(1) {
+ Ok(datagrams) if datagrams.is_empty() => {}
+ Ok(_) => panic!("expected error, got datagrams"),
+ Err(ErrorCode::ConnectionRefused | ErrorCode::ConnectionReset) => break,
+ Err(error) => panic!("unexpected error: {error:?}"),
+ }
+
+ let received = rx.subscribe();
+ let timeout = monotonic_clock::subscribe_instant(deadline);
+
+ for ready in poll::poll(&[&received, &timeout]) {
+ match ready {
+ 0 => break,
+ 1 => panic!("receive timed out instead of returning an error"),
+ _ => unreachable!(),
+ }
+ }
+ }
+}
+
+fn main() {
+ let net = Network::default();
+
+ test_send_to_closed_receiver(&net, IpAddressFamily::Ipv4);
+ test_send_to_closed_receiver(&net, IpAddressFamily::Ipv6);
+}
diff --git a/crates/test-programs/src/bin/p3_sockets_udp_send.rs b/crates/test-programs/src/bin/p3_sockets_udp_send.rs
index d0840e00ce..31d8bd824f 100644
--- a/crates/test-programs/src/bin/p3_sockets_udp_send.rs
+++ b/crates/test-programs/src/bin/p3_sockets_udp_send.rs
@@ -1,3 +1,6 @@
+use futures::future::{self, Either};
+use std::pin::pin;
+use test_programs::p3::wasi::clocks::monotonic_clock;
use test_programs::p3::wasi::sockets::types::{
ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, UdpSocket,
};
@@ -32,6 +35,29 @@ async fn test_unspecified_remote_addr(family: IpAddressFamily) {
assert!(matches!(result, Err(ErrorCode::InvalidArgument)));
}
+async fn test_send_to_closed_receiver(family: IpAddressFamily) {
+ let unspecified_port = IpSocketAddress::new(IpAddress::new_loopback(family), 0);
+
+ let sender = UdpSocket::create(family).unwrap();
+ sender.bind(unspecified_port).unwrap();
+
+ let receiver = UdpSocket::create(family).unwrap();
+ receiver.bind(unspecified_port).unwrap();
+
+ let receiver_address = receiver.get_local_address().unwrap();
+ drop(receiver);
+ sender.connect(receiver_address).unwrap();
+ sender.send(b"hello".to_vec(), None).await.unwrap();
+
+ let received = pin!(sender.receive());
+ let timeout = pin!(monotonic_clock::wait_for(5_000_000_000));
+ match future::select(received, timeout).await {
+ Either::Left((Err(ErrorCode::ConnectionRefused | ErrorCode::ConnectionReset), _)) => {}
+ Either::Left((received, _)) => panic!("unexpected result: {received:?}"),
+ Either::Right(_) => panic!("receive timed out instead of returning an error"),
+ }
+}
+
impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
async fn run() -> Result<(), ()> {
test_udp_send_without_bind_or_connect(IpAddressFamily::Ipv4).await;
@@ -43,6 +69,9 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
test_unspecified_remote_addr(IpAddressFamily::Ipv4).await;
test_unspecified_remote_addr(IpAddressFamily::Ipv6).await;
+ test_send_to_closed_receiver(IpAddressFamily::Ipv4).await;
+ test_send_to_closed_receiver(IpAddressFamily::Ipv6).await;
+
Ok(())
}
}
diff --git a/crates/wasi/tests/all/p2/async_.rs b/crates/wasi/tests/all/p2/async_.rs
index bf88a8131c..0e2417e1b6 100644
--- a/crates/wasi/tests/all/p2/async_.rs
+++ b/crates/wasi/tests/all/p2/async_.rs
@@ -372,6 +372,12 @@ async fn p2_udp_connect() {
run(P2_UDP_CONNECT_COMPONENT, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_udp_send_to_closed_receiver() {
+ run(P2_UDP_SEND_TO_CLOSED_RECEIVER_COMPONENT, |_| {})
+ .await
+ .unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_stream_pollable_correct() {
run(P2_STREAM_POLLABLE_CORRECT_COMPONENT, |_| {})
.await
diff --git a/crates/wasi/tests/all/p2/sync.rs b/crates/wasi/tests/all/p2/sync.rs
index 7608457a38..514c1b7ac3 100644
--- a/crates/wasi/tests/all/p2/sync.rs
+++ b/crates/wasi/tests/all/p2/sync.rs
@@ -346,6 +346,14 @@ fn p2_udp_connect() {
run(P2_UDP_CONNECT_COMPONENT, |_| {}).unwrap()
}
#[test_log::test]
+// This test is flaky on Windows. It consistently passes when run by itself but
+// consistently fails when run in combination with the `p2::async_` variation,
+// and we've thus far been unable to determine the reason.
+#[cfg_attr(windows, ignore = "This test is flaky on Windows.")]
+fn p2_udp_send_to_closed_receiver() {
+ run(P2_UDP_SEND_TO_CLOSED_RECEIVER_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
fn p2_stream_pollable_correct() {
run(P2_STREAM_POLLABLE_CORRECT_COMPONENT, |_| {}).unwrap()
}