New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix missing conn_id check in bluetooth_proxy disconnect event #6590
Conversation
Pointed out by @elupus while debugging home-assistant/core#103117
Pushed to al my production and so far so good. But I didn't see a problem before, however that is likely because I have so many proxies and it was very rare for one to have more than one connection at a time |
I think it looks correct. Will report back if i get confirmation on that other issue. |
Hey there @jesserockz, mind taking a look at this pull request as it has been labeled with an integration ( |
Running fine overnight here |
Looks like the base class has similar code:
Im suddenly not fully sure here. A bit unclear från docs how one is expected to handle this. |
So there are virtual and physical connections. If there are multiple virtual connections to same device, there will not be a disconnect event, only a close event untill all virtual connections are closed. I dont think this change here will make a difference, might break things if there was never any OPEN event. That said, this should likely listen to CLOSE events with that check. Not sure if the proxy code manages this virtual connections. |
https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/bluetooth/esp_gattc.html close is the virtual |
Looks like we don't handle ESP_GATTC_CANCEL_OPEN_EVT |
not sure ESP_GATTC_CANCEL_OPEN_EVT actually ever happens? |
ESP_GATTC_CANCEL_OPEN_EVT is never referenced in the underlying code so I don't think it actually ever happens |
Should be fine if there is never an open event as it will end up freeing the connection slot as it does the same thing as an open failure |
Here is the order of events for a connection to a lock
|
My point was if we have
Second virtual connection starts up
First virtual connection is closed
(no disconnect here, so proxy still thinks the virtual connection is open)
Strange order of close disconnect from your log. Now both the proxy connection will be considered closed. |
Didn't realize it was possible with the proxy code to create more than one virtual connection to the same device. Need to check that it actually is and the library doesn't assume already connected means you get the same connection |
I am not sure it is, but ive seen users playing with ble_client configs on the esp proxy itself. See this log.
|
It looks like if you try to connect to an address and it's already connected we return the existing connection
|
That definitely doesn't look right. It shouldn't see two connect events. It does look like the check is wrong in esp32_ble_client as well.
|
We don't set the conn_id until the open event. Hmmm |
Do you by chance have a reproducer for the above log transaction? |
This is all i know: |
Are they using the esphome component and the Home Assistant integration at the same time? |
I think in this case incorrectly so yes. I don't think that is the original source of the problem though. I think they have ended up trying many weird things. That said, we should probably behave good in that case too (or fail in a understandable way) |
Looks like we don't need to check any conn_id in the proxy code. The base class should have filtered all those events already. but we should likely look at both disconnect and close event. |
Here is actually one of those cases where the proxy missing the close event: home-assistant/core#103117 (comment) if you look at the last log lines, we have an event == 5, which is ESP_GATTC_CLOSE_EVT , but no disconnect ever comes. So it's likely still connected by some background connection, which breaks the next proxy connection. |
Looks like some things changes (and broke) in #5277, the proxy code will no longer get the failed event status for a ESP_GATTC_OPEN_EVT. It now will return false in base class for this here: https://github.com/esphome/esphome/pull/5277/files#diff-63fb25f9255daa17cf4c5e4faa27822d42c678f5fbca526e44010ba4055ce905R145 so the proxy will no longer parse this event. |
I made an alternate change here: #6596 not sure how i'm going to test that thou. |
superseded by #6596 |
What does this implement/fix?
Fix missing conn_id check in bluetooth_proxy disconnect event
Pointed out by @elupus while debugging home-assistant/core#103117
Test yaml
Types of changes
Related issue or feature (if applicable): fixes
Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#
Test Environment
Example entry for
config.yaml
:# Example config.yaml
Checklist:
tests/
folder).If user exposed functionality or configuration variables are added/changed: