diff --git a/esphome/components/esp32_ble/queue.h b/esphome/components/esp32_ble/queue.h index afa9a9b668..49b0ec5480 100644 --- a/esphome/components/esp32_ble/queue.h +++ b/esphome/components/esp32_ble/queue.h @@ -46,7 +46,13 @@ template class Queue { } size_t size() const { - return q_.size(); // Atomic read, no lock needed + // Lock-free size check. While std::queue::size() is not thread-safe, we intentionally + // avoid locking here to prevent blocking the BLE callback thread. The size is only + // used to decide whether to drop incoming events when the queue is near capacity. + // With a queue limit of 40-64 events and normal processing, dropping events should + // be extremely rare. When it does approach capacity, being off by 1-2 events is + // acceptable to avoid blocking the BLE stack's time-sensitive callbacks. + return q_.size(); } protected: