From fc5c2d5c09b0a47778e1d03648833cac64b5408a Mon Sep 17 00:00:00 2001 From: Michael Beardsworth Date: Wed, 8 May 2024 14:35:16 -0700 Subject: [PATCH] Guard relayAddrs with a mutex. Signed-off-by: Michael Beardsworth --- include/gz/transport/Discovery.hh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/gz/transport/Discovery.hh b/include/gz/transport/Discovery.hh index 02888980..1bb4a9b4 100644 --- a/include/gz/transport/Discovery.hh +++ b/include/gz/transport/Discovery.hh @@ -64,6 +64,7 @@ #include #include #include +#include #include #include #include @@ -744,6 +745,7 @@ namespace gz /// \param[in] _ip New IP address. public: void AddRelayAddress(const std::string &_ip) { + std::lock_guard lock(this->relayAddrsMutex); // Sanity check: Make sure that this IP address is not already saved. for (auto const &addr : this->relayAddrs) { @@ -766,6 +768,8 @@ namespace gz { std::vector result; + std::shared_lock lock(this->relayAddrsMutex); + for (auto const &addr : this->relayAddrs) { result.push_back(inet_ntoa(addr.sin_addr)); } @@ -1287,6 +1291,8 @@ namespace gz if (_msg.SerializeToArray(buffer + sizeof(msgSize), msgSize)) { // Send the discovery message to the unicast relays. + std::shared_lock lock(this->relayAddrsMutex); + for (const auto &sockAddr : this->relayAddrs) { errno = 0; @@ -1548,6 +1554,9 @@ namespace gz /// \brief Collection of socket addresses used as remote relays. private: std::vector relayAddrs; + /// \brief Mutex to guarantee exclusive write access to relayAddrs. + private: mutable std::shared_mutex relayAddrsMutex; + /// \brief Mutex to guarantee exclusive access between the threads. private: mutable std::mutex mutex;