Repository navigation
Conversation
Devices can outlive their context in some cases (in particular with python garbage collection). Guard against this happening by clearing the ctx pointer so that it is not pointing to uninitialized memory.
|
I think the fix itself seems to be a quick fix to address the paticular issue, which may or may not be the right fix. Let's check with pyusb side. @jonasmalacofilho
|
|
This usage seems wrong to me. The context should not be destroyed with outstanding devices. I mean we should be better but i think the Python library is doing it wrong. Thoughts? |
|
I can only reproduce it with PyUSB 1.0.2, which is considerably outdated by now (released in 2017). In particular, it lacks pyusb/pyusb@f644ea7a6b7e, which fixed an issue around cleanup. # file: repro.py
import usb
devices = list(usb.core.find(find_all=True))
print(f"Found {len(devices)} USB devices")
# make the failure more obvious and reliable
del(devices)(It would have helped if the Fedora bug report mentioned the PyUSB version and showed the contents of PyUSB 1.1.0 (from 2020) and later already don't result in a segfault: That said, the debug output from LibUSB still shows the call to So while the documentation of Finally, note that unlike with PyUSB 1.0.2, more recent versions don't result in warnings like: P.S. I also tested (and am the maintainer of) liquidctl: no issues with LibUSB 1.0.25 and latest PyUSB. Footnotes |
|
also feel like context should not be e destroyed with outstanding devices. |
|
@benzea What is the version of pyusb you are using? Please also post the contents of "playground.py". Thanks. |
|
BTW, there are two issues related to libusb_unref_device(). |
|
The Fedora bug was reported on FC36 and they have only pyusb 1.0.2. https://src.fedoraproject.org/rpms/pyusb EDIT: I read the version wrongly at first. |
|
Well, it happens on F35 too, which is why I cancelled the F35/F34 updates. I reproduced this with Reproduction is trivial, I just do: Interestingly, the Either way, while I think that pyusb is doing something wrong (as mentioned in the downstream bug), there is also a libusb bug that is fixed by this patch. |
|
Right, I should have read the other comments properly first. So pyusb is already fixed then, and Fedora should really update it. |
|
Great, thanks to the clear analysis from Jonas, the issue is clear now. Fedora is using pyusb 1.0.2 and they should upgrade pyusb version in this case. @hjelmn @tormodvolden |
I know I am repeating myself. But I really think this patch is necessary. Without it, libusb will correctly warn about the misuse of the API, and then continues to crash anyway. |
|
I kind of like how the change exposed these usage issues. I would even support an explicit |
I think @benzea is correct that what exposed the issue in 1.0.25 was Specifically, the global context was used before 6cae9c6 ("core: update usbi_dbg to take the context as an argument"). Can you please confirm whether the restriction on I ask because it isn't documented in either function, and getting Python finalizers to execute in a particular order is tricky. So part of me doesn't want to open that particular can of worms... at the same time, I think this restriction makes sense, since one cannot try to open a Anyway, if this restriction is intended, then PyUSB is still not behaving correctly, even if we haven't been able to reproduce the segfault with its latest versions. |
|
In my understanding libusb_unref_device should not be called after the context is destroyed. The warning in libusb_exit is intended to aid in debugging an erroneous program. From what I can tell this was never properly documented so I consider it a documentation bug. The fact that it worked before 1.0.25 is more dumb luck than anything. It could have been broken at any time (or could have never broken for that matter) so pyusb is in error here. They should always destroy all libusb objects before the context is destroyed. This usage only became apparent due to some work to rethink the global context. In this particular case wanted to ensure that usbi_dbg honors the debug level of the context the device belonged to (which it was not). |
|
All that said. I am leaning toward taking this because accessing the destroyed context is not great. pyusb went a long time without the usage bug being flagged so i may want to make the warning unconditional to avoid future issues if we take this. |
Yeah, the main thing pointing to this is the On the pyusb side, couldn't the device object just store a reference to the context? My thinking is that this should enforce destruction in the correct order (assuming there is no circular reference pointing from the context to the device). |
|
Yeah. I will work on the changes to the warning tomorrow. Once that is done I will merge this. |
Note that the
We already do that, don't we? Besides that, what we could try to do is not have a persistent reference to the context in the module, but that would mean using a different context for every PyUSB I opened pyusb/pyusb#409 for this, and we should continue this part of the discussion there. I'll look into it when I have some time... P.S. I had to resolve similar problem in cython-hidapi a couple of months ago... the end result looks surprisingly simple (since we don't see all the failed attempts that preceded it). |
|
@soerengrunewald |
@hjelmn Is this still valid? |
Devices can outlive their context in some cases (in particular with python garbage collection). Guard against this happening by clearing the ctx pointer so that it is not pointing to uninitialized memory. Closes libusb#1058
|
I would like to merge this and #1073 (and prepare 1.0.26-RC1 soon) so that it gets more testing. We still have to rework the warning in libusb_exit. |
That will be great. |
Devices can outlive their context in some cases (in particular with
python garbage collection). Guard against this happening by clearing the
ctx pointer so that it is not pointing to uninitialized memory.
See: https://bugzilla.redhat.com/show_bug.cgi?id=2050638
This is now happening because the debug output in libusb_unref_device was changed to use the device ctx.