From 2a8a51129f7fa916dac6fb721b774784f42b360d Mon Sep 17 00:00:00 2001 From: Lee *!* Clagett Date: Tue, 24 Aug 2021 19:25:45 -0400 Subject: [PATCH] Quicker resource cleanup on p2p socks timeout --- src/net/socks.h | 7 +++++++ src/p2p/net_node.cpp | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/src/net/socks.h b/src/net/socks.h index 739c972ab..506b53195 100644 --- a/src/net/socks.h +++ b/src/net/socks.h @@ -201,6 +201,13 @@ namespace socks std::shared_ptr self_; void operator()(boost::system::error_code error = boost::system::error_code{}); }; + + //! Calls `async_close` on `self` at destruction. NOP if `nullptr`. + struct close_on_exit + { + std::shared_ptr self; + ~close_on_exit() { async_close{std::move(self)}(); } + }; }; template diff --git a/src/p2p/net_node.cpp b/src/p2p/net_node.cpp index 84cc1581e..d3034b081 100644 --- a/src/p2p/net_node.cpp +++ b/src/p2p/net_node.cpp @@ -339,6 +339,7 @@ namespace nodetool } }; + net::socks::client::close_on_exit close_client{}; boost::unique_future socks_result{}; { boost::promise socks_promise{}; @@ -347,6 +348,7 @@ namespace nodetool auto client = net::socks::make_connect_client( boost::asio::ip::tcp::socket{service}, net::socks::version::v4a, notify{std::move(socks_promise)} ); + close_client.self = client; if (!start_socks(std::move(client), proxy, remote)) return boost::none; } @@ -368,7 +370,10 @@ namespace nodetool { auto result = socks_result.get(); if (!result.first) + { + close_client.self.reset(); return {std::move(result.second)}; + } MERROR("Failed to make socks connection to " << remote.str() << " (via " << proxy << "): " << result.first.message()); }