File tree Expand file tree Collapse file tree 1 file changed +11
-6
lines changed
library/std/src/sys/windows Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change 22
33use crate :: cmp;
44use crate :: io:: { self , IoSlice , IoSliceMut , Read } ;
5+ use crate :: lazy:: SyncOnceCell ;
56use crate :: mem;
67use crate :: net:: { Shutdown , SocketAddr } ;
78use crate :: ptr;
8- use crate :: sync:: Once ;
99use crate :: sys;
1010use crate :: sys:: c;
1111use crate :: sys_common:: net;
@@ -26,26 +26,31 @@ pub mod netc {
2626
2727pub 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.
3333pub 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
4449pub 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}
You can’t perform that action at this time.
0 commit comments