Skip to content

Cannot re-connect to BLE device after KeyboardInterrupt (CTRL-C) #1811

@nomadbyte

Description

@nomadbyte
  • bleak: v1.1.0
  • python: 3.13.7 (Windows 10)

I was testing the bleak functionality using its examples/disconnect_callback.py. The code works correctly -- connects to the BLE device, then waits for the device to turn off, then correctly disconnects it , and ends the execution. On the next run (after turning the BLE device on), the example code again proceeds and ends correctly.

However, when the wait is terminated with a KeyboardInterrupt (Ctrl+C) instead of turning the device off, the execution terminates with a Traceback. Then on the subsequent re-run, the example cannot connect to the BLE device (could not find device). The device has to be manually turned off and on for the example to be able to connect to it again.

While a Traceback on the KeyboardInterrupt is expected, I would also expect bleak to properly disconnect the BLE device from the context manager. For some reason this does not take place. Thus the device remains connected to the host, and won't be available for the re-connection.

Below is the log of the interrupted execution and the subsequent re-run of the example code:

>python disconnect_callback.py --name "TestBLE"
2025-08-14 21:35:40,048 __main__ INFO: scanning...
2025-08-14 21:35:44,580 __main__ INFO: Sleeping until device disconnects...    ### CTRL-C pressed
2025-08-14 21:35:49,025 __main__ INFO: Disconnected callback called!
Traceback (most recent call last):
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "C:\Users\tester\Documents\python\bleak\disconnect_callback.py", line 58, in main
    await disconnected_event.wait()
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\locks.py", line 213, in wait
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\tester\Documents\python\bleak\disconnect_callback.py", line 99, in <module>
    asyncio.run(main(args))
    ~~~~~~~~~~~^^^^^^^^^^^^
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 123, in run
    raise KeyboardInterrupt()
KeyboardInterrupt
^C


>python disconnect_callback.py --name "TestBLE"
2025-08-14 21:36:52,162 __main__ INFO: scanning...
2025-08-14 21:37:02,314 __main__ ERROR: could not find device with name 'TestBLE'

Metadata

Metadata

Assignees

No one assigned

    Labels

    more info requiredIssues does not have a reproducible test case, has insufficent logs or otherwise needs more feedback

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions