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

Prefer SetHandleInformation to DuplicateHandle in set_close_on_exec #907

Merged
merged 1 commit into from
Dec 3, 2021

Conversation

MisterDA
Copy link
Contributor

@MisterDA MisterDA commented Dec 1, 2021

From DuplicateHandle:

You should not use DuplicateHandle to duplicate handles to the following objects:

  • […]
  • Sockets. No error is returned, but the duplicate handle may not be
    recognized by Winsock at the target process. Also, using
    DuplicateHandle interferes with internal reference counting on
    the underlying object. To duplicate a socket handle, use the
    WSADuplicateSocket function.

and from Inheriting Handles:

Use the SetHandleInformation function to control if an existing
handle is inheritable or not.

and from SetHandleInformation:

You can specify a handle to one of the following types of objects:
[…], socket, […].

So DuplicateHandle shouldn't be used on sockets, and it's safe to
replace it with SetHandleInformation.

@smorimoto
Copy link
Member

Could you check this as well @raphael-proust?

From [DuplicateHandle][]:

> You should not use `DuplicateHandle` to duplicate handles to the following objects:
>
> - […]
> - Sockets. No error is returned, but the duplicate handle may not be
>   recognized by Winsock at the target process. Also, using
>   `DuplicateHandle` interferes with internal reference counting on
>   the underlying object. To duplicate a socket handle, use the
>   `WSADuplicateSocket` function.

and from [Inheriting Handles][]:

> Use the `SetHandleInformation` function to control if an existing
> handle is inheritable or not.

and from [SetHandleInformation][]:

> You can specify a handle to one of the following types of objects:
> […], socket, […].

So `DuplicateHandle` shouldn't be used on sockets, and it's safe to
replace it with `SetHandleInformation`.

[DuplicateHandle]: https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle#remarks
[Inheriting Handles]: https://docs.microsoft.com/en-us/windows/win32/procthread/inheritance#inheriting-handles
[SetHandleinformation]: https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-sethandleinformation#parameters
@smorimoto smorimoto force-pushed the windows-duplicate-handle-socket branch from 3b6c723 to 2878a05 Compare December 2, 2021 13:18
@MisterDA
Copy link
Contributor Author

MisterDA commented Dec 2, 2021

Related to this PR: ocaml/ocaml#10697.

@smorimoto smorimoto merged commit a14da6c into ocsigen:master Dec 3, 2021
@MisterDA MisterDA deleted the windows-duplicate-handle-socket branch December 3, 2021 12:21
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.

None yet

2 participants