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

Conversation

arthur-proglove
Copy link
Contributor

@arthur-proglove arthur-proglove commented Oct 25, 2022

If a device disconnects while waiting for the services to be discovered, the manager will wait forever.
This commit fixes that by introducing a context to the _wait_condition method with an exception on disconnect.

@dlech
Copy link
Collaborator

dlech commented Oct 25, 2022

I agree this should be fixed, but I would like to suggest a different way of doing it. Instead of introducing a new class, we should be able to use asyncio.wait(..., return_when=FIRST_COMPLETED) to wait for either ServicesResolved==True or "Connected"==False. If the latter wins the race, raise an exception.

@arthur-proglove
Copy link
Contributor Author

arthur-proglove commented Oct 26, 2022

I agree this should be fixed, but I would like to suggest a different way of doing it. Instead of introducing a new class, we should be able to use asyncio.wait(..., return_when=FIRST_COMPLETED) to wait for either ServicesResolved==True or "Connected"==False. If the latter wins the race, raise an exception.

Oh, thank you for highlighting this function, I only knew gather.

I tried to make something prettier than accessing directly self._properties[device_path][defs.DEVICE_INTERFACE]["Connected"] in the callback but could not find something more elegant. Using the device watcher callbacks seemed overkill.

@arthur-proglove arthur-proglove force-pushed the feat/cancel_service_discovery branch 3 times, most recently from 7d650a2 to 5efc2f4 Compare October 26, 2022 08:33
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.

Good start. However, _wait_condition is a generic function, so we should leave it that way.

The asyncio.wait() logic looks right, but it should replace await self._wait_condition(device_path, "ServicesResolved", True) in get_services(). And the two tasks can be defined at self._wait_condition(device_path, "ServicesResolved", True) and self._wait_condition(device_path, "Connected", False).

@arthur-proglove
Copy link
Contributor Author

Right! I was so focused on my services discovery issue that I completely skipped the fact that this method is for any property change.

…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.
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.

Looks great, thanks!

@dlech dlech merged commit 850cab4 into hbldh:develop Oct 27, 2022
@arthur-proglove arthur-proglove deleted the feat/cancel_service_discovery branch October 27, 2022 15:56
@dlech dlech mentioned this pull request Oct 29, 2022
bdraco added a commit to Bluetooth-Devices/bleak-retry-connector that referenced this pull request Oct 29, 2022
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.

None yet

2 participants