From 28573f5abe3cda8670766553dbe7baecc5933ea8 Mon Sep 17 00:00:00 2001 From: h2zero Date: Tue, 12 Jan 2021 14:01:44 -0700 Subject: [PATCH] Fix crash in NimBLEDevice::deleteClient when notification arrives. While deleting the client attribute database, if a notification occurs there is a possibility of concurrency causing an exception. This fixes that by setting a flag before calling disconnect in the deleteClient function to prevent processing further notifications. --- src/NimBLEDevice.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/NimBLEDevice.cpp b/src/NimBLEDevice.cpp index 4598313..b1a0f7b 100644 --- a/src/NimBLEDevice.cpp +++ b/src/NimBLEDevice.cpp @@ -165,6 +165,9 @@ void NimBLEDevice::stopAdvertising() { return false; } + // Set the connection established flag to false to stop notifications + // from accessing the attribute vectors while they are being deleted. + pClient->m_connEstablished = false; int rc =0; if(pClient->isConnected()) { @@ -176,6 +179,10 @@ void NimBLEDevice::stopAdvertising() { while(pClient->isConnected()) { taskYIELD(); } + // Since we set the flag to false the app will not get a callback + // in the disconnect event so we call it here for good measure. + pClient->m_pClientCallbacks->onDisconnect(pClient); + } else if(pClient->m_pTaskData != nullptr) { rc = ble_gap_conn_cancel(); if (rc != 0 && rc != BLE_HS_EALREADY) {