Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize networking on Windows #183

Merged
merged 3 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,8 @@ fn connect(
#[cfg(windows)]
use rustix::fd::AsFd;

setup_networking();

#[cfg(any(
target_os = "android",
target_os = "dragonfly",
Expand Down Expand Up @@ -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<'_>,
Expand Down
18 changes: 18 additions & 0 deletions tests/issue_182.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! https://github.com/smol-rs/async-io/issues/182

use async_io::Async;
use std::net::{TcpStream, ToSocketAddrs};

#[test]
fn networking_initialized() {
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::<TcpStream>::connect(address).await.unwrap();
});
}
Loading