Skip to content

Commit

Permalink
software/usb/device: endpoint should keep reference to the usb device
Browse files Browse the repository at this point in the history
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
  • Loading branch information
povauboin committed Nov 7, 2024
1 parent ba52bef commit e87ae64
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lambdalib/software/usb/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e87ae64

Please sign in to comment.