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

File access won't be closed on /dev/i2c #247

Closed
toefde opened this issue Apr 14, 2023 · 3 comments · Fixed by #252
Closed

File access won't be closed on /dev/i2c #247

toefde opened this issue Apr 14, 2023 · 3 comments · Fixed by #252
Labels
bug Something isn't working

Comments

@toefde
Copy link

toefde commented Apr 14, 2023

  • Input source: HDMI
  • Output device: Raspberry Pi 4
  • Operating system: Raspberry OS
  • Python version: 3
  • monitorcontrol version: 3.0.2

Hi, I've got a problem with the python library.
Everytime I execute monitorcontrol.get_monitors() it accesses the file (in my case) /dev/i2c-21 with a new file descriptor.
I need to execute it every second and after a few hundred iterations my script crashes with [Errno 24] Too many open files. Because there is a limit of open files on the system (Yes, I could increase the limit, but it won't help) is it possible to dispose whatever the library does there? I need to somehow evade this problem.

Thanks for the help

@newAM newAM added the bug Something isn't working label Apr 14, 2023
@newAM
Copy link
Owner

newAM commented Apr 14, 2023

I'm pretty busy this weekend, but I can try to take a look next weekend. This was the first python library I wrote, it's likely that there's just something sub-optimal somewhere :)

Do you have some simple code I can use to reproduce this?

@toefde
Copy link
Author

toefde commented Apr 14, 2023

Yes of course.

  • Run python3 on the terminal
  • Open a second terminal and get the pid of python3 ps -a
  • With sudo lsof -p pidofpython3 there is no /dev/i2c device in access
  • Run in the python terminal:
    • import monitorcontrol
    • test = monitorcontrol.get_monitors()
  • Now sudo lsof -p pidofpython3 there is one /dev/i2c device in access
  • And if you run test = monitorcontrol.get_monitors() in the python terminal again, there will be more /dev/i2c devices in access

The limit of open files will be reached sooner or later. But keep in mind the limit depends on if the python script is started as a service or started automatically on startup (standard open file limit is 1024 on Raspberry OS on a RPi 4 Model B 2GB). There are more differences if I execute this manually over ssh in the Visual Studio Code terminal the script won't crash. I don't know the difference there yet.

So let's grab the problem at the root and try to avoid the multiple open i2c files. :)

Huge thanks for the quick support

@newAM
Copy link
Owner

newAM commented Apr 22, 2023

Thanks for that reproduction, that was very helpful!

This is indeed a bug. When exceptions were generated, which occurred for non-monitor devices on the i2c bus __exit__ never got called and the file descriptors never got closed.

I normally wait for feedback on fixes, but I'm pretty confident that #252 fixes this, so I'll merge that and cut a release. If that doesn't fix it let me know and I can do another release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants