From d3afdce8fccd37507f9eb0ae9b35bf2ebaf5ff93 Mon Sep 17 00:00:00 2001 From: Kars Mulder Date: Tue, 12 Mar 2024 18:51:30 +0100 Subject: [PATCH] Wait up to two seconds for input device. It has been reported 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. 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. --- evdev/uinput.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/evdev/uinput.py b/evdev/uinput.py index c09b4f1..edf948f 100644 --- a/evdev/uinput.py +++ b/evdev/uinput.py @@ -330,11 +330,20 @@ def _find_device_linux(self, sysname): # It is possible that there is some delay before /dev/input/event* shows # up on old systems that do not use devtmpfs, so if the device cannot be # found, wait for a short amount and then try again once. - try: - return device.InputDevice(device_path) - except FileNotFoundError: - time.sleep(0.1) - return device.InputDevice(device_path) + # + # Furthermore, even if devtmpfs is in use, it is possible that the device + # does show up immediately, but without the correct permissions that + # still need to be set by udev. Wait for up to two seconds for either the + # device to show up or the permissions to be set. + for attempt in range(19): + try: + return device.InputDevice(device_path) + except (FileNotFoundError, PermissionError): + time.sleep(0.1) + + # Last attempt. If this fails, whatever exception the last attempt raises + # shall be the exception that this function raises. + return device.InputDevice(device_path) def _find_device_fallback(self): """