-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Cannot reenumarate devices in same python process #114
Comments
Another thing to check on Linux is the output of |
Disconnecting and reconnecting the mcp2221 to the system produces the below dmesg:
It does not seem to be getting grabbed by another driver.
I can connect to it fine. |
Yup, agreed. I think you're right that the patch to add |
except I'm pretty sure I compiled the version with that patch, and it still wasn't working.
Has the version with the hid_ext() been pushed to pypi yet? |
You are not exiting the process, so hid_exit is not being called. But anyways, enumerate should work just fine. You should not need to run hid_exit to be able to enumerate.
Not yet |
So the thing I want to do is re enumerate a reconnected device without spawning a new python process. Ideally without disconnecting other devices as well (but can work around that). I bumped the source version number and ensured that it had the hid_ext patch and that it was installed correctly.
So it is possible to reconnect to devices now, but you have to nuke everything else connected via hidapi to do so. Also, calling hidapi_exit() with a device still connected produces a segmentation fault, or at least a core dump
|
I cannot reproduce this with the hidraw backend, it could be a bug in hidapi-libusb. |
yeah, I am using libusb. |
Actually, I still couldn't reproduce the enumeration issue with the libusb backend (using cython-hidapi 0.10.1, hidapi 0.10.1 and libusb 1.0.24; linux 5.11.4). Plugging the device in and out should while running the program bellow should have showed me the problem, right? import hid
import time
def enumerate_hids():
print("HID devices found:")
for x in hid.enumerate():
print(
"{:04x}:{:04x} {} {} at: {}".format(
x["vendor_id"],
x["product_id"],
x["manufacturer_string"] or "(None)",
x["product_string"] or "(None)",
x["path"],
)
)
if __name__ == "__main__":
while True:
enumerate_hids()
print("---")
time.sleep(1) Version directly using hidapi from C: /* repro.c */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include "hidapi/hidapi.h"
int enumerate() {
struct hid_device_info *head = hid_enumerate(0, 0);
struct hid_device_info *cur = head;
if (!head)
return -EIO;
printf("HID devices found:\n");
do {
printf("- %04x:%04x %ls %ls at: %s\n", cur->vendor_id,
cur->product_id, cur->manufacturer_string,
cur->product_string, cur->path);
cur = cur->next;
} while (cur != NULL);
hid_free_enumeration(head);
return 0;
}
int main()
{
int ret;
while (1) {
ret = enumerate();
if (-ret)
return -ret;
printf("---\n");
sleep(1);
}
return 0;
}
|
yeah, that program looks like it should work. The a dockerfile that should work to reproduce the error:
Given that this now appears to potentially be related to passing devices through docker, and there is a workaround. It's possible you want to close this issue unless you feel like debugging that mess. If I find out more useful information I will likely post it here. It is interesting that restarting Python or hidapi_exit() works to refresh devices though. |
@Teslafly regarding Docker, you shouldn't need a privileged container or full access to
this works for me (I access multiple hid devices, which get plugged/unplugged by the user multiple times over the lifetime of the application, from a container using libusb). for more info see Marc Merlin's post http://marc.merlins.org/perso/linux/post_2018-12-20_Accessing-USB-Devices-In-Docker-_ttyUSB0_-dev-bus-usb-_-for-fastboot_-adb_-without-using-privileged.html |
Hi @Teslafly , I'm having the same device reconnection issues only within a docker container. Did you manage to find a workaround? I haven't tried the |
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")
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")
I guess this is no longer an issue with cython-hidapi but rather docker container. So this can be closed, right? |
It seems that once hidapi has been enumerated, it cannot detect / connect to new or replugged devices.
I have 2 examples.
First, using the
hid.enumerate()
example code, I have an mcp2221 device disconnected and runThen I connect the mcp2221 and it shows up in lsusb:
rerunning
hid.enumerate()
and the mcp2221 is not found:and I can't connect to the device either:
but if I restart python I can see the device:
and I can connect to it:
But if I unplug it and replug it, and close and reopen, I cannot reconnect to the same device:
It requires restarting python (or spawning a new python process) to get the device connected again.
Though if you start python, import hid but do not use any functions, then connect your device and run an hid function it will connect/find your device just fine. Just after the first hid function has been run your device list is locked in.
Is this a limitation of libusb hidapi itself? or something this library can fix by properly reinitialising the hidapi library? It seems like the list of usb devices may be being cached and not updated.
This is running in a docker container (but works the same on a native ubuntu system) and I have built the hidapi library from source because it seemed like #91 might fix it. (Though I have not verified this source build library is the actual one being used vs pypi)
The text was updated successfully, but these errors were encountered: