While I'd appreciate the answer, I'd really like to understand how to diagnose what's happening in Python.
The basic issue is that Raspberry PI OS or a driver causes some USB connections to disconnect and immediately reconnect. The device in question is an RFID reader that acts as a keyboard. The consequence is that my application crashes and I lose state. For a variety of reasons I cannot reasonably reset state on a restart. I could save state and restart, but that gets messy fast. We have 2 dozen of these systems running, and only a subset exhibit the problem. I haven't spotted a common theme across installations that aids in identifying what's going on.
I can intercept, identify, and handle the exception that occurs, but Python cannot detect the device after the exception occurs even though it has reappeared in the device directory. Kernel logs indicate it's only gone for about 300 ms. I've tried waiting 10 seconds before trying the device again without success. I'd like to figure out how to get the library and Python to recognize that the device has reappeared without having to restart the application.
My Google FU has failed me: I find others describing the same issue in the OS, but no recommended diagnostic steps, fixes, or work-arounds.
I'm using EVDEV on a Raspberry PI running Python 3.9. (Yes, I'll be updating to the most current available Python version under Raspberry PI OS when I get a different driver updated.)
The resulting exception looks like this:
2025年10月22日 09:21:06 User: {'authorized': True, 'id': '0009444691'}
2025年10月22日 09:21:33 Exception: [Errno 19] No such device
Traceback (most recent call last):
File "drivers/RFID/KeyboardRFID.py", line 28, in run
for event in dev.read_loop():
File "/usr/local/lib/python3.9/dist-packages/evdev/eventio.py", line 46, in read_loop
for event in self.read():
File "/usr/local/lib/python3.9/dist-packages/evdev/eventio.py", line 71, in read
events = _input.device_read_many(self.fd)
The first line is a successful read of the USB device. The exceptions do not appear to correlate in time with reads.
The code block that generates this is:
for event in dev.read_loop():
# If key event and key up (0)
if event.type == ecodes.EV_KEY and event.value == 0:
key = scancodes.get(event.code)
if key == u'\n': # if enter
self.notifyScanObservers(rfid_code)
rfid_code = ""
else:
rfid_code = rfid_code + key
except Exception as e:
logging.error("Exception: %s" % str(e), exc_info=1)
os._exit(42) # Make sure entire application exits
I've modified this to handle the exception, but can't figure out to reconnect to the device without restarting python.
The entire (unmodified) application code base is here: https://github.com/Dallas-Makerspace/RFID-KeyMaster
and the specific driver is:
https://github.com/Dallas-Makerspace/RFID-KeyMaster/blob/master/drivers/RFID/KeyboardRFID.py