From bb5ea6995bd458afc1bda2e9b6843033f70fc604 Mon Sep 17 00:00:00 2001 From: Martin Rys Date: Mon, 28 Oct 2024 11:07:41 +0100 Subject: [PATCH] Add more typing to the usb module, and catch transfer.submit() failures that can happen when a device resumes from suspend --- scc/drivers/usb.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scc/drivers/usb.py b/scc/drivers/usb.py index 401ab0e7..71cf1bc7 100644 --- a/scc/drivers/usb.py +++ b/scc/drivers/usb.py @@ -18,7 +18,7 @@ import usb1 if TYPE_CHECKING: - from usb1 import USBDeviceHandle + from usb1 import USBDeviceHandle, USBTransfer from scc.sccdaemon import SCCDaemon @@ -41,7 +41,7 @@ def set_input_interrupt(self, endpoint: int, size: int, callback) -> None: callback(endpoint, data) is called repeadedly with every packed received. """ - def callback_wrapper(transfer) -> None: + def callback_wrapper(transfer: USBTransfer) -> None: if (transfer.getStatus() != usb1.TRANSFER_COMPLETED or transfer.getActualLength() != size): return @@ -54,7 +54,10 @@ def callback_wrapper(transfer) -> None: log.error(e) log.error(traceback.format_exc()) finally: - transfer.submit() + try: # https://github.com/C0rn3j/sc-controller/issues/57 + transfer.submit() + except Exception: + log.exception("Failed to submit the transfer!") transfer = self.handle.getTransfer() transfer.setInterrupt( @@ -134,7 +137,7 @@ def force_restart(self): _usb._retry_devices.append(tp) - def claim(self, number): + def claim(self, number: int): """Remember list of claimed interfaces and allow to unclaim them all at once using unclaim() method or automatically when device is closed.