From d3dc97d42d77f3a288b1d344ffe182d9b986485d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 10 Aug 2026 17:53:31 -0500 Subject: [PATCH] Tear down a handle stamped mid-escalation and pin the wrap flags literally --- .../bluetooth_connection/bluetooth_connection_rp2.cpp | 6 ++++++ tests/component_tests/rp2040_ble/test_pool_wrap.py | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/esphome/components/bluetooth_connection/bluetooth_connection_rp2.cpp b/esphome/components/bluetooth_connection/bluetooth_connection_rp2.cpp index 4ef7293f4c..eff07fb5a0 100644 --- a/esphome/components/bluetooth_connection/bluetooth_connection_rp2.cpp +++ b/esphome/components/bluetooth_connection/bluetooth_connection_rp2.cpp @@ -653,6 +653,12 @@ void RP2GattClient::fail_connection_(uint8_t reason) { if (connect_owner == this) { connect_owner = nullptr; } + if (this->state_ == EngineState::CONNECTING && this->con_handle_ != HCI_CON_HANDLE_INVALID) { + // A success completion stamped the handle between the escalation + // decision and this lock: tear the link down before cleanup wipes the + // handle, or it leaks its pool block for the rest of the boot. + gap_disconnect(this->con_handle_); + } } this->cleanup_link_state_(); this->release_scan_inhibit_(); diff --git a/tests/component_tests/rp2040_ble/test_pool_wrap.py b/tests/component_tests/rp2040_ble/test_pool_wrap.py index 6cbcc03e3f..291ca5eb58 100644 --- a/tests/component_tests/rp2040_ble/test_pool_wrap.py +++ b/tests/component_tests/rp2040_ble/test_pool_wrap.py @@ -7,13 +7,17 @@ from __future__ import annotations from collections.abc import Callable from pathlib import Path -from esphome.components import rp2040_ble from esphome.core import CORE from ..helpers import get_define_value -WRAP_FLAGS = tuple( - f"-Wl,--wrap={symbol}" for symbol in rp2040_ble._BTSTACK_POOL_SYMBOLS +# Spelled out rather than derived from rp2040_ble's symbol tuple, so a typo +# in the component's list fails here instead of mirroring into the test. +WRAP_FLAGS = ( + "-Wl,--wrap=btstack_memory_gatt_client_get", + "-Wl,--wrap=btstack_memory_gatt_client_free", + "-Wl,--wrap=btstack_memory_hci_connection_get", + "-Wl,--wrap=btstack_memory_hci_connection_free", )