Skip to content

windows: consider skipping WSACleanup on exit #141799

@jstarks

Description

@jstarks

On Windows, std calls WSACleanup at exit if it called WSAStartup, to uninitialize Windows sockets:

pub fn cleanup() {
// only perform cleanup if network functionality was actually initialized
if let Some(cleanup) = WSA_CLEANUP.get() {
unsafe {
cleanup();
}
}
}

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

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on May 30, 2025
ChrisDenton

ChrisDenton commented on May 31, 2025

@ChrisDenton
Member

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.

linked a pull request that will close this issue on May 31, 2025
ChrisDenton

ChrisDenton commented on May 31, 2025

@ChrisDenton
Member

The only potential issue I'm hearing is that it may be flagged by some tools as a bug.

added
O-windowsOperating system: Windows
T-libsRelevant to the library team, which will review and decide on the PR/issue.
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jun 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`C-bugCategory: This is a bug.O-windowsOperating system: WindowsT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @ChrisDenton@jstarks@rustbot@Noratrieb

      Issue actions

        windows: consider skipping WSACleanup on exit · Issue #141799 · rust-lang/rust