Skip to content

Commit

Permalink
Add timeout to async_connect operation (#516)
Browse files Browse the repository at this point in the history
* Add timeout to async_connect operation

If the network goes down during a async_connect, we may end up being
stuck forever in this function since there is no timeout to cancel it.

Signed-off-by: Alex Michon <[email protected]>

* Indent and line breaking

---------

Signed-off-by: Alex Michon <[email protected]>
Co-authored-by: Jung-Sang Ahn <[email protected]>
  • Loading branch information
amichon-kalray and greensky00 authored Jun 18, 2024
1 parent f7ac85f commit 8731295
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/asio_service.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,15 @@ class asio_rpc_client
send_timeout_ms,
std::placeholders::_1,
std::placeholders::_2 ) );
if (send_timeout_ms != 0) {
operation_timer_.expires_after
( std::chrono::duration_cast<std::chrono::nanoseconds>
( std::chrono::milliseconds( send_timeout_ms ) ) );
operation_timer_.async_wait(
std::bind( &asio_rpc_client::cancel_socket,
this,
std::placeholders::_1 ) );
}
} else {
ptr<resp_msg> rsp;
ptr<rpc_exception> except
Expand Down Expand Up @@ -1477,6 +1486,7 @@ class asio_rpc_client
std::error_code err,
asio::ip::tcp::resolver::iterator itor)
{
operation_timer_.cancel();
if (!err) {
p_in( "%p connected to %s:%s (as a client)",
this, host_.c_str(), port_.c_str() );
Expand Down

0 comments on commit 8731295

Please sign in to comment.