From 5fb3190c1288b7c96ec74cffad538db4135faa8e Mon Sep 17 00:00:00 2001 From: Kars Mulder Date: Mon, 1 Apr 2024 10:57:06 +0000 Subject: [PATCH] Wait up to two seconds for input device. (#215) 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): """