From 208f8de37b3d24999f5246cec59f64843b59e75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 19 Nov 2024 00:25:31 +0100 Subject: [PATCH] Avoid coroutine lambda captures in tcp_listener --- nano/node/transport/tcp_listener.cpp | 57 +++++++++++++++------------- nano/node/transport/tcp_listener.hpp | 1 + 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/nano/node/transport/tcp_listener.cpp b/nano/node/transport/tcp_listener.cpp index e80de72ddc..372d9bbbc6 100644 --- a/nano/node/transport/tcp_listener.cpp +++ b/nano/node/transport/tcp_listener.cpp @@ -66,40 +66,43 @@ void nano::transport::tcp_listener::start () throw; } - task = nano::async::task (strand, [this] () -> asio::awaitable { - try - { - logger.debug (nano::log::type::tcp_listener, "Starting acceptor"); + task = nano::async::task (strand, start_impl ()); - try - { - co_await run (); - } - catch (boost::system::system_error const & ex) - { - // Operation aborted is expected when cancelling the acceptor - debug_assert (ex.code () == asio::error::operation_aborted); - } - debug_assert (strand.running_in_this_thread ()); + cleanup_thread = std::thread ([this] { + nano::thread_role::set (nano::thread_role::name::tcp_listener); + run_cleanup (); + }); +} - logger.debug (nano::log::type::tcp_listener, "Stopped acceptor"); - } - catch (std::exception const & ex) +asio::awaitable nano::transport::tcp_listener::start_impl () +{ + try + { + logger.debug (nano::log::type::tcp_listener, "Starting acceptor"); + + try { - logger.critical (nano::log::type::tcp_listener, "Error: {}", ex.what ()); - release_assert (false); // Unexpected error + co_await run (); } - catch (...) + catch (boost::system::system_error const & ex) { - logger.critical (nano::log::type::tcp_listener, "Unknown error"); - release_assert (false); // Unexpected error + // Operation aborted is expected when cancelling the acceptor + debug_assert (ex.code () == asio::error::operation_aborted); } - }); + debug_assert (strand.running_in_this_thread ()); - cleanup_thread = std::thread ([this] { - nano::thread_role::set (nano::thread_role::name::tcp_listener); - run_cleanup (); - }); + logger.debug (nano::log::type::tcp_listener, "Stopped acceptor"); + } + catch (std::exception const & ex) + { + logger.critical (nano::log::type::tcp_listener, "Error: {}", ex.what ()); + release_assert (false); // Unexpected error + } + catch (...) + { + logger.critical (nano::log::type::tcp_listener, "Unknown error"); + release_assert (false); // Unexpected error + } } void nano::transport::tcp_listener::stop () diff --git a/nano/node/transport/tcp_listener.hpp b/nano/node/transport/tcp_listener.hpp index d2841aca85..8d10a180d8 100644 --- a/nano/node/transport/tcp_listener.hpp +++ b/nano/node/transport/tcp_listener.hpp @@ -95,6 +95,7 @@ class tcp_listener final nano::logger & logger; private: + asio::awaitable start_impl (); asio::awaitable run (); asio::awaitable wait_available_slots () const;