From f54c957be8d1b3b46ef7ab5f47b2ce817dae19ea Mon Sep 17 00:00:00 2001 From: Rune Harlyk Date: Thu, 25 Dec 2025 13:36:49 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Secure=20sub=20and=20unsub=20wit?= =?UTF-8?q?h=20mutex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esp32/include/communication/comm_base.hpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/esp32/include/communication/comm_base.hpp b/esp32/include/communication/comm_base.hpp index b1ed5b0..8b469bb 100644 --- a/esp32/include/communication/comm_base.hpp +++ b/esp32/include/communication/comm_base.hpp @@ -55,8 +55,16 @@ class CommAdapterBase { void send(const char *data, int cid = -1) { send(reinterpret_cast(data), strlen(data), cid); } virtual void send(const uint8_t *data, size_t len, int cid = -1) = 0; - void subscribe(const char *event, int cid = 0) { client_subscriptions[event].push_back(cid); } - void unsubscribe(const char *event, int cid = 0) { client_subscriptions[event].push_back(cid); } + void subscribe(const char *event, int cid = 0) { + xSemaphoreTake(mutex_, portMAX_DELAY); + client_subscriptions[event].push_back(cid); + xSemaphoreGive(mutex_); + } + void unsubscribe(const char *event, int cid = 0) { + xSemaphoreTake(mutex_, portMAX_DELAY); + client_subscriptions[event].remove(cid); + xSemaphoreGive(mutex_); + } void handleEventCallbacks(std::string event, JsonVariant &jsonObject, int originId) { for (auto &callback : event_callbacks[event]) {