gvalkov / python-evdev

Python bindings for the Linux input subsystem
https://python-evdev.rtfd.org/
BSD 3-Clause "New" or "Revised" License
336 stars 112 forks source link

Wait up to two seconds for input device to become available. #215

Closed KarsMulder closed 8 months ago

KarsMulder commented 8 months ago

It has been reported in issue #214 that even on modern Linux distributions that use devtmpfs, it can take longer than 0.1 seconds before the input device becomes available with the right permissions.

To solve this: if sysfs reports that a specific input device should be used, but it cannot be opened immediately, keep trying again for up to two seconds.

This only adjusts the _find_device_linux function, because adding such a loop to _find_device_fallback runs at risk of causing issue #205 again on outdated operating systems: if the sought device is not readable yet, but another device with the same name already exists, then there is a risk that we open the other device with the same name instead of the intended one.

(Unless _find_device_fallback refuses to return a device if another device with a higher event number is still unreadable. But that goes another layer deeper into "I do not have the proper environment to test this", so I've refrained from implementing that.)

A sporadic error of "sometimes the wrong device is opened when the system is under heavy load" is even harder to debug than a sporadic error of "sometimes no device is opened at all when the system is under heavy load".

The function _find_device_linux does not risk returning the wrong device because sysfs can immediately tell us exactly which device we need, even if the that input device doesn't have the right permissions yet.

sezanzeb commented 8 months ago

Thanks a bunch!