[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:
- Created a year ago
- Comments:9 (3 by maintainers)
Top GitHub Comments
Closing as duplicate of #805.
FYI, I’m currently working on some changes that will sidestep the issue for the scanner.