Skip to content

Commit

Permalink
Guard relayAddrs with a mutex.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Beardsworth <[email protected]>
  • Loading branch information
mbeards committed May 8, 2024
1 parent 5fd12a2 commit fc5c2d5
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/gz/transport/Discovery.hh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include <map>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <string>
#include <thread>
#include <vector>
Expand Down Expand Up @@ -744,6 +745,7 @@ namespace gz
/// \param[in] _ip New IP address.
public: void AddRelayAddress(const std::string &_ip)
{
std::lock_guard<std::shared_mutex> lock(this->relayAddrsMutex);
// Sanity check: Make sure that this IP address is not already saved.
for (auto const &addr : this->relayAddrs)
{
Expand All @@ -766,6 +768,8 @@ namespace gz
{
std::vector<std::string> result;

std::shared_lock<std::shared_mutex> lock(this->relayAddrsMutex);

for (auto const &addr : this->relayAddrs) {
result.push_back(inet_ntoa(addr.sin_addr));
}
Expand Down Expand Up @@ -1287,6 +1291,8 @@ namespace gz
if (_msg.SerializeToArray(buffer + sizeof(msgSize), msgSize))
{
// Send the discovery message to the unicast relays.
std::shared_lock<std::shared_mutex> lock(this->relayAddrsMutex);

for (const auto &sockAddr : this->relayAddrs)
{
errno = 0;
Expand Down Expand Up @@ -1548,6 +1554,9 @@ namespace gz
/// \brief Collection of socket addresses used as remote relays.
private: std::vector<sockaddr_in> 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;

Expand Down

0 comments on commit fc5c2d5

Please sign in to comment.