Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 10 additions & 10 deletions bumble/smp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,16 +1304,6 @@ async def on_pairing(self) -> None:

self.completed = True

# The keys we have just distributed may still be waiting for the
# controller's flow control. Don't report success until they have all
# been acknowledged: an application that disconnects as soon as
# `pair()` returns would otherwise cut the last key distribution PDU,
# and a peer that never receives it discards the incomplete bond.
await self.connection.drain()

if self.pairing_result is not None and not self.pairing_result.done():
self.pairing_result.set_result(None)

# Use the peer address from the pairing protocol or the connection
if self.peer_bd_addr is not None:
peer_address = self.peer_bd_addr
Expand Down Expand Up @@ -1366,6 +1356,16 @@ async def on_pairing(self) -> None:
)
await self.manager.on_pairing(self, peer_address, keys)

# The keys we have just distributed may still be waiting for the
# controller's flow control. Don't report success until they have all
# been acknowledged: an application that disconnects as soon as
# `pair()` returns would otherwise cut the last key distribution PDU,
# and a peer that never receives it discards the incomplete bond.
await self.connection.drain()

if self.pairing_result is not None and not self.pairing_result.done():
self.pairing_result.set_result(None)

def on_pairing_failure(self, reason: ErrorCode) -> None:
logger.warning('pairing failure (%s)', reason.name)

Expand Down
10 changes: 9 additions & 1 deletion tests/self_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,18 @@ async def test_self_smp_waits_for_key_distribution():
await async_barrier()

# Check that the withholding is in effect, then that pairing has not been
# reported as complete while the keys may still be sitting in the queue.
# reported as complete while the keys may still be sitting in the queue,
# even though the keys have already been saved to the keystore.
assert connection.is_encrypted
assert packet_queue.pending > 0
assert not pairing.done()
assert two_devices.devices[0].keystore is not None
assert (
await two_devices.devices[0].keystore.get(
str(two_devices.devices[1].public_address)
)
is not None
)

# Complete the packets: pairing may now finish.
packet_queue.on_packets_completed = complete_packets
Expand Down
Loading