Repository navigation
Description
【Description】
I am experiencing an issue where the ESP32 occasionally encounters a WDT reset (Watchdog Timer reset) when calling client->getService(servName) or pService->getCharacteristic(charName). It appears that the library hangs indefinitely while waiting for a response from the BLE server.
【My Workaround】
To prevent the device from freezing, I modified the source code to add a timeout mechanism. I replaced the infinite wait flag BLE_NPL_TIME_FOREVER with a fixed timeout (e.g., 3000ms) in the following functions:
-- NimBLEClient::retrieveServices
-- NimBLERemoteService::retrieveCharacteristics
-- NimBLERemoteValueAttribute::readValue
Code Change:
// Original
NimBLEUtils::taskWait(taskData, BLE_NPL_TIME_FOREVER);
// Modified
NimBLEUtils::taskWait(taskData, 3000); // 3 second timeout
【The Problem】
While this successfully prevents the WDT reset, when I use NimBLEDevice::deleteClient(client) to release the client after the 3-second timeout, it occasionally triggers a bug. This bug appears to be caused by xTaskNotify(static_cast<TaskHandle_t>(taskData.m_pHandle), TASK_BLOCK_BIT, eSetBits) inside NimBLEUtils::taskRelease:
D NimBLEClient: Service Discovered >> status: 7 handle: -1
NimBLEClient: << Service Discovered; Disconnected
Guru Meditation Error: Core 0 panic'ed (LoadStoreError). Exception was unhandled.
Core 0 register dump:
PC : 0x40099a22 PS : 0x00060233 A0 : 0x800d859d A1 : 0x3ffd4e70
A2 : 0x3f4069f8 A3 : 0x00000000 A4 : 0x80000000 A5 : 0x00000001
A6 : 0x00000000 A7 : 0x3f4069f8 A8 : 0x0000008b A9 : 0x3f406af8
A10 : 0x00000002 A11 : 0xffffffff A12 : 0x0000002d A13 : 0x00060223
A14 : 0x00000002 A15 : 0x0000cdcd SAR : 0x00000010 EXCCAUSE: 0x00000003
EXCVADDR: 0x3f406b54 LBEG : 0x400930d5 LEND : 0x400930e5 LCOUNT : 0xfffffffd
Backtrace: 0x40099a1f:0x3ffd4e70 0x400d859a:0x3ffd4e90 0x400d5d0a:0x3ffd4eb0 0x400de46f:0x3ffd4ee0 0x400de4a3:0x3ffd4f00 0x400def29:0x3ffd4f20 0x400dfcb3:0x3ffd4f50 0x400dd7e3:0x3ffd4f70 0x400dd878:0x3ffd4fd0 0x400e2276:0x3ffd5030 0x400e24b5:0x3ffd5050 0x400e10bd:0x3ffd5070 0x400e8d25:0x3ffd5090 0x400d5dff:0x3ffd50b0 0x40097385:0x3ffd50d0
#0 0x40099a1f in xTaskGenericNotify at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/FreeRTOS-Kernel/tasks.c:5921
#1 0x400d859a in NimBLEUtils::taskRelease(NimBLETaskData const&, int) at lib/NimBLE-Arduino/src/NimBLEUtils.cpp:127
#2 0x400d5d0a in NimBLEClient::serviceDiscoveredCB(unsigned short, ble_gatt_error const*, ble_gatt_svc const*, void*) at lib/NimBLE-Arduino/src/NimBLEClient.cpp:790
【Questions】
How should I properly handle the timeout to avoid this secondary bug?
Is it possible for the official library to integrate a configurable timeout feature for these blocking calls to make them more robust?
Activity
yorling commentedon Mar 30, 2026
NimBLE-Arduino version 2.4.0
h2zero commentedon Mar 30, 2026
The reason for those timers to be set forever like that is because the stack is guaranteed to call the callback that releases it.
I suspect the issue that's causing this for you is your code is calling these blocking functions from a callback, which is not supported.
yorling commentedon Mar 31, 2026
@h2zero I am developing a Bluetooth gateway that needs to frequently connect to different devices and read GATT values. I very often encounter infinite waiting situations. This happens multiple times within an hour and causes the device to restart. Is there any solution to resolve the infinite waiting issue?
yorling commentedon Mar 31, 2026
@h2zero
my code:
h2zero commentedon Mar 31, 2026
Thanks, but it doesn't really show the context of these calls.
I have opened #1136 that will help discover why your code hangs, if your calls are in the host task context this PR will print an error and return immediately.
We need to find the cause of these calls hanging, the WDT timeout is just the symptom of the problem.
yorling commentedon Apr 2, 2026
@h2zero
After merging the code from branch "err-on-block" into branch "2.4.0", the following error log was encountered. It seems that the log "xxx cannot be called from host task" was not printed.
yorling commentedon Apr 8, 2026
After merging the code from the "err-on-block branch", I didn't see any "xxx cannot be called from host task" logs. This suggests that the root cause of the crash might not be in the area you modified.
However, after some investigation, I managed to resolve the "Guru Meditation Error" by implementing a finite timeout for NimBLEUtils::taskWait.
I've pasted my code below. I hope you find it useful. Could you please review it to make it more robust and consider integrating this finite timeout logic into your main codebase?
h2zero commentedon Apr 10, 2026
While that may resolve the issue you are having, and I'm willing to entertain this, I would like to find the root cause first. Can you share some code snippets that I can try to reproduce this with, what is the WDT timer set to?
yorling commentedon Apr 13, 2026
@h2zero
WDT timeout: 15s
Main code flow: Scan BLE device list > Upload to server and obtain operation actions > Traverse actions, and according to the actions: connect to BLE device, read GATT values, release BLE device > Upload GATT values to server. Then loop the above process
The main code snippet after simplification:
BLEClientGuard
Plus the code I modified above: "NimBLEUtils::taskWait(taskData, 3000);" and "NimBLEUtils::taskRelease(...)"
I hope these codes can help you