pyusb: OSError with wxPython
If I try to find the USB devices in a wxPython window, I get the following error:
Exception ignored in: <bound method _AutoFinalizedObjectBase.__del__ of <usb.backend.libusb1._Device object at 0x04307210>>
Traceback (most recent call last):
File "C:\Users\guest\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\_objfinalizer.py", line 84, in __del__
self.finalize()
File "C:\Users\guest\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\_objfinalizer.py", line 144, in finalize
self._finalizer()
File "C:\Users\guest\AppData\Local\Programs\Python\Python36-32\lib\weakref.py", line 548, in __call__
return info.func(*info.args, **(info.kwargs or {}))
File "C:\Users\guest\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\_objfinalizer.py", line 104, in _do_finalize_object_ref
obj._do_finalize_object()
File "C:\Users\guest\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\_objfinalizer.py", line 71, in _do_finalize_object
self._finalize_object()
File "C:\Users\guest\AppData\Local\Programs\Python\Python36-32\lib\site-packages\usb\backend\libusb1.py", line 604, in _finalize_object
_lib.libusb_unref_device(self.devid)
OSError: exception: access violation writing 0x00000014
^C
Here is some simple code that can create the error.
import wx
import usb
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Example")
panel = wx.Panel(self, wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
button = wx.Button(panel, label="Run test()")
button.Bind(wx.EVT_BUTTON, self.test)
sizer.Add(button, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
def test(self, event):
valueList = []
devices = usb.core.find(find_all=True)
for config in devices:
valueList.append((config.idVendor, config.idProduct))
print(valueList)
event.Skip()
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
To produce the error, press the button “Run test()” in the frame that appears after running the code.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (10 by maintainers)
Commits related to this issue
- Fix #203: libusb sometimes cleaned up too early. Each call to libusb1's get_backend returns a new _LibUSB object. When each _LibUSB object falls out of scope, it calls ends up calling libusb_exit() ... — committed to minkustree/pyusb by minkustree 6 years ago
- Patch PyUSB and upgrade libusb to 1.0.22 on Windows builds The patch for PyUSB successfully prevents access violation errors during cleanup with libusb 1.0.22, but "some libusb_devices were leaked" w... — committed to liquidctl/liquidctl by jonasmalacofilho 5 years ago
- Fix #203: libusb sometimes cleaned up too early. (#227) Each call to libusb1's get_backend returns a new _LibUSB object. When each _LibUSB object falls out of scope, it calls ends up calling libus... — committed to AlanJAS/pyusb by minkustree 5 years ago
@JoshMayberry
I understand (including from my own experience) that this is an annoying issue for downstream projects. I’ll try to get a release ready soon, even if some other issues end up missing the cut.
Maybe the following can help.
Ref: https://github.com/eblot/pyftdi/issues/121 eblot commented:
I found a weird behaviour in pyusb: when the port is indirectly closed right after the port is open, some kind of race condition in pyusb/libusb lets it to re-open the closed device to perform the cleanup action, which triggers a native exception in libusb. This has been worked around in the latest PyFtdi patch version (v0.30.1), so that no cleanup action is performed when the existing pyusb handle is not longer existing. This prevents the pyusb resource manager to re-open the device in such event.