mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Fix unavailable callback leak in private_ble_device (#182886)
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -119,7 +119,8 @@ class PrivateDevicesCoordinator:
|
||||
self._irk_to_mac[irk] = mac
|
||||
|
||||
# Stop ignoring this MAC
|
||||
self._ignored.pop(mac, None)
|
||||
if cancel := self._ignored.pop(mac, None):
|
||||
cancel()
|
||||
|
||||
# Ignore availability events for the previous address
|
||||
if cancel := self._unavailability_trackers.pop(irk, None):
|
||||
@@ -155,7 +156,10 @@ class PrivateDevicesCoordinator:
|
||||
return
|
||||
|
||||
def _unignore(service_info: bluetooth.BluetoothServiceInfoBleak) -> None:
|
||||
self._ignored.pop(service_info.address, None)
|
||||
# Unavailable callbacks stay registered after firing, so cancel it
|
||||
# or one callback leaks per ignored (rotating) address forever.
|
||||
if cancel := self._ignored.pop(service_info.address, None):
|
||||
cancel()
|
||||
|
||||
self._ignored[mac] = bluetooth.async_track_unavailable(
|
||||
self.hass, _unignore, mac, False
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Tests for the private_ble_device coordinator."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import (
|
||||
DUMMY_IRK,
|
||||
MAC_RPA_VALID_1,
|
||||
MAC_STATIC,
|
||||
async_inject_broadcast,
|
||||
async_mock_config_entry,
|
||||
async_move_time_forwards,
|
||||
)
|
||||
|
||||
from tests.components.bluetooth import _get_manager
|
||||
|
||||
OTHER_IRK = "11111111111111111111111111111111"
|
||||
|
||||
|
||||
def _unavailable_callback_count(address: str) -> int:
|
||||
"""Return how many non-connectable unavailable callbacks track an address."""
|
||||
# pylint: disable-next=protected-access
|
||||
return len(_get_manager()._unavailable_callbacks.get(address, ()))
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_bluetooth")
|
||||
async def test_ignored_address_unavailable_callback_released(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test an ignored address stops being tracked once it goes unavailable."""
|
||||
await async_mock_config_entry(hass)
|
||||
await async_inject_broadcast(hass, MAC_STATIC)
|
||||
assert _unavailable_callback_count(MAC_STATIC) == 1
|
||||
|
||||
await async_move_time_forwards(hass, 910)
|
||||
assert _unavailable_callback_count(MAC_STATIC) == 0
|
||||
|
||||
# Seen again after going away, it is ignored (and tracked) exactly once
|
||||
await async_inject_broadcast(hass, MAC_STATIC)
|
||||
assert _unavailable_callback_count(MAC_STATIC) == 1
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("enable_bluetooth")
|
||||
async def test_ignored_address_released_when_irk_resolves(
|
||||
hass: HomeAssistant,
|
||||
) -> None:
|
||||
"""Test the ignore tracker is cancelled when a new IRK resolves the address."""
|
||||
await async_mock_config_entry(hass, OTHER_IRK)
|
||||
await async_inject_broadcast(hass, MAC_RPA_VALID_1)
|
||||
# Unresolvable by OTHER_IRK, so it is ignored
|
||||
assert _unavailable_callback_count(MAC_RPA_VALID_1) == 1
|
||||
|
||||
await async_mock_config_entry(hass, DUMMY_IRK)
|
||||
# Only the resolved IRK's availability tracker remains
|
||||
assert _unavailable_callback_count(MAC_RPA_VALID_1) == 1
|
||||
Reference in New Issue
Block a user