From e87ae6443be89e0840696c4b38d4510caf92aa29 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Vauboin Date: Thu, 7 Nov 2024 14:02:20 +0100 Subject: [PATCH] software/usb/device: endpoint should keep reference to the usb device without reference to the device, and depending on the invocation the usb device may be collected by the garbage collector while the usb endpoint is still in use. this causes the following error which is difficult to trace: python: core.c:1278: libusb_ref_device: Assertion `refcnt >= 2' failed. now the endpoint keeps a reference to the device preventing it to be freed --- lambdalib/software/usb/device.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lambdalib/software/usb/device.py b/lambdalib/software/usb/device.py index 913afb5..3c277b7 100644 --- a/lambdalib/software/usb/device.py +++ b/lambdalib/software/usb/device.py @@ -7,13 +7,15 @@ class USBEndpoint(): - def __init__(self, handle, size, num, asynchronous=True, context=None, timeout=1000): + def __init__(self, handle, size, num, asynchronous=True, context=None, + timeout=1000, device=None): self.handle = handle self.size = size self.num = num self.asynchronous = asynchronous self.context = context self.timeout = timeout + self.device = device # function alias self.write = self.send @@ -98,7 +100,8 @@ def control_read(self, *args): def get_endpoint(self, num, asynchronous=True): return USBEndpoint(self.handle, self.bulksize, num, - asynchronous=asynchronous, context=self.context) + asynchronous=asynchronous, context=self.context, + device=self) def __del__(self): if self.handle: