eblot / pyftdi

FTDI device driver written in pure Python
Other
509 stars 212 forks source link

Not all devices are released with UsbTools.release_all_devices #343

Open marcintomiczek opened 1 year ago

marcintomiczek commented 1 year ago

I am using pyftdi to communicate with USB device to perform some tests, in which I'm setting pins on the device. To do this, I communicate with device in following manner:

ftdi = Ftdi()
device = UsbTools.get_device(UsbDeviceDescriptor(...)
ftdi.open_from_device(device, interface=1)
pins = ftdi.read_pins()
ftdi.close()

Between different test cases, external tool is executed in separate process, that verifies if test environment is in expected state, which also requires access to usb device. It looks like before that tool is executed, all devices must have resources released back. Calling UsbTools.release_all_devices() seems to not solve this. This is probably due to the way UsbTools.get_device iterates over all USB devices first, and they are not kept after in UsbTools.Devices dictionary (only those that match UsbDeviceDescriptor are kept).

I do the following workaround to ensure that all resources are released:

for devs in UsbTools.UsbDevices.values():  # another dictionary
    for dev in devs:
        usb.util.dispose_resources(dev)

It was quite difficult to come up with this solution and I believe UsbTools.release_all_devices() should allow to handle this situation on its own.

eblot commented 1 year ago

Yes, I think you're right.