From fcc4435fa0208553f1d141e12348b1a36ab5b5de Mon Sep 17 00:00:00 2001 From: John Nunley Date: Thu, 25 Jan 2024 20:06:48 -0800 Subject: [PATCH 1/3] Initialize networking before running connect() This commit adds a setup_networking function. On most platforms it does nothing. However, on Windows it calls `wsa_startup`. This ensures that all networking is set up before anything else is called. Closes #182 Signed-off-by: John Nunley --- src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index cf35fd4..1d0489a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2069,6 +2069,8 @@ fn connect( #[cfg(windows)] use rustix::fd::AsFd; + setup_networking(); + #[cfg(any( target_os = "android", target_os = "dragonfly", @@ -2168,6 +2170,20 @@ fn connect( Ok(socket) } +#[inline] +fn setup_networking() { + #[cfg(windows)] + { + // On Windows, we need to call WSAStartup before calling any networking code. + // Make sure to call it at least once. + static INIT: std::sync::Once = std::sync::Once::new(); + + INIT.call_once(|| { + let _ = rustix::net::wsa_startup(); + }); + } +} + #[inline] fn set_nonblocking( #[cfg(unix)] fd: BorrowedFd<'_>, From 298568efa17d8056b69589eba0cf2ae1af68d6d0 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Thu, 25 Jan 2024 20:10:13 -0800 Subject: [PATCH 2/3] Set up a test Signed-off-by: John Nunley --- tests/issue_182.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/issue_182.rs diff --git a/tests/issue_182.rs b/tests/issue_182.rs new file mode 100644 index 0000000..1d4a342 --- /dev/null +++ b/tests/issue_182.rs @@ -0,0 +1,15 @@ +//! https://github.com/smol-rs/async-io/issues/182 + +use async_io::Async; +use std::net::{TcpStream, ToSocketAddrs}; + +#[test] +fn networking_initialized() { + let address = ToSocketAddrs::to_socket_addrs(&("google.com", 80)) + .unwrap() + .next() + .unwrap(); + async_io::block_on(async move { + let _ = Async::::connect(address).await.unwrap(); + }); +} From 874155479be6bc6c6ac0c1a156837a41ef43ea00 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sat, 27 Jan 2024 09:24:32 -0800 Subject: [PATCH 3/3] Ignore nslookup failure Signed-off-by: John Nunley --- tests/issue_182.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/issue_182.rs b/tests/issue_182.rs index 1d4a342..92aff96 100644 --- a/tests/issue_182.rs +++ b/tests/issue_182.rs @@ -5,10 +5,13 @@ use std::net::{TcpStream, ToSocketAddrs}; #[test] fn networking_initialized() { - let address = ToSocketAddrs::to_socket_addrs(&("google.com", 80)) - .unwrap() - .next() - .unwrap(); + let address = match ToSocketAddrs::to_socket_addrs(&("google.com", 80)) { + Ok(mut addrs) => addrs.next().unwrap(), + Err(err) => { + eprintln!("Got error {err} when looking up google.com, exiting test early."); + return; + } + }; async_io::block_on(async move { let _ = Async::::connect(address).await.unwrap(); });