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?

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

    No branches or pull requests

    Issue actions