Protect _clients lookup in discoverServices with _conn_mutex

The unprotected _clients.find() could race with
processPendingDisconnects() erasing from the map concurrently.
Mutex is released before the blocking getService() GATT call.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
torlando-techandClaude Opus 4.6 committed 2026-03-05 12:30:52 -05:00
1 parent 602d8f7083
commit e2df70161b
1 file changed
+9 -4
@@ -1458,13 +1458,18 @@ bool NimBLEPlatform::discoverServices(uint16_t conn_handle) {
return false;
}
auto client_it = _clients.find(conn_handle);
if (client_it == _clients.end() || !client_it->second) {
NimBLEClient* client = nullptr;
if (xSemaphoreTake(_conn_mutex, pdMS_TO_TICKS(100))) {
auto client_it = _clients.find(conn_handle);
if (client_it != _clients.end()) {
client = client_it->second;
}
xSemaphoreGive(_conn_mutex);
}
if (!client) {
return false;
}
NimBLEClient* client = client_it->second;
// Get our service — blocking GATT operation
NimBLERemoteService* service = client->getService(UUID::SERVICE);
if (!service) {