Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use WSA_FLAG_NO_HANDLE_INHERIT on Windows for cloexec sockets #910

Conversation

MisterDA
Copy link
Contributor

@MisterDA MisterDA commented Dec 6, 2021

The call to

socket(domain, type, protocol);

is equivalent to

WSASocket(domain, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);

the latter offering more (needed) control over the creation of
sockets.

On Windows, in order to mark sockets non-inheritable (close-on-exec),
it is sometimes not sufficient to use SetHandleInformation. There's
some explanation why in 1 and KB2398202.

Introduced in the hotfix KB2398202 and supported on Windows 7 with
SP1, Windows Server 2008 R2 with SP1, and later, the flag
WSA_FLAG_NO_HANDLE_INHERIT can be used with WSASocket to create a
non-inheritable socket.

We can still call SetHandleInformation to clear the inheritable flag
on the socket in case the flag isn't supported (running on an early
Windows Vista or 7).

Related OCaml PR:

@MisterDA MisterDA changed the title Windows socket wsa socket wsa flag no handle inherit Use WSA_FLAG_NO_HANDLE_INHERIT on Windows for cloexec sockets Dec 6, 2021
@MisterDA
Copy link
Contributor Author

Please don't merge, the PR is also under review in OCaml. I'll "backport" the changes after review here too.

@MisterDA MisterDA marked this pull request as draft December 20, 2021 11:56
@MisterDA MisterDA force-pushed the windows-socket-WSASocket-WSA_FLAG_NO_HANDLE_INHERIT branch 2 times, most recently from 3f466ec to d0a6055 Compare February 9, 2022 10:53
The call to

    socket(domain, type, protocol);

is equivalent to

    WSASocket(domain, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED);

the latter offering more (needed) control over the creation of
sockets.
On Windows, in order to mark sockets non-inheritable (close-on-exec),
it is sometimes not sufficient to use SetHandleInformation. There's
some explanation why in [1] and [KB2398202].

Introduced in the hotfix [KB2398202] and supported on Windows 7 with
SP1, Windows Server 2008 R2 with SP1, and later, the flag
WSA_FLAG_NO_HANDLE_INHERIT can be used with WSASocket to create a
non-inheritable socket.

We can still call SetHandleInformation to clear the inheritable flag
on the socket in case the flag isn't supported (running on an early
Windows Vista or 7).

Related OCaml PR:
- ocaml/ocaml#10809.

[1]: https://stackoverflow.com/questions/12058911/can-tcp-socket-handles-be-set-not-inheritable
[KB2398202]: https://support.microsoft.com/en-us/topic/an-application-may-stop-responding-when-the-application-closes-a-socket-connection-or-shuts-down-41321a1f-d80c-6975-98df-ae499d63133c
@MisterDA MisterDA force-pushed the windows-socket-WSASocket-WSA_FLAG_NO_HANDLE_INHERIT branch from d0a6055 to e47ee13 Compare March 28, 2022 08:34
@MisterDA MisterDA marked this pull request as ready for review March 29, 2022 13:28
@MisterDA
Copy link
Contributor Author

MisterDA commented Apr 5, 2022

The corresponding PR was just merged in OCaml.

@smorimoto
Copy link
Member

Looks good to me (cc @raphael-proust)

@raphael-proust
Copy link
Collaborator

Looks ok to me although I don't know nearly enough about the windows system code to make sense of that part and I don't have a machine to test it on. Still, it looks ok and the Unix part definitely looks fine.

@raphael-proust raphael-proust merged commit 6d8eb07 into ocsigen:master Apr 12, 2022
@MisterDA MisterDA deleted the windows-socket-WSASocket-WSA_FLAG_NO_HANDLE_INHERIT branch April 12, 2022 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants