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

Handle cancellation while waiting for an encrypted BLE response #356

Merged
merged 2 commits into from
Jan 13, 2024
Merged
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
34 changes: 25 additions & 9 deletions aiohomekit/controller/ble/pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,31 @@ async def _async_request_under_lock(
char.service.type, char.type, endpoint_iid
)

pdu_status, result_data = await ble_request(
self.client,
self._encryption_key,
self._decryption_key,
opcode,
endpoint,
endpoint_iid,
data,
)
try:
pdu_status, result_data = await ble_request(
self.client,
self._encryption_key,
self._decryption_key,
opcode,
endpoint,
endpoint_iid,
data,
)
except BaseException as ex:
# If the request fails for any reason (especially
# cancellation), we need to close the connection
# otherwise the encryption counters will be out of sync
try:
await self._close_while_locked()
except Exception as exc:
logger.debug(
"%s: Failed to close connection after exception: %s",
self.name,
exc,
)
# Make sure we propagate the exception to the caller
# especially if it was a cancellation
raise ex

if not self.client or not self.client.is_connected:
logger.debug("%s: Client not connected; rssi=%s", self.name, self.rssi)
Expand Down
Loading