From bed4a34d184a68117a0870d9c3f6abc96012bd6d Mon Sep 17 00:00:00 2001 From: Ken Sanislo Date: Tue, 10 Mar 2026 21:23:33 -0700 Subject: [PATCH] Treat empty hidraw read as device removal (EOF) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When select() reports a hidraw fd as readable but os.read() returns empty bytes, that's EOF per POSIX — the device has been removed. Previously this was silently treated as no data, causing the listener to loop indefinitely on a gone device instead of cleaning up. --- lib/hidapi/udev_impl.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/hidapi/udev_impl.py b/lib/hidapi/udev_impl.py index 8b0c1f03..5ad54069 100644 --- a/lib/hidapi/udev_impl.py +++ b/lib/hidapi/udev_impl.py @@ -403,6 +403,8 @@ def read(device_handle, bytes_count, timeout_ms=-1): data = os.read(device_handle, bytes_count) assert data is not None assert isinstance(data, bytes), (repr(data), type(data)) + if not data: # empty read when select() said readable means EOF (device removed) + raise OSError(errno.EIO, f"device disconnected on file descriptor {int(device_handle)}") return data else: return b""