Skip to content

Commit

Permalink
Handle cancellation while waiting for an encrypted BLE response (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 13, 2024
1 parent f25dcb1 commit 6e8e374
Showing 1 changed file with 25 additions and 9 deletions.
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

0 comments on commit 6e8e374

Please sign in to comment.