Skip to content

Commit e8b1031

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. Fixes: #1829
1 parent 2e7192c commit e8b1031

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
1010
`Unreleased`_
1111
=============
1212

13+
Fixed
14+
-----
15+
- Fixed potential race condition causing timeout while connecting in WinRT backend. Fixes #1829.
16+
1317
`1.1.1`_ (2025-09-07)
1418
=====================
1519

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)