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

bluezdbus/client: Make sure the disconnect monitor task is properly cancelled #1159

Merged

Conversation

arthur-proglove
Copy link
Contributor

Sometimes the exception on connect is raised so early that the disconnect monitor task does not get the disconnect event set because the on_connected_changed callback is already unregistered and the task stays in pending state causing asyncio to throw an exception:
Task was destroyed but it is pending!

Copy link
Collaborator

@dlech dlech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

It would be nice if we could fix this without introducing additional object state variables since that is what triggered the bug in the first place.

Does something like this solve the problem?

diff --git a/bleak/backends/bluezdbus/client.py b/bleak/backends/bluezdbus/client.py
index 8bf88ff..b42bf02 100644
--- a/bleak/backends/bluezdbus/client.py
+++ b/bleak/backends/bluezdbus/client.py
@@ -197,10 +197,12 @@ class BleakClientBlueZDBus(BaseBleakClient):
                 self._is_connected = True
 
                 # Create a task that runs until the device is disconnected.
-                self._disconnect_monitor_event = asyncio.Event()
+                self._disconnect_monitor_event = (
+                    local_disconnect_monitor_event
+                ) = asyncio.Event()
                 asyncio.ensure_future(
                     self._disconnect_monitor(
-                        self._bus, self._device_path, self._disconnect_monitor_event
+                        self._bus, self._device_path, local_disconnect_monitor_event
                     )
                 )
 
@@ -246,6 +248,9 @@ class BleakClientBlueZDBus(BaseBleakClient):
 
                 raise
         except BaseException:
+            # this effectively cancels the disconnect monitor in case the event
+            # was not triggered by a D-Bus callback
+            local_disconnect_monitor_event.set()
             self._cleanup_all()
             raise

@arthur-proglove arthur-proglove force-pushed the bluez/client/disconnect_monitor branch from 1d10cf6 to 60e5ea3 Compare December 1, 2022 17:22
…ancelled.

Sometimes the exception on connect is raised so early that the
disconnect monitor task does not get the disconnect event set because the
on_connected_changed callback is already unregistered and the task stays
in pending state causing asyncio to throw an exception:
"Task was destroyed but it is pending!"
@arthur-proglove arthur-proglove force-pushed the bluez/client/disconnect_monitor branch from 60e5ea3 to 1ccc8c0 Compare December 1, 2022 17:24
@arthur-proglove
Copy link
Contributor Author

Seems to work as well ;-). I updated the PR.

@dlech dlech merged commit ba172f0 into hbldh:develop Dec 1, 2022
@dlech
Copy link
Collaborator

dlech commented Dec 1, 2022

Thanks!

@dlech dlech mentioned this pull request Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants