Repository navigation
Use WSA_FLAG_NO_HANDLE_INHERIT on Windows for cloexec sockets - #910
Merged
raphael-proust merged 2 commits intoApr 12, 2022
Merged
raphael-proust merged 2 commits into
raphael-proust merged 2 commits into
Conversation
WSA_FLAG_NO_HANDLE_INHERIT on Windows for cloexec sockets
smorimoto
approved these changes
Dec 20, 2021
Contributor
Author
|
Please don't merge, the PR is also under review in OCaml. I'll "backport" the changes after review here too. |
MisterDA
marked this pull request as draft
December 20, 2021 11:56
MisterDA
force-pushed
the
windows-socket-WSASocket-WSA_FLAG_NO_HANDLE_INHERIT
branch
2 times, most recently
from
February 9, 2022 10:53
3f466ec to
d0a6055
Compare
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
force-pushed
the
windows-socket-WSASocket-WSA_FLAG_NO_HANDLE_INHERIT
branch
from
March 28, 2022 08:34
d0a6055 to
e47ee13
Compare
MisterDA
marked this pull request as ready for review
March 29, 2022 13:28
Contributor
Author
|
The corresponding PR was just merged in OCaml. |
Member
|
Looks good to me (cc @raphael-proust) |
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. |
MisterDA
deleted the
windows-socket-WSASocket-WSA_FLAG_NO_HANDLE_INHERIT
branch
April 12, 2022 08:42
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The call to
is equivalent to
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'ssome 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_INHERITcan be used withWSASocketto create anon-inheritable socket.
We can still call
SetHandleInformationto clear the inheritable flagon the socket in case the flag isn't supported (running on an early
Windows Vista or 7).
Related OCaml PR:
socketwithWSASocketand setWSA_FLAG_NO_HANDLE_INHERITocaml/ocaml#10809.