[bluetooth_proxy] Fix connection slot race by deferring slot release until GATT close - #10303
Conversation
|
To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file: external_components:
- source: github://pr#10303
components: [bluetooth_proxy]
refresh: 1h(Added by the PR bot) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #10303 +/- ##
=======================================
Coverage 60.45% 60.45%
=======================================
Files 51 51
Lines 10417 10417
Branches 1388 1388
=======================================
Hits 6298 6298
Misses 3760 3760
Partials 359 359 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a Bluetooth proxy race condition where connection slots were marked as available too early, before the ESP32 controller had fully released resources. This caused immediate reconnection attempts to fail with HCI error 0x0d, leading to connection/disconnection loops.
- Defers connection slot release from
ESP_GATTC_DISCONNECT_EVTtoESP_GATTC_CLOSE_EVT - Adds explicit logging to track disconnect and close events separately
- Sends disconnection notification immediately but waits to free the slot until controller cleanup is complete
|
👋 Hi there! I've automatically requested reviews from codeowners based on the files changed in this PR. @jesserockz - You've been requested to review this PR as codeowner(s) of 1 file(s) that were modified. Thanks for your time! 🙏 |
…until GATT close (#10303)
What does this implement/fix?
This PR fixes a long standing race condition in the Bluetooth proxy where connection slots were marked as available too early, before the ESP32 controller had fully released the resources. This caused immediate reconnection attempts to fail with HCI error 0x0d (Connection Rejected Due to Limited Resources), leading to rapid connection/disconnection loops.
The issue occurs because
ESP_GATTC_DISCONNECT_EVTonly indicates disconnection has started, not that it's complete. The ESP32 controller needs additional time to clean up L2CAP channels, release ATT resources, free the HCI connection handle, and update internal connection tracking. By marking the slot as free immediately upon disconnect, ESPHome would try to reuse it before the controller was ready.The fix ensures connection slots are only marked as free after receiving
ESP_GATTC_CLOSE_EVT, which indicates the GATT connection is fully closed and controller resources are freed.Types of changes
Related issue or feature (if applicable):
Pull request in esphome-docs with documentation (if applicable):
Test Environment
Example entry for
config.yaml:Checklist:
tests/folder).If user exposed functionality or configuration variables are added/changed: