add mutex locking for the peer status, to prevent segfaults;

pull/75/head
Bryan Biedenkapp 1 year ago
parent 2893ed20d4
commit bc05a4d06e

@ -188,7 +188,10 @@ int HostWS::run()
if (peerStatusUpdate.isRunning() && peerStatusUpdate.hasExpired()) {
peerStatusUpdate.start();
getNetwork()->lockPeerStatus();
std::map<uint32_t, json::object> peerStatus(getNetwork()->peerStatus.begin(), getNetwork()->peerStatus.end());
getNetwork()->unlockPeerStatus();
for (auto entry : peerStatus) {
json::object wsObj = json::object();
std::string type = "peer_status";

@ -452,7 +452,10 @@ public:
void update()
{
const auto& rootWidget = getRootWidget();
getNetwork()->lockPeerStatus();
std::map<uint32_t, json::object> peerStatus(getNetwork()->peerStatus.begin(), getNetwork()->peerStatus.end());
getNetwork()->unlockPeerStatus();
for (auto entry : peerStatus) {
uint32_t peerId = entry.first;
json::object peerObj = entry.second;

@ -19,6 +19,12 @@ using namespace network;
#include <cassert>
// ---------------------------------------------------------------------------
// Static Class Members
// ---------------------------------------------------------------------------
std::mutex PeerNetwork::m_peerStatusMutex;
// ---------------------------------------------------------------------------
// Public Class Members
// ---------------------------------------------------------------------------
@ -89,6 +95,7 @@ void PeerNetwork::userPacketHandler(uint32_t peerId, FrameQueue::OpcodePair opco
}
json::object obj = v.get<json::object>();
std::lock_guard<std::mutex> lock(m_peerStatusMutex);
peerStatus[peerId] = obj;
}
break;

@ -25,6 +25,7 @@
#include <string>
#include <cstdint>
#include <mutex>
namespace network
{
@ -56,6 +57,15 @@ namespace network
PeerNetwork(const std::string& address, uint16_t port, uint16_t localPort, uint32_t peerId, const std::string& password,
bool duplex, bool debug, bool dmr, bool p25, bool nxdn, bool slot1, bool slot2, bool allowActivityTransfer, bool allowDiagnosticTransfer, bool updateLookup, bool saveLookup);
/**
* @brief Helper to lock the peer status mutex.
*/
void lockPeerStatus() { m_peerStatusMutex.lock(); }
/**
* @brief Helper to unlock the peer status mutex.
*/
void unlockPeerStatus() { m_peerStatusMutex.unlock(); }
/**
* @brief Map of peer status.
*/
@ -78,6 +88,9 @@ namespace network
* @returns bool True, if configuration was sent, otherwise false.
*/
bool writeConfig() override;
private:
static std::mutex m_peerStatusMutex;
};
} // namespace network

Loading…
Cancel
Save

Powered by TurnKey Linux.