Skip to content
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

backends/bluezdbus/manager: Cancel the discovery on disconnect to avoid a timeout #1092

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
Fixed
-----
* Fixed crash in Android backend introduced in v0.19.0. Fixes #1085.
* 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
25 changes: 24 additions & 1 deletion bleak/backends/bluezdbus/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ async def get_services(
logger.debug("Using cached services for %s", device_path)
return services

await self._wait_condition(device_path, "ServicesResolved", True)
await self._wait_for_services_disovery(device_path)

services = BleakGATTServiceCollection()

Expand Down Expand Up @@ -648,6 +648,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