Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth off throws an unhandleable error on Mac #1219

Closed
felixbade opened this issue Feb 13, 2023 · 0 comments · Fixed by #1234
Closed

Bluetooth off throws an unhandleable error on Mac #1219

felixbade opened this issue Feb 13, 2023 · 0 comments · Fixed by #1234
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend bug Something isn't working

Comments

@felixbade
Copy link

  • bleak version: 0.19.5
  • Python version: 3.10.9 (via Homebrew)
  • Operating System: macOS 12.6 (M1 arm64)

Description

Running the scanner when device Bluetooth is turned off throws an exception that is not possible to catch (unraisable?).

I was expecting the error to be catchable so I can show a more friendly error to the user that they need to turn their Bluetooth on.

What I Did

Running this code with Bluetooth turned off.

import asyncio
from bleak import BleakScanner
from bleak.exc import BleakError

async def callback(device, adv_data):
    pass

async def main():
    try:
        scanner = BleakScanner(callback, service_uuids=[])
    except BleakError as error:
        print('Error:', error)

asyncio.run(main())

Expected output:

Error: Bluetooth device is turned off

Actual output:

Error: Bluetooth device is turned off
Exception ignored in: <CentralManagerDelegate objective-c instance 0x0>
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.10/site-packages/bleak/backends/corebluetooth/CentralManagerDelegate.py", line 103, in __del__
    self.central_manager.removeObserver_forKeyPath_(self, "isScanning")
IndexError: NSRangeException - Cannot remove an observer <CentralManagerDelegate 0x14bed5b70> for the key path "isScanning" from <CBCentralManager 0x600002dfc580> because it is not registered as an observer.
Exception ignored in: <function CentralManagerDelegate.__del__ at 0x10405fac0>

Logs

2023-02-13 22:08:43,494 bleak.backends.corebluetooth.CentralManagerDelegate DEBUG: centralManagerDidUpdateState_
2023-02-13 22:08:43,495 bleak.backends.corebluetooth.CentralManagerDelegate DEBUG: Bluetooth powered off
Error: Bluetooth device is turned off
Exception ignored in: <CentralManagerDelegate objective-c instance 0x0>
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.10/site-packages/bleak/backends/corebluetooth/CentralManagerDelegate.py", line 103, in __del__
    self.central_manager.removeObserver_forKeyPath_(self, "isScanning")
IndexError: NSRangeException - Cannot remove an observer <CentralManagerDelegate 0x149ec3510> for the key path "isScanning" from <CBCentralManager 0x600000950600> because it is not registered as an observer.
Exception ignored in: <function CentralManagerDelegate.__del__ at 0x103d83ac0>

Extra

Ignoring unraisable errors makes the situation a bit better, but not perfect, and also this feels like a very dirty solution:

import asyncio
from bleak import BleakScanner
from bleak.exc import BleakError

async def callback(device, adv_data):
    pass

async def main():
    try:
        scanner = BleakScanner(callback, service_uuids=[])
    except BleakError as error:
        import sys
        sys.unraisablehook = lambda x: None

        print('Error:', error)

asyncio.run(main())

Output:

Error: Bluetooth device is turned off
Exception ignored in: <function CentralManagerDelegate.__del__ at 0x1040cfb50>
@dlech dlech added bug Something isn't working Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend labels Feb 13, 2023
dlech added a commit that referenced this issue Feb 19, 2023
…n in __del__

Since the init() method can raise an exception before registering the
observer, attempting to unregister the observer will fail with
IndexError. We can safely ignore this error.

Fixes: #1219
dlech added a commit that referenced this issue Feb 19, 2023
…n in __del__

Since the init() method can raise an exception before registering the
observer, attempting to unregister the observer will fail with
IndexError. We can safely ignore this error.

Fixes: #1219
@dlech dlech mentioned this issue Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants