From fffd8ec79e399a285762d8d47ab8260be26dc26f Mon Sep 17 00:00:00 2001 From: torlando-tech Date: Wed, 4 Mar 2026 23:54:01 -0500 Subject: [PATCH] Add beginWriteOperation() guards to all blocking GATT methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit writeCharacteristic(), read(), and enableNotifications() resolve characteristic pointers under _conn_mutex then call blocking GATT ops after releasing it — same pattern as write(). Without the active-operation guard, processPendingDisconnects() could delete the client (and its child characteristics) during the GATT call. Co-Authored-By: Claude Opus 4.6 --- lib/ble_interface/platforms/NimBLEPlatform.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/ble_interface/platforms/NimBLEPlatform.cpp b/lib/ble_interface/platforms/NimBLEPlatform.cpp index 13a51195..a4ecbe84 100644 --- a/lib/ble_interface/platforms/NimBLEPlatform.cpp +++ b/lib/ble_interface/platforms/NimBLEPlatform.cpp @@ -1780,7 +1780,10 @@ bool NimBLEPlatform::writeCharacteristic(uint16_t conn_handle, uint16_t char_han if (!chr) return false; - return chr->writeValue(data.data(), data.size(), response); + beginWriteOperation(); + bool result = chr->writeValue(data.data(), data.size(), response); + endWriteOperation(); + return result; } bool NimBLEPlatform::read(uint16_t conn_handle, uint16_t char_handle, @@ -1833,7 +1836,9 @@ bool NimBLEPlatform::read(uint16_t conn_handle, uint16_t char_handle, return false; } + beginWriteOperation(); NimBLEAttValue value = chr->readValue(); + endWriteOperation(); if (callback) { Bytes result(value.data(), value.size()); callback(OperationResult::SUCCESS, result); @@ -1903,9 +1908,15 @@ bool NimBLEPlatform::enableNotifications(uint16_t conn_handle, bool enable) { } }; - return txChar->subscribe(true, notifyCb); + beginWriteOperation(); + bool result = txChar->subscribe(true, notifyCb); + endWriteOperation(); + return result; } else { - return txChar->unsubscribe(); + beginWriteOperation(); + bool result = txChar->unsubscribe(); + endWriteOperation(); + return result; } }