Skip to content

_bleio: make Connection.disconnect() safe after the peer disconnects - #11560

Open
dhalbert wants to merge 1 commit into
adafruit:mainfrom
dhalbert:bleio-disconnect-after-peer-disconnect
Open

dhalbert wants to merge 1 commit into
adafruit:mainfrom
dhalbert:bleio-disconnect-after-peer-disconnect

Conversation

@dhalbert

Copy link
Copy Markdown
Collaborator

Diagnosis and code by Claude, reviewed by @dhalbert.

Problem

  • On espressif, calling Connection.disconnect() after the peer had already disconnected caused a hard fault.
  • When the peer disconnects, the Connection object's internal connection pointer is set to NULL.
  • disconnect() still passed that pointer to common_hal_bleio_connection_disconnect(), relying on each port to do nothing when already disconnected.
    • espressif reads conn_handle through it without checking, and faults on the NULL read.
    • nordic, silabs and ble_hci don't check either. zephyr-cp does.

Fix

  • disconnect() now calls common-hal only if common_hal_bleio_connection_get_connected() is true. Every port's get_connected() already handles a NULL connection.

Testing

Metro ESP32-S3 as central, nRF54LM20 DK as peripheral. The DK disconnects, then the Metro calls disconnect() twice:

Build Result
main hard fault on the first disconnect()
this PR both calls do nothing

When the peer disconnects, the Connection object's connection pointer
is set to NULL. disconnect() still passed it to
common_hal_bleio_connection_disconnect(), relying on each port to do
nothing when already disconnected. espressif reads conn_handle from
it without checking, so the next disconnect() hard-faulted. nordic,
silabs and ble_hci don't check either.

Call common-hal only when the connection is still connected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dhalbert
dhalbert requested a review from mikeysklar October 12, 2026 01:19
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.

1 participant