Skip to content

Commit

Permalink
backends/bluezdbus/manager: Cancel the discovery on disconnect to avo…
Browse files Browse the repository at this point in the history
…id a timeout

If a device disconnects while waiting for the services to be
discovered, the manager will wait forever.
This commit fixes that by throwing an exception in case of a disconnect.
  • Loading branch information
arthur-proglove authored and pmessan committed Nov 28, 2022
1 parent 1a99fcf commit 3e94760
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
`Unreleased`_
=============

Fixed
-----
* BlueZ: Cancel the device discovery wait task if the device disconnects in
between to avoid a timeout

`0.19.0`_ (2022-10-13)
======================

Expand Down
23 changes: 23 additions & 0 deletions bleak/backends/bluezdbus/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,29 @@ def is_connected(self, device_path: str) -> bool:
except KeyError:
return False

async def _wait_for_services_disovery(self, device_path: str) -> None:
"""Wait for the device services to be discovered.
If a disconnect happens before the completion a BleakError exception is raised.
"""
services_discovered_wait_task = asyncio.create_task(
self._wait_condition(device_path, "ServicesResolved", True)
)
device_disconnected_wait_task = asyncio.create_task(
self._wait_condition(device_path, "Connected", False)
)
done, pending = await asyncio.wait(
{services_discovered_wait_task, device_disconnected_wait_task},
return_when=asyncio.FIRST_COMPLETED,
)

for p in pending:
p.cancel()

if device_disconnected_wait_task in done:
raise BleakError("failed to discover services, device disconnected")

async def _wait_condition(
self, device_path: str, property_name: str, property_value: Any
) -> None:
Expand Down

0 comments on commit 3e94760

Please sign in to comment.