Skip to content

Prefer SetHandleInformation to DuplicateHandle in set_close_on_exec - #907

Merged
smorimoto merged 1 commit into
ocsigen:masterfrom
MisterDA:windows-duplicate-handle-socket
Dec 3, 2021
Merged

smorimoto merged 1 commit into
ocsigen:masterfrom
MisterDA:windows-duplicate-handle-socket

Conversation

@MisterDA

@MisterDA MisterDA commented Dec 1, 2021

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

MisterDA commented Dec 2, 2021

Copy link
Copy Markdown
Contributor Author

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.

2 participants