From 987623567ae06ce58e996b474d3659dd88495e05 Mon Sep 17 00:00:00 2001 From: Jorropo Date: Thu, 1 May 2025 13:32:20 +0200 Subject: [PATCH] router: on linux add a mutex around the queue (#6709) UDP reception happens on an other thread, avoid data races and potential data corruption issues. --- src/mesh/Router.cpp | 8 ++++++++ src/mesh/Router.h | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index fef29388e..bb0858c8b 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -64,6 +64,10 @@ Router::Router() : concurrency::OSThread("Router"), fromRadioQueue(MAX_RX_FROMRA */ int32_t Router::runOnce() { +#ifdef ARCH_PORTDUINO + const std::lock_guard lock(queueMutex); +#endif + meshtastic_MeshPacket *mp; while ((mp = fromRadioQueue.dequeuePtr(0)) != NULL) { // printPacket("handle fromRadioQ", mp); @@ -80,6 +84,10 @@ int32_t Router::runOnce() */ void Router::enqueueReceivedMessage(meshtastic_MeshPacket *p) { +#ifdef ARCH_PORTDUINO + const std::lock_guard lock(queueMutex); +#endif + // Try enqueue until successful while (!fromRadioQueue.enqueue(p, 0)) { meshtastic_MeshPacket *old_p; diff --git a/src/mesh/Router.h b/src/mesh/Router.h index 58ca50f3d..f4782d863 100644 --- a/src/mesh/Router.h +++ b/src/mesh/Router.h @@ -8,6 +8,9 @@ #include "PointerQueue.h" #include "RadioInterface.h" #include "concurrency/OSThread.h" +#ifdef ARCH_PORTDUINO +#include +#endif /** * A mesh aware router that supports multiple interfaces. @@ -19,6 +22,12 @@ class Router : protected concurrency::OSThread, protected PacketHistory /// forwarded to the phone. PointerQueue fromRadioQueue; +#ifdef ARCH_PORTDUINO + /// linux calls enqueueReceivedMessage from an other thread when receiving UDP packets, + /// to avoid a data race with LoRa, lock that method. + std::mutex queueMutex; +#endif + protected: RadioInterface *iface = NULL;