Skip to content

Close the socket gracefully on Windows #284

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

Merged
merged 2 commits into from
Jun 20, 2023

Conversation

BewareMyPower
Copy link
Contributor

Fixes #261

Motivation

When closing the socket on Windows, the ERROR_CONNECTION_ABORTED error might be returned in the callback of async_receive. It's caused by the socket::close method is not portable enough, see https://www.boost.org/doc/libs/1_81_0/doc/html/boost_asio/reference/basic_stream_socket/close/overload2.html

For portable behaviour with respect to graceful closure of a connected socket, call shutdown() before closing the socket.

Modifications

Call shutdown in ClientConnection::closeSocket before calling close. In addition, when no bytes are returned or the eof error happened, print the logs with debug level whatever the error code is in the read callback.

Documentation

  • doc-required
    (Your PR needs to update docs and you will update later)

  • doc-not-needed
    (Please explain why)

  • doc
    (Your PR contains doc changes)

  • doc-complete
    (Docs have been already added)

Fixes apache#261

### Motivation

When closing the socket on Windows, the `ERROR_CONNECTION_ABORTED` error
might be returned in the callback of `async_receive`. It's caused by the
`socket::close` method is not portable enough, see
https://www.boost.org/doc/libs/1_81_0/doc/html/boost_asio/reference/basic_stream_socket/close/overload2.html

> For portable behaviour with respect to graceful closure of a connected socket, call shutdown() before closing the socket.

### Modifications

Call `shutdown` in `ClientConnection::closeSocket` before calling
`close`. In addition, when no bytes are returned or the `eof` error
happened,  print the logs with debug level whatever the error code
is in the read callback.
@BewareMyPower BewareMyPower added this to the 3.3.0 milestone Jun 14, 2023
@BewareMyPower BewareMyPower self-assigned this Jun 14, 2023
@BewareMyPower
Copy link
Contributor Author

Here are some tests results with debug logs based on this patch.

Without calling shutdown, the error log is always:

# error code: connection_aborted ("The network connection was aborted by the local system")
2023-06-14 13:14:15.190 DEBUG [11468] ..\..\..\lib\ClientConnection:615 | [[::1]:10740 -> [::1]:6650] Server closed the connection:  由本地系统中止网络连接。

With calling shutdown, the error log will be:

# error code: operation_aborted ("Operation cancelled")
2023-06-14 13:15:08.607 DEBUG [21080] ..\..\..\lib\ClientConnection:613 | [[::1]:10745 -> [::1]:6650] Read operation was canceled: 由于线程退出或应用程序请求,已中止 I/O 操作。

There is a little chance that the error code could be eof (value() is 2 on Windows) when I debugged it in my IDE so I handled the EOF error the same way with bytesTransferred == 0 because they both mean the socket client calls recv while the socket server has already closed.

image

@shibd shibd merged commit 8d5a412 into apache:main Jun 20, 2023
@BewareMyPower BewareMyPower deleted the bewaremypower/fix-win-socket-close branch June 20, 2023 12:19
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.

[Bug] Unexpected error logs when closing client on Windows
3 participants