Description
On Windows, std
calls WSACleanup
at exit if it called WSAStartup
, to uninitialize Windows sockets:
rust/library/std/src/sys/net/connection/socket/windows.rs
Lines 137 to 144 in 15825b7
This is an unnecessary expense--the process is going away, so there's nothing WSACleanup
can do that won't also be done by the kernel when the process terminates. In a short-lived process, the call takes about 60us if you've only used TCP. If you also have used AF_UNIX and AF_HYPERV, for example, it takes about 400us--this is because it has to unload some DLLs that were dynamically loaded. If some of the data is cold, it has the potential to take much longer. None of this is necessary to "cleanly" terminate a process.
Now, arguably, you might want to call WSACleanup
on DLL unload, to ensure you've cleaned everything up. But Rust does not do this today, AFAICT, just as it doesn't drop statics, either on DLL unload or on clean exit from main
. So this WSACleanup
behavior seems inconsistent.
@ChrisDenton wdyt?
Activity
ChrisDenton commentedon May 31, 2025
Seems reasonable to me. I looked back at the history here and it seems it was added in the initial implementation ~11 years ago and more or less kept ever since.
I'd add that std doesn't really support unloading itself. Not that it isn't possible but we don't have any test coverage for that and doing it soundly is very much on the user, as far as I'm aware.
ChrisDenton commentedon May 31, 2025
The only potential issue I'm hearing is that it may be flagged by some tools as a bug.