mbuesch / razer

Razer device library and tools
http://bues.ch/h/razercfg
GNU General Public License v2.0
250 stars 50 forks source link

Close client fd on disconnect #134

Closed GrantM11235 closed 4 years ago

GrantM11235 commented 4 years ago

I noticed that after razerd had been running for a while it would use a lot of cpu. I discovered that this is due to a combination of three bugs:

  1. Every time a usb device is disconnected, razerd thinks that five clients and five privileged clients connected and disconnected
  2. razerd doesn't close client socket file descriptors when they disconnect
  3. When razerd can't open any more file descriptors, the main loop runs continuously at full speed, maxing out a cpu thread

This pull request effectively solves the issue by fixing the second bug, but it would probably be a good idea to look at the other bugs as well.

mbuesch commented 4 years ago

Thanks for your pull request. I also added a change that should avoid the fast-loop problem. Could you please try latest master? I currently don't have access to my Razer mice. Thanks a lot :)

GrantM11235 commented 4 years ago

Thanks for the quick response!

I reverted my fix to test out yours, it still runs at full speed after 1024 fds have been opened. It looks like you need to check accept for errors rather than select, strace shows the following lines repeated over and over:

select(1024, [9 10], NULL, NULL, NULL)  = 2 (in [9 10])
accept(10, 0x7ffdead5ad40, [110])       = -1 EMFILE (Too many open files)
accept(9, 0x7ffdead5ad40, [110])        = -1 EMFILE (Too many open files)
mbuesch commented 4 years ago

Thanks for the feedback. I added a change 066d476fb32c06940bace967a63ac107f8ee8fd6 that checks for failure in all socket operations.

GrantM11235 commented 4 years ago

That works great, cpu usage stays at 0.0%

mbuesch commented 4 years ago

Thanks for testing.

mbuesch commented 4 years ago

Fixed in version 0.42.