Skip to content

Commit af1c39c

Browse files
committed
Auto merge of #24211 - alexcrichton:windows-wsa-flag-overlapped, r=aturon
This commit modifies the socket creation functions on windows to always specify the `WSA_FLAG_OVERLAPPED` and `WSA_FLAG_NO_HANDLE_INHERIT` flags by default. The overlapped flag enables IOCP APIs on Windows to be used with the socket at no cost, enabling better interoperation with external libraries. The no handle inherit flag mirrors the upcoming change to Unix to set CLOEXEC by default for all handles. Closes #24206
2 parents c773442 + 433f0e8 commit af1c39c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/libstd/sys/windows/c.rs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub const WSA_WAIT_TIMEOUT: libc::DWORD = libc::consts::os::extra::WAIT_TIMEOUT;
4444
pub const WSA_WAIT_EVENT_0: libc::DWORD = libc::consts::os::extra::WAIT_OBJECT_0;
4545
pub const WSA_WAIT_FAILED: libc::DWORD = libc::consts::os::extra::WAIT_FAILED;
4646
pub const WSAESHUTDOWN: libc::c_int = 10058;
47+
pub const WSA_FLAG_OVERLAPPED: libc::DWORD = 0x01;
48+
pub const WSA_FLAG_NO_HANDLE_INHERIT: libc::DWORD = 0x80;
4749

4850
pub const ERROR_NO_MORE_FILES: libc::DWORD = 18;
4951
pub const TOKEN_READ: libc::DWORD = 0x20008;

src/libstd/sys/windows/net.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ impl Socket {
8080
SocketAddr::V4(..) => libc::AF_INET,
8181
SocketAddr::V6(..) => libc::AF_INET6,
8282
};
83-
match unsafe { libc::socket(fam, ty, 0) } {
83+
let socket = unsafe {
84+
c::WSASocketW(fam, ty, 0, 0 as *mut _, 0,
85+
c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT)
86+
};
87+
match socket {
8488
INVALID_SOCKET => Err(last_error()),
8589
n => Ok(Socket(n)),
8690
}
@@ -103,7 +107,9 @@ impl Socket {
103107
match c::WSASocketW(info.iAddressFamily,
104108
info.iSocketType,
105109
info.iProtocol,
106-
&mut info, 0, 0) {
110+
&mut info, 0,
111+
c::WSA_FLAG_OVERLAPPED |
112+
c::WSA_FLAG_NO_HANDLE_INHERIT) {
107113
INVALID_SOCKET => Err(last_error()),
108114
n => Ok(Socket(n)),
109115
}

0 commit comments

Comments
 (0)