From cde7b4ec67c387b687b74b1598c23f92be99d2e3 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Mon, 26 Oct 2020 15:48:43 +0100 Subject: [PATCH] Properly move the sockets to the handler threads At times like this you really wish you were writing Rust right now. --- src/common/communication.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/common/communication.h b/src/common/communication.h index 638cc94b..a240bf2c 100644 --- a/src/common/communication.h +++ b/src/common/communication.h @@ -116,7 +116,7 @@ inline T read_object(Socket& socket, {buffer.begin(), size}, object); if (BOOST_UNLIKELY(!success)) { - throw std::runtime_error("Deserialization failure in call:" + + throw std::runtime_error("Deserialization failure in call: " + std::string(__PRETTY_FUNCTION__)); } @@ -370,9 +370,12 @@ class EventHandler { [&](boost::asio::local::stream_protocol::socket secondary_socket) { const size_t request_id = next_request_id.fetch_add(1); + // We have to make sure to keep moving these sockets into the + // threads that will handle them std::lock_guard lock(active_secondary_requests_mutex); - active_secondary_requests[request_id] = - std::jthread([&, request_id]() { + active_secondary_requests[request_id] = std::jthread( + [&, request_id](boost::asio::local::stream_protocol::socket + secondary_socket) { // TODO: Factor this out auto event = read_object(secondary_socket); if (logging) { @@ -405,7 +408,8 @@ class EventHandler { // std::jthread active_secondary_requests.erase(request_id); }); - }); + }, + std::move(secondary_socket)); }); std::jthread secondary_requests_handler( @@ -465,9 +469,9 @@ class EventHandler { std::optional> logging, F callback) { acceptor.async_accept( - [&](const boost::system::error_code& error, + [&, logging, callback]( + const boost::system::error_code& error, boost::asio::local::stream_protocol::socket secondary_socket) { - // if (error.failed()) { if (logging) { auto [logger, is_dispatch] = *logging;