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

OSError: [Errno 24] Too many open files #169

Closed
farzadpanahi opened this issue Mar 19, 2020 · 4 comments
Closed

OSError: [Errno 24] Too many open files #169

farzadpanahi opened this issue Mar 19, 2020 · 4 comments
Assignees
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend bug Something isn't working
Milestone

Comments

@farzadpanahi
Copy link

farzadpanahi commented Mar 19, 2020

bleak version: 0.6.1
Python version: 3.8
Operating System: ArchArm Linux
BlueZ version: 5.54

What I Did

I wrote a script that keeps trying to connect to a BLE device. It runs in an infinite loop forever. I let it run overnight when the BLE device was not in range and obviously it would be failing to connect. In the morning I check the logs and it is failing with OSError: [Errno 24] Too many open files.

Stacktrace

Traceback (most recent call last):
  File "./my-app.py", line 29, in connect_to_device
    async with BleakClient(device.ble_mac, loop=loop) as client:
  File ".../venv/lib/python3.8/site-packages/bleak/backends/bluezdbus/client.py", line 62, in __init__
    p = subprocess.Popen(["bluetoothctl", "--version"], stdout=subprocess.PIPE)
  File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1603, in _execute_child
    errpipe_read, errpipe_write = os.pipe()
OSError: [Errno 24] Too many open files

Code

async def connect_to_device(device, loop):

    while True:
        try:
            logInfo("connecting to {} ...".format(device.ble_mac))
            async with BleakClient(device.ble_mac, loop=loop) as client:
                logInfo("connected to {}".format(device.ble_mac))
                ble = BleDevice(device, client)
                await ble.run()

                while await ble.is_connected():
                    logDebug('waiting ...')
                    await asyncio.sleep(1)
                
                logInfo('disconnected from {}'.format(device.ble_mac))

        except Exception as e:  # TODO: catch specific exception
            logException(e)


def main():
    devices = get_devices(config)

    loop = asyncio.get_event_loop()

    for d in devices:
        loop.create_task(connect_to_device(d, loop))

    loop.run_forever()


if __name__ == "__main__":
    main()

@dlech
Copy link
Collaborator

dlech commented Mar 19, 2020

There are quite a fixes in v0.6.1. Have you tried that version?

@farzadpanahi
Copy link
Author

@dlech Actually I am using the latest v0.6.1 release. (Will update the issue description above)

@hbldh hbldh self-assigned this Mar 27, 2020
@hbldh hbldh added the Backend: BlueZ Issues and PRs relating to the BlueZ backend label Mar 27, 2020
@hbldh
Copy link
Owner

hbldh commented Mar 27, 2020

No, it does not work. Bleak or txdbus or (more unlikely) the BlueZ DBus API does not close things appropriately and leaks file descriptors. This leads to this problem in you use it continuously. The simpel solution is to not use it continuously.

I will not look at this in the foreseeable future I am afraid. I am swamped with other (paid) work and cannot guarantee doing anything with this project for quite some time. I will still address PRs to develop branch occasionally, so if you desire these changes you have to do the legwork yourselves.

@hbldh
Copy link
Owner

hbldh commented Jul 20, 2020

I think this was already addressed in #154 and released in version 0.6.2. It might be further improved by #236 though.

@hbldh hbldh closed this as completed Jul 20, 2020
treibholz pushed a commit to treibholz/AirStatus that referenced this issue Jan 3, 2022
There was a bug in bleak (hbldh/bleak#169),
which resulted in crashes. Updating to a newer version of bleak fixes
this issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: BlueZ Issues and PRs relating to the BlueZ backend bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants