Skip to content

Commit c3140e7

Browse files
committed
Only register WSACleanup if WSAStartup is actually ever called
1 parent e4ca166 commit c3140e7

File tree

1 file changed

+11
-6
lines changed
  • library/std/src/sys/windows

1 file changed

+11
-6
lines changed

library/std/src/sys/windows/net.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
use crate::cmp;
44
use crate::io::{self, IoSlice, IoSliceMut, Read};
5+
use crate::lazy::SyncOnceCell;
56
use crate::mem;
67
use crate::net::{Shutdown, SocketAddr};
78
use crate::ptr;
8-
use crate::sync::Once;
99
use crate::sys;
1010
use crate::sys::c;
1111
use crate::sys_common::net;
@@ -26,26 +26,31 @@ pub mod netc {
2626

2727
pub struct Socket(c::SOCKET);
2828

29-
static INIT: Once = Once::new();
29+
static WSA: SyncOnceCell<unsafe extern "system" fn() -> i32> = SyncOnceCell::new();
3030

3131
/// Checks whether the Windows socket interface has been started already, and
3232
/// if not, starts it.
3333
pub fn init() {
34-
INIT.call_once(|| unsafe {
34+
let _ = WSA.get_or_init(|| unsafe {
3535
let mut data: c::WSADATA = mem::zeroed();
3636
let ret = c::WSAStartup(
3737
0x202, // version 2.2
3838
&mut data,
3939
);
4040
assert_eq!(ret, 0);
41+
42+
// Only register `WSACleanup` if `WSAStartup` is actually ever called.
43+
// Workaround to prevent linking to `WS2_32.dll` when no network functionality is used.
44+
// See issue #85441.
45+
c::WSACleanup
4146
});
4247
}
4348

4449
pub fn cleanup() {
45-
if INIT.is_completed() {
46-
// only close the socket interface if it has actually been started
50+
// only perform cleanup if network functionality was actually initialized
51+
if let Some(cleanup) = WSA.get() {
4752
unsafe {
48-
c::WSACleanup();
53+
cleanup();
4954
}
5055
}
5156
}

0 commit comments

Comments
 (0)