From c99b93a338670ab9ff0cd55576ddc7a41222cf5b Mon Sep 17 00:00:00 2001 From: Alex Michon Date: Wed, 12 Jun 2024 10:21:52 +0200 Subject: [PATCH 1/2] 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 --- src/asio_service.cxx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/asio_service.cxx b/src/asio_service.cxx index a1fd31fe..eefcfe0a 100644 --- a/src/asio_service.cxx +++ b/src/asio_service.cxx @@ -1407,6 +1407,14 @@ 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::milliseconds( send_timeout_ms ) ) ); + operation_timer_.async_wait( std::bind( &asio_rpc_client::cancel_socket, + this, + std::placeholders::_1 ) ); + } } else { ptr rsp; ptr except @@ -1477,6 +1485,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() ); From 4c7ccdc8510e73753bf1ee7bcf0b98c8a06c58ba Mon Sep 17 00:00:00 2001 From: Jung-Sang Ahn Date: Tue, 18 Jun 2024 15:53:35 -0700 Subject: [PATCH 2/2] Indent and line breaking --- src/asio_service.cxx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/asio_service.cxx b/src/asio_service.cxx index eefcfe0a..ff84ce86 100644 --- a/src/asio_service.cxx +++ b/src/asio_service.cxx @@ -1411,9 +1411,10 @@ class asio_rpc_client operation_timer_.expires_after ( std::chrono::duration_cast ( std::chrono::milliseconds( send_timeout_ms ) ) ); - operation_timer_.async_wait( std::bind( &asio_rpc_client::cancel_socket, - this, - std::placeholders::_1 ) ); + operation_timer_.async_wait( + std::bind( &asio_rpc_client::cancel_socket, + this, + std::placeholders::_1 ) ); } } else { ptr rsp;