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

CoreBluetooth callback fixes and cleanups #678

Merged
merged 4 commits into from
Oct 23, 2021
Merged

CoreBluetooth callback fixes and cleanups #678

merged 4 commits into from
Oct 23, 2021

Conversation

dlech
Copy link
Collaborator

@dlech dlech commented Oct 23, 2021

This started as a fix for #675 and I found some cleanups to do in the surrounding code. See commit messages for details.

@dlech dlech linked an issue Oct 23, 2021 that may be closed by this pull request
dlech added 4 commits October 23, 2021 14:58
In the CoreBluetooth backend, futures are used to pass results/errors
from the delegate callbacks to methods that are waiting for them. The
futures are stored as part of the object state so that the delegates
can access them.

Prior to this change, completed futures were not removed from the
object status. This worked most of the time because a second call
of the same method would replace the completed future from the prior
call. However, in the case of notifications and reads, the same
delegate callback is shared with two methods. So when both of these
were used, notifications would attempt to complete the already completed
read future which raises an InvalidStateError.

Fixes #675.
This updates CoreBluetooth to read the changed value in the delegate
callback and pass it back to the BleakClient method via the future
object.

By reading the value in the delegate callback rather than the method
scheduled by call_soon_threadsafe(), we ensure that we get the original
value from the delegate callback and not a possibly changed value later.
This removes `return True` from several PeripheralDelegate methods.
These methods unconditionally return `True`, so we can simplify the
code by removing it.
The same delegate callback is used for notifications and reading
characteristics. This rearranges the logic so that if there is a
read request pending, it will complete the read, otherwise it will
trigger a notification callback. Previously, reads would also trigger
notification callbacks.
@dlech dlech merged commit ac0d6bf into develop Oct 23, 2021
@dlech dlech deleted the cb-futures branch October 23, 2021 20:51
@dlech dlech added the Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend label Dec 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InvalidStateError after callback
1 participant