question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BlueZ] Scanner fails to close sockets, which ain't that great

See original GitHub issue
  • bleak version: 0.14.3
  • Python version: 3.10…5
  • Operating System: Gentoo Linux, of course!
  • BlueZ version (bluetoothctl -v) in case of Linux: 5.64

Description

Bleak’s BlueZ-backed scanner implementation fails to close sockets in a timely manner – like, ever. That’s badness incarnate, because sockets are extremely scarce OS resources. It’s definitely best not to squander them. Sadly, bleak squanders them.

What I Did

Minimal length example or it didn’t happen, so:

$ python3.10 -X dev -c '
from asyncio import run
from bleak import BleakScanner

run(BleakScanner.discover())
'
sys:1: ResourceWarning: unclosed <socket.socket fd=6, family=AddressFamily.AF_UNIX, type=SocketKind.SOCK_STREAM, proto=0, raddr=/run/dbus/system_bus_socket>

😮

Note that the -X dev is essential here. By default, Python hides many critical things that it really shouldn’t. This includes deprecation and resource warnings. Sad, yet true.

Above, -X dev is disclosing that the bleak.backends.bluezdbus.scanner.BleakScannerBlueZDBus.start() method is failing to clean up after itself. Interestingly, the companion bleak.backends.bluezdbus.client.BleakClientBlueZDBus.connect() method does clean up after itself: e.g.,

# In "bleak.backends.bluezdbus.client":
class BleakClientBlueZDBus(BaseBleakClient):
    ...

    async def connect(self, **kwargs) -> bool:
        try:
            ...
        except BaseException:
            self._cleanup_all()
            raise

The BleakClientBlueZDBus._cleanup_all() method, in turn, correctly deallocates and closes all currently open resources. The BleakScannerBlueZDBus class could benefit from a similar _cleanup_all() method, which the BleakScannerBlueZDBus.start() method should then call from a similar try: ...; except BaseException: ... branch.

And… We Done Here

Thanks so much (as always!) for this phenomenal framework. Bleak enables our entire tech stack. We wouldn’t be here without you. Here’s to you, breathtaking dev team. 🥂

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dlechcommented, Jul 26, 2022

Closing as duplicate of #805.

1reaction
dlechcommented, Jul 12, 2022

FYI, I’m currently working on some changes that will sidestep the issue for the scanner.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Too many open files" error in BlueZ log after a while #805
"Too many open files" error in BlueZ log after a while #805. Closed ... [BlueZ] Scanner fails to close sockets, which ain't that...
Read more >
AWS-Greengrass BLE scanner. Problem running python script ...
Problem running python script Sockets Bad File Descriptor ... I am getting an issue with the main python bluetooth package bluez .
Read more >
Bluetooth & Pairing FAQs, Troubleshooting - Socket Mobile
Possible Cause: Solution: The scanner is too close or too far from the barcode or at an incorrect angle. ... Practice scanning at...
Read more >
How to Fix Bluetooth Pairing Problems - Techlicious
Can't connect two devices? Try these 16 tips to solve your Bluetooth pairing problems.
Read more >
How To Fix Bluetooth Problems On A Mac - Macworld
Here's a complete guide to pairing Bluetooth devices with your Mac, ... Here's what to do if Bluetooth will not connect, isn't supported, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found