Skip to content

Synchronize the ESP32 BLE receive queue#3007

Merged
ripplebiz merged 2 commits into
meshcore-dev:devfrom
axhoff:agent/synchronize-ble-receive-queue
Jul 21, 2026
Merged

Synchronize the ESP32 BLE receive queue#3007
ripplebiz merged 2 commits into
meshcore-dev:devfrom
axhoff:agent/synchronize-ble-receive-queue

Conversation

@axhoff

@axhoff axhoff commented Jul 20, 2026

Copy link
Copy Markdown

Summary

  • replace the ESP32 BLE receive array with a statically allocated FreeRTOS queue
  • use non-blocking queue send/receive operations and reset the queue when buffers are cleared
  • keep send-queue behavior unchanged

Root cause

The BLE callback task appended to recv_queue while the Arduino loop task read, decremented, shifted, and cleared the same array without synchronization. Closely spaced frames could therefore observe partially published state or race with draining and clearing.

Scope

Only the receive queue is changed. xQueueSend(..., 0) retains the existing drop-newest behavior when all four slots are occupied, xQueueReceive(..., 0) keeps reads non-blocking, and xQueueReset handles clear/reset. Static allocation avoids introducing heap allocation or queue-lifetime management.

Validation

  • git diff --check
  • pio test -e native — 13/13 tests passed
  • pio run -e Xiao_S3_WIO_companion_radio_ble

Fixes #2923

@axhoff
axhoff marked this pull request as ready for review July 20, 2026 14:51
@ripplebiz

Copy link
Copy Markdown
Member

Wow, I did a bit of research and it's news to me that the Arduino BLE classes don't wrap the operations/callbacks into thread-safe accessors. Yoiks! So, in that case, we should just use an RTOS Queue, eg
dataQueue = xQueueCreate(FRAME_QUEUE_SIZE, sizeof(Frame));
...
onWrite() { ... xQueueSend(dataQueue, ...) }
...
in checkRecvFrame() { ... if (xQueueReceive(dataQueue, ... }

@axhoff

axhoff commented Jul 21, 2026

Copy link
Copy Markdown
Author

Thanks @ripplebiz — good call on using the RTOS primitive. I replaced the custom queue and critical section with a statically allocated FreeRTOS queue using xQueueSend, xQueueReceive, and xQueueReset in 4d4d7c3. The BLE target and native tests both pass.

@ripplebiz
ripplebiz merged commit 8b048ab into meshcore-dev:dev Jul 21, 2026
15 checks passed
hermes-gadget added a commit to gadgethd/MeshCore-MQTT that referenced this pull request Jul 22, 2026
Upstream dev: 7ecb2cb..78240e9 (6 commits)
3885c67 fix: synchronize BLE receive queue
9fd6b41 fix: preserve bonded BLE reconnects
4d4d7c3 refactor: use FreeRTOS BLE receive queue
8b048ab Merge pull request meshcore-dev#3007
4157e43 Merge pull request meshcore-dev#3005
78240e9 optimised: value-init not needed

Clean auto-merge (no conflicts). MQTT additions preserved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants