Skip to content

Commit

Permalink
Avoid coroutine lambda captures in tcp_listener
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Nov 24, 2024
1 parent b06d3e2 commit 6f49cab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
57 changes: 30 additions & 27 deletions nano/node/transport/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,40 +66,43 @@ void nano::transport::tcp_listener::start ()
throw;
}

task = nano::async::task (strand, [this] () -> asio::awaitable<void> {
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<void> 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 ()
Expand Down
1 change: 1 addition & 0 deletions nano/node/transport/tcp_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class tcp_listener final
nano::logger & logger;

private:
asio::awaitable<void> start_impl ();
asio::awaitable<void> run ();
asio::awaitable<void> wait_available_slots () const;

Expand Down

0 comments on commit 6f49cab

Please sign in to comment.