From 737cfd2196b31eb31931d6dbe3150cec720f22d1 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Mon, 2 Mar 2026 00:52:27 -0500 Subject: [PATCH] Fix race condition on m_tag_to_queue_map --- include/util/string.h | 12 ++++++------ src/librpc.cpp | 7 ++++--- src/mDNSDiscoveryService.cpp | 6 +++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/include/util/string.h b/include/util/string.h index 7f5b412..86cf5ec 100644 --- a/include/util/string.h +++ b/include/util/string.h @@ -4,11 +4,11 @@ #ifndef STRING_H #define STRING_H +#include +#include #include #include #include -#include -#include inline std::vector split(const std::string &str, const char delimiter) { std::vector result; @@ -22,15 +22,15 @@ inline std::vector split(const std::string &str, const char delimit return result; } -inline bool is_integer(const std::string& s) { - if (s.empty()) return false; +inline bool is_integer(const std::string &s) { + if (s.empty()) + return false; size_t start = 0; if (s[0] == '-' || s[0] == '+') start = 1; - return start < s.size() && - std::all_of(s.begin() + start, s.end(), ::isdigit); + return start < s.size() && std::all_of(s.begin() + start, s.end(), ::isdigit); } #endif // STRING_H diff --git a/src/librpc.cpp b/src/librpc.cpp index ffbf627..bc52d77 100644 --- a/src/librpc.cpp +++ b/src/librpc.cpp @@ -66,9 +66,10 @@ std::optional MessagingInterface::recv(uint8_t *buffer, const siz {tag, std::make_unique>>>( PER_TAG_MAX_QUEUE_SIZE)}); } + const auto &queue = m_tag_to_queue_map[tag]; lock.unlock(); - const auto data = m_tag_to_queue_map[tag]->dequeue(MAX_RECV_WAIT_TIME); + const auto data = queue->dequeue(MAX_RECV_WAIT_TIME); if (!data.has_value()) { return std::nullopt; @@ -139,10 +140,10 @@ void MessagingInterface::handle_recv() { std::make_unique>>>( PER_TAG_MAX_QUEUE_SIZE)}); } + const auto &queue = m_tag_to_queue_map[mpi_message->tag()]; lock.unlock(); - m_tag_to_queue_map[mpi_message->tag()]->enqueue(std::move(data.value()), - MAX_WAIT_TIME_TAG_ENQUEUE); + queue->enqueue(std::move(data.value()), MAX_WAIT_TIME_TAG_ENQUEUE); } } } diff --git a/src/mDNSDiscoveryService.cpp b/src/mDNSDiscoveryService.cpp index 2338d09..57dcac2 100644 --- a/src/mDNSDiscoveryService.cpp +++ b/src/mDNSDiscoveryService.cpp @@ -314,9 +314,9 @@ std::optional mDNSDiscoveryService::parse_response(uint8_t *buf if (split_string[0] == CONNECTED_MODULES_STR) { for (const auto connected_modules = split(split_string[1], ','); const auto &module_id : connected_modules) { - if (is_integer(module_id)) { - response.connected_module_ids.emplace_back(stoi(module_id)); - } + if (is_integer(module_id)) { + response.connected_module_ids.emplace_back(stoi(module_id)); + } } } }