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

Change to shutdown the connection directly in case the server do not participate close. #212

Merged
merged 4 commits into from
Mar 29, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 14 additions & 34 deletions src/common/http/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -503,23 +503,13 @@ response_t HttpImpl::Post(
response_t res(new beast::http::response<beast::http::string_body>);
beast::http::async_read(stream, buffer, *res, yield);

// Gracefully close the socket.
// Receive an error code instead of throwing an exception if this fails, so
// we can ignore some expected not_connected errors.
boost::system::error_code ec;
stream.async_shutdown(yield[ec]);

if (ec) {
// not_connected happens sometimes so don't bother reporting it.
// stream_truncated also happens sometime and we choose to ignore the
// stream_truncated error, as recommended by the github thread:
// https://github.com/boostorg/beast/issues/824
if (ec != beast::errc::not_connected &&
ec != boost::asio::ssl::error::stream_truncated) {
spdlog::error("{}: HTTP error encountered: {}", __func__, ec.message());
return response_t();
}
}
spdlog::trace("{}: closing connection, response payload size {}", __func__,
res->payload_size().value());
// Close the socket. We already got the response we need so close the socket
// directly. We choose not to use `ssl_stream::async_shutdown` since in some
// case, the HTTPS server does not participate with closing
// stream/connection. That would make the async_shutdown waiting forever.
beast::get_lowest_layer(stream).close();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the cause of this change. This problem is rooted from asio's implementation and I think it is better to use timeout to wait ack of close_notify message. (But async_shutdown doesn't have this feature. ref chriskohlhoff/asio#650)
My proposal is to note that this fix is like a workaround here and may be fixed in future work.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the timeout idea.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that post as well. And my initial attempt, eeb5776 was trying to use a timer, but I didn't managed to get it work properly, lack of C++ and boost familiarity ... :(

Added a tracker and a TODO. #214

Copy link
Collaborator

@Shikugawa Shikugawa Mar 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that eeb5776 won't work because the timer callback won't be executed before async_shutdown will have done ;) This is why we should wait the fix of boost/asio internal. Thanks.

Shikugawa marked this conversation as resolved.
Show resolved Hide resolved

return res;
// If we get here then the connection is closed gracefully
Expand Down Expand Up @@ -630,23 +620,13 @@ response_t HttpImpl::Get(
response_t res(new beast::http::response<beast::http::string_body>);
beast::http::async_read(stream, buffer, *res, yield);

// Gracefully close the socket.
// Receive an error code instead of throwing an exception if this fails, so
// we can ignore some expected not_connected errors.
boost::system::error_code ec;
stream.async_shutdown(yield[ec]);

if (ec) {
// not_connected happens sometimes so don't bother reporting it.
// stream_truncated also happens sometime and we choose to ignore the
// stream_truncated error, as recommended by the github thread:
// https://github.com/boostorg/beast/issues/824
if (ec != beast::errc::not_connected &&
ec != boost::asio::ssl::error::stream_truncated) {
spdlog::error("{}: HTTP error encountered: {}", __func__, ec.message());
return response_t();
}
}
spdlog::trace("{}: closing connection, response payload size {}", __func__,
res->payload_size().value());
// Close the socket. We already got the response we need so close the socket
// directly. We choose not to use `ssl_stream::async_shutdown` since in some
// case, the HTTPS server does not participate with closing
// stream/connection. That would make the async_shutdown waiting forever.
beast::get_lowest_layer(stream).close();

return res;
// If we get here then the connection is closed gracefully
Expand Down