-
-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fatal Python error: Segmentation fault on 0.11.0.post2 #128
Comments
You need to compile with debug symbols ( |
I also get a segfault using any version >= 0.11. If I revert b94c8a0 everything works. The problem is the |
@bearsh did you call the |
@hnrhonkala88 same here - https://github.com/Yepkit/pykush/blob/1ee65a7646acc14680321ea9d0a69f22a60dcd69/pykush/pykush.py#L223-L226 - the del destructor is not guaranteed to be called in python; so I guess you need to call the |
no I did not. once I close the device it doesn't segfault anymore. |
I think it is better to fix the faulty applications that do not close the descriptors. Since the segfault occurs at the end of the application run, it is not harmful anyway and as you say, the memory gets freed anyway. |
I see your point, as well as the point of the commit that introduced the change. However, while a segfault is fairly harmless, this breaks memory safety. And the segfault isn't guaranteed. Another thing to consider is that the Python interpreter may have been embedded in another software. In that case, |
Replace atexit.register() with weakref.finalize(), since that should guarantee that there are no remaining references to the module (and, thus, HIDAPI) when it runs. Related: trezor#128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
hid_exit() is only safe to call once every hid_device has been closed. Fixes: trezor#128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
I tried to improve the situation in PR #130. Can you give it a try, with and, more importantly, without explicitly calling Essentially, we cannot call The only simple way I found to fix the issue is to use I have also turned our public
|
Thanks everyone about having this discussion and let me understand it more. @jonasmalacofilho Thanks, I will try to give it a try as soon as I can. EDIT: The fix in your PR seems to work! |
Calling hid_exit() can lead to memory bugs if care is not taken. This has been observed as segmentation faults with the LibUSB backend, where hid_exit() calls libusb_exit(), which in turn has a well documented contract: > Deinitialize libusb. Should be called after closing all open devices > and before your application terminates. While the Linux HIDRAW backend appears to be more tolerant, there may be other backends that behave like LibUSB, or even have additional prerequisites for calling hid_exit(). On its part, HIDAPI is not clear about the requirements of hid_exit(): > This function frees all of the static data associated with HIDAPI. It > should be called at the end of execution to avoid memory leaks. Because of these reasons, there is no alternative but to threat hid_exit() as a very unsafe function. As such, any wrapper we provide for it – in practice, hidapi_exit() – would also have to be made safe. Unfortunately, doing that is non-trivial, and possibly quite expensive. Instead, turn hidapi_exit() into a no-op for now. The HIDAPI library will still be cleanly finalized before the program terminates, through another – automatic – mechanism. Related: trezor#128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
It was not supposed to be used explicitly in the first place (see comments on the PR this commit is a part of). The HIDAPI library will still be cleanly finalized before the program terminates, through another – automatic – mechanism. Additionally, the only currently known caller is using it as a workaround for another issue (trezor#114), and that use case would not be supported by the no-op compatibility shim anyway. Related: trezor#114 ("Cannot reenumarate devices in same python process") Related: trezor#128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
Replace atexit.register() with weakref.finalize(), since that should guarantee that there are no remaining references to the module (and, thus, HIDAPI) when it runs. Related: #128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
hid_exit() is only safe to call once every hid_device has been closed. Fixes: #128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
Calling hid_exit() can lead to memory bugs if care is not taken. This has been observed as segmentation faults with the LibUSB backend, where hid_exit() calls libusb_exit(), which in turn has a well documented contract: > Deinitialize libusb. Should be called after closing all open devices > and before your application terminates. While the Linux HIDRAW backend appears to be more tolerant, there may be other backends that behave like LibUSB, or even have additional prerequisites for calling hid_exit(). On its part, HIDAPI is not clear about the requirements of hid_exit(): > This function frees all of the static data associated with HIDAPI. It > should be called at the end of execution to avoid memory leaks. Because of these reasons, there is no alternative but to threat hid_exit() as a very unsafe function. As such, any wrapper we provide for it – in practice, hidapi_exit() – would also have to be made safe. Unfortunately, doing that is non-trivial, and possibly quite expensive. Instead, turn hidapi_exit() into a no-op for now. The HIDAPI library will still be cleanly finalized before the program terminates, through another – automatic – mechanism. Related: #128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
It was not supposed to be used explicitly in the first place (see comments on the PR this commit is a part of). The HIDAPI library will still be cleanly finalized before the program terminates, through another – automatic – mechanism. Additionally, the only currently known caller is using it as a workaround for another issue (#114), and that use case would not be supported by the no-op compatibility shim anyway. Related: #114 ("Cannot reenumarate devices in same python process") Related: #128 ("Fatal Python error: Segmentation fault on 0.11.0.post2")
Hi,
My python tests started to fail after update to hidapi 0.11.0.post2 -version. version 0.10.1 seems to work fine.
Initially the problem was detected on our CI, where docker dependencies were automatically updated and suddenly all our CI runs were just jammed (did not exit at all) but the root cause finding took some time for us. Anyway, it seems that it's hidapi v0.11.0.post2 that caused this issue.
Now I was finally able to get some logs on my local virtual machine setup on :
ubuntu 20.04.1
python 3.7.10
Main problems:
We are using the Yepkit's pykush tool, https://github.com/Yepkit/pykush
Failure that we receive is:
I was able to find following failure with gdb:
Please note that pykush version has been same all the time.
The text was updated successfully, but these errors were encountered: