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

Fix race with _disconnect_monitor event in BlueZ #999

Merged
merged 3 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Fixed
* Fixed use of wrong enum in unpair function of WinRT backend.
* Fixed inconsistent return types for ``properties`` and ``descriptors`` properties of ``BleakGATTCharacteristic``.
* Handle device being removed before GetManagedObjects returns in BlueZ backend. Fixes #996.
* Fixes a race in the BlueZ D-Bus backend where the disconnect monitor would be removed before it could be awaited.

Removed
-------
Expand Down
10 changes: 7 additions & 3 deletions bleak/backends/bluezdbus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def on_value_changed(char_path: str, value: bytes) -> None:

# Create a task that runs until the device is disconnected.
self._disconnect_monitor_event = asyncio.Event()
asyncio.ensure_future(self._disconnect_monitor())
asyncio.ensure_future(
self._disconnect_monitor(self._disconnect_monitor_event)
bdraco marked this conversation as resolved.
Show resolved Hide resolved
)

#
# We will try to use the cache if it exists and `dangerous_use_bleak_cache`
Expand Down Expand Up @@ -226,7 +228,9 @@ def on_value_changed(char_path: str, value: bytes) -> None:
self._cleanup_all()
raise

async def _disconnect_monitor(self) -> None:
async def _disconnect_monitor(
self, disconnect_monitor_event: asyncio.Event
) -> None:
# This task runs until the device is disconnected. If the task is
# cancelled, it probably means that the event loop crashed so we
# try to disconnected the device. Otherwise BlueZ will keep the device
Expand All @@ -237,7 +241,7 @@ async def _disconnect_monitor(self) -> None:
# task will still be running and asyncio complains if a loop with running
# tasks is stopped.
try:
await self._disconnect_monitor_event.wait()
await disconnect_monitor_event.wait()
except asyncio.CancelledError:
try:
# by using send() instead of call(), we ensure that the message
Expand Down