Hold weak ptr to node in socket (#4542)

This commit is contained in:
Piotr Wójcik 2024-04-08 13:02:32 +02:00 committed by GitHub
commit 59285bf8ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 99 additions and 46 deletions

View file

@ -23,7 +23,7 @@ nano::transport::socket::socket (nano::node & node_a, endpoint_type_t endpoint_t
send_queue{ max_queue_size_a }, send_queue{ max_queue_size_a },
strand{ node_a.io_ctx.get_executor () }, strand{ node_a.io_ctx.get_executor () },
tcp_socket{ node_a.io_ctx }, tcp_socket{ node_a.io_ctx },
node{ node_a }, node_w{ node_a.shared () },
endpoint_type_m{ endpoint_type_a }, endpoint_type_m{ endpoint_type_a },
timeout{ std::numeric_limits<uint64_t>::max () }, timeout{ std::numeric_limits<uint64_t>::max () },
last_completion_time_or_init{ nano::seconds_since_epoch () }, last_completion_time_or_init{ nano::seconds_since_epoch () },
@ -55,10 +55,18 @@ void nano::transport::socket::async_connect (nano::tcp_endpoint const & endpoint
tcp_socket.async_connect (endpoint_a, tcp_socket.async_connect (endpoint_a,
boost::asio::bind_executor (strand, boost::asio::bind_executor (strand,
[this_l = shared_from_this (), callback = std::move (callback_a), endpoint_a] (boost::system::error_code const & ec) { [this_l = shared_from_this (), callback = std::move (callback_a), endpoint_a] (boost::system::error_code const & ec) {
debug_assert (this_l->strand.running_in_this_thread ());
auto node_l = this_l->node_w.lock ();
if (!node_l)
{
return;
}
this_l->remote = endpoint_a; this_l->remote = endpoint_a;
if (ec) if (ec)
{ {
this_l->node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_connect_error, nano::stat::dir::in); node_l->stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_connect_error, nano::stat::dir::in);
this_l->close (); this_l->close ();
} }
else else
@ -69,7 +77,7 @@ void nano::transport::socket::async_connect (nano::tcp_endpoint const & endpoint
boost::system::error_code ec; boost::system::error_code ec;
this_l->local = this_l->tcp_socket.local_endpoint (ec); this_l->local = this_l->tcp_socket.local_endpoint (ec);
} }
this_l->node.observers.socket_connected.notify (*this_l); node_l->observers.socket_connected.notify (*this_l);
} }
callback (ec); callback (ec);
})); }));
@ -90,14 +98,20 @@ void nano::transport::socket::async_read (std::shared_ptr<std::vector<uint8_t>>
[this_l, buffer_a, cbk = std::move (callback)] (boost::system::error_code const & ec, std::size_t size_a) { [this_l, buffer_a, cbk = std::move (callback)] (boost::system::error_code const & ec, std::size_t size_a) {
debug_assert (this_l->strand.running_in_this_thread ()); debug_assert (this_l->strand.running_in_this_thread ());
auto node_l = this_l->node_w.lock ();
if (!node_l)
{
return;
}
if (ec) if (ec)
{ {
this_l->node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_read_error, nano::stat::dir::in); node_l->stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_read_error, nano::stat::dir::in);
this_l->close (); this_l->close ();
} }
else else
{ {
this_l->node.stats.add (nano::stat::type::traffic_tcp, nano::stat::dir::in, size_a); node_l->stats.add (nano::stat::type::traffic_tcp, nano::stat::dir::in, size_a);
this_l->set_last_completion (); this_l->set_last_completion ();
this_l->set_last_receive_time (); this_l->set_last_receive_time ();
} }
@ -116,11 +130,17 @@ void nano::transport::socket::async_read (std::shared_ptr<std::vector<uint8_t>>
void nano::transport::socket::async_write (nano::shared_const_buffer const & buffer_a, std::function<void (boost::system::error_code const &, std::size_t)> callback_a, nano::transport::traffic_type traffic_type) void nano::transport::socket::async_write (nano::shared_const_buffer const & buffer_a, std::function<void (boost::system::error_code const &, std::size_t)> callback_a, nano::transport::traffic_type traffic_type)
{ {
auto node_l = node_w.lock ();
if (!node_l)
{
return;
}
if (closed) if (closed)
{ {
if (callback_a) if (callback_a)
{ {
node.background ([callback = std::move (callback_a)] () { node_l->background ([callback = std::move (callback_a)] () {
callback (boost::system::errc::make_error_code (boost::system::errc::not_supported), 0); callback (boost::system::errc::make_error_code (boost::system::errc::not_supported), 0);
}); });
} }
@ -132,7 +152,7 @@ void nano::transport::socket::async_write (nano::shared_const_buffer const & buf
{ {
if (callback_a) if (callback_a)
{ {
node.background ([callback = std::move (callback_a)] () { node_l->background ([callback = std::move (callback_a)] () {
callback (boost::system::errc::make_error_code (boost::system::errc::not_supported), 0); callback (boost::system::errc::make_error_code (boost::system::errc::not_supported), 0);
}); });
} }
@ -170,15 +190,21 @@ void nano::transport::socket::write_queued_messages ()
boost::asio::bind_executor (strand, [this_l = shared_from_this (), next /* `next` object keeps buffer in scope */] (boost::system::error_code ec, std::size_t size) { boost::asio::bind_executor (strand, [this_l = shared_from_this (), next /* `next` object keeps buffer in scope */] (boost::system::error_code ec, std::size_t size) {
debug_assert (this_l->strand.running_in_this_thread ()); debug_assert (this_l->strand.running_in_this_thread ());
auto node_l = this_l->node_w.lock ();
if (!node_l)
{
return;
}
this_l->write_in_progress = false; this_l->write_in_progress = false;
if (ec) if (ec)
{ {
this_l->node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_write_error, nano::stat::dir::in); node_l->stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_write_error, nano::stat::dir::in);
this_l->close (); this_l->close ();
} }
else else
{ {
this_l->node.stats.add (nano::stat::type::traffic_tcp, nano::stat::dir::out, size); node_l->stats.add (nano::stat::type::traffic_tcp, nano::stat::dir::out, size);
this_l->set_last_completion (); this_l->set_last_completion ();
} }
@ -233,10 +259,25 @@ void nano::transport::socket::set_last_receive_time ()
void nano::transport::socket::ongoing_checkup () void nano::transport::socket::ongoing_checkup ()
{ {
std::weak_ptr<nano::transport::socket> this_w (shared_from_this ()); auto node_l = node_w.lock ();
node.workers.add_timed_task (std::chrono::steady_clock::now () + std::chrono::seconds (node.network_params.network.is_dev_network () ? 1 : 5), [this_w] () { if (!node_l)
if (auto this_l = this_w.lock ())
{ {
return;
}
node_l->workers.add_timed_task (std::chrono::steady_clock::now () + std::chrono::seconds (node_l->network_params.network.is_dev_network () ? 1 : 5), [this_w = weak_from_this ()] () {
auto this_l = this_w.lock ();
if (!this_l)
{
return;
}
auto node_l = this_l->node_w.lock ();
if (!node_l)
{
return;
}
// If the socket is already dead, close just in case, and stop doing checkups // If the socket is already dead, close just in case, and stop doing checkups
if (!this_l->alive ()) if (!this_l->alive ())
{ {
@ -250,7 +291,7 @@ void nano::transport::socket::ongoing_checkup ()
// if this is a server socket, and no data is received for silent_connection_tolerance_time seconds then disconnect // if this is a server socket, and no data is received for silent_connection_tolerance_time seconds then disconnect
if (this_l->endpoint_type () == endpoint_type_t::server && (now - this_l->last_receive_time_or_init) > static_cast<uint64_t> (this_l->silent_connection_tolerance_time.count ())) if (this_l->endpoint_type () == endpoint_type_t::server && (now - this_l->last_receive_time_or_init) > static_cast<uint64_t> (this_l->silent_connection_tolerance_time.count ()))
{ {
this_l->node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_silent_connection_drop, nano::stat::dir::in); node_l->stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_silent_connection_drop, nano::stat::dir::in);
condition_to_disconnect = true; condition_to_disconnect = true;
} }
@ -258,14 +299,14 @@ void nano::transport::socket::ongoing_checkup ()
// if there is no activity for timeout seconds then disconnect // if there is no activity for timeout seconds then disconnect
if ((now - this_l->last_completion_time_or_init) > this_l->timeout) if ((now - this_l->last_completion_time_or_init) > this_l->timeout)
{ {
this_l->node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, this_l->endpoint_type () == endpoint_type_t::server ? nano::stat::dir::in : nano::stat::dir::out); node_l->stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, this_l->endpoint_type () == endpoint_type_t::server ? nano::stat::dir::in : nano::stat::dir::out);
condition_to_disconnect = true; condition_to_disconnect = true;
} }
if (condition_to_disconnect) if (condition_to_disconnect)
{ {
this_l->node.logger.debug (nano::log::type::tcp_server, "Closing socket due to timeout ({})", nano::util::to_str (this_l->remote)); node_l->logger.debug (nano::log::type::tcp_server, "Closing socket due to timeout ({})", nano::util::to_str (this_l->remote));
this_l->timed_out = true; this_l->timed_out = true;
this_l->close (); this_l->close ();
@ -274,15 +315,20 @@ void nano::transport::socket::ongoing_checkup ()
{ {
this_l->ongoing_checkup (); this_l->ongoing_checkup ();
} }
}
}); });
} }
void nano::transport::socket::read_impl (std::shared_ptr<std::vector<uint8_t>> const & data_a, std::size_t size_a, std::function<void (boost::system::error_code const &, std::size_t)> callback_a) void nano::transport::socket::read_impl (std::shared_ptr<std::vector<uint8_t>> const & data_a, std::size_t size_a, std::function<void (boost::system::error_code const &, std::size_t)> callback_a)
{ {
auto node_l = node_w.lock ();
if (!node_l)
{
return;
}
// Increase timeout to receive TCP header (idle server socket) // Increase timeout to receive TCP header (idle server socket)
auto const prev_timeout = get_default_timeout_value (); auto const prev_timeout = get_default_timeout_value ();
set_default_timeout_value (node.network_params.network.idle_timeout); set_default_timeout_value (node_l->network_params.network.idle_timeout);
async_read (data_a, size_a, [callback_l = std::move (callback_a), prev_timeout, this_l = shared_from_this ()] (boost::system::error_code const & ec_a, std::size_t size_a) { async_read (data_a, size_a, [callback_l = std::move (callback_a), prev_timeout, this_l = shared_from_this ()] (boost::system::error_code const & ec_a, std::size_t size_a) {
this_l->set_default_timeout_value (prev_timeout); this_l->set_default_timeout_value (prev_timeout);
callback_l (ec_a, size_a); callback_l (ec_a, size_a);
@ -314,6 +360,12 @@ void nano::transport::socket::close ()
// This must be called from a strand or the destructor // This must be called from a strand or the destructor
void nano::transport::socket::close_internal () void nano::transport::socket::close_internal ()
{ {
auto node_l = node_w.lock ();
if (!node_l)
{
return;
}
if (closed.exchange (true)) if (closed.exchange (true))
{ {
return; return;
@ -330,8 +382,8 @@ void nano::transport::socket::close_internal ()
if (ec) if (ec)
{ {
node.stats.inc (nano::stat::type::socket, nano::stat::detail::error_socket_close); node_l->stats.inc (nano::stat::type::socket, nano::stat::detail::error_socket_close);
node.logger.error (nano::log::type::socket, "Failed to close socket gracefully: {} ({})", ec.message (), nano::util::to_str (remote)); node_l->logger.error (nano::log::type::socket, "Failed to close socket gracefully: {} ({})", ec.message (), nano::util::to_str (remote));
} }
} }

View file

@ -153,7 +153,8 @@ private:
protected: protected:
boost::asio::strand<boost::asio::io_context::executor_type> strand; boost::asio::strand<boost::asio::io_context::executor_type> strand;
boost::asio::ip::tcp::socket tcp_socket; boost::asio::ip::tcp::socket tcp_socket;
nano::node & node;
std::weak_ptr<nano::node> node_w;
/** The other end of the connection */ /** The other end of the connection */
boost::asio::ip::tcp::endpoint remote; boost::asio::ip::tcp::endpoint remote;