Skip to content

Commit 5c5ec4f

Browse files
committed
backends/winrt/client: fix potential race condition for session status
We need to check the current session status *after* registering the event handler, otherwise we might miss the event if the session status changes between checking the status and registering the handler.
1 parent 0df017d commit 5c5ec4f

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Changed
2323

2424
Fixed
2525
-----
26+
* Fixed potential race condition causing timeout while connecting in WinRT backend.
2627
* Fixed file handle leak in BlueZ backend when D-Bus connection is lost and re-established.
2728
* Fixed crash in CoreBluetooth backend if an ObjC delegate callback is called after the asyncio run loop stops.
2829
* Fixed possible deadlock when starting scanning on Windows when Bluetooth is turned off.

bleak/backends/winrt/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,20 +360,20 @@ def max_pdu_size_changed_handler(sender: GattSession, args: Object) -> None:
360360
)
361361
)
362362

363-
# If the session is already active, we need to set the event since
364-
# the session_status_changed event won't fire. This happens, e.g.,
365-
# when pairing before connecting which causes the device to already
366-
# be connected.
367-
if self._session.session_status == GattSessionStatus.ACTIVE:
368-
event.set()
369-
370363
self._max_pdu_size_changed_token = self._session.add_max_pdu_size_changed(
371364
max_pdu_size_changed_handler
372365
)
373366

374367
services_changed_event = asyncio.Event()
375368
self._services_changed_events.append(services_changed_event)
376369

370+
# If the session is already active, we need to set the event since
371+
# the session_status_changed event won't fire. This happens, e.g.,
372+
# when pairing before connecting which causes the device to already
373+
# be connected.
374+
if self._session.session_status == GattSessionStatus.ACTIVE:
375+
event.set()
376+
377377
try:
378378
# Windows does not support explicitly connecting to a device.
379379
# Instead it has the concept of a GATT session that is owned

0 commit comments

Comments
 (0)