Fix race condition on m_tag_to_queue_map

This commit is contained in:
2026-03-02 00:52:27 -05:00
parent 9a0d2c2f99
commit 737cfd2196
3 changed files with 13 additions and 12 deletions

View File

@@ -4,11 +4,11 @@
#ifndef STRING_H
#define STRING_H
#include <algorithm>
#include <cctype>
#include <sstream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>
inline std::vector<std::string> split(const std::string &str, const char delimiter) {
std::vector<std::string> result;
@@ -22,15 +22,15 @@ inline std::vector<std::string> 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

View File

@@ -66,9 +66,10 @@ std::optional<SizeAndSource> MessagingInterface::recv(uint8_t *buffer, const siz
{tag, std::make_unique<BlockingQueue<std::unique_ptr<std::vector<uint8_t>>>>(
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<BlockingQueue<std::unique_ptr<std::vector<uint8_t>>>>(
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);
}
}
}

View File

@@ -314,9 +314,9 @@ std::optional<mDNSRobotModule> 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));
}
}
}
}