You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that once a 'data' listener is added, device can not be closed properly.
I encountered the problem with code like this. ran it in node console
>var HID = require('node-hid');
undefined
>var p = h.devices()[1].path; // had a bar code scanner connected
undefined
>var dev = new HID.HID(p);
undefined
>dev.on('data', function(d){console.log(d)});
......
>dev.removeAllListeners('data');
......
>dev.close(); //close the device
undefined
>dev = new HID.HID(p); //reopen the device, failed, seems like the device is not released
cannot open device with path \\?\hid#vi...blahblahblah
reopen is good when i skip the dev.on('data',...) and dev.removeAllListeners('data') statement.
looked into the code. i think it's caused by a hidapi issue. signal11/hidapi#48
to all that suffered from this issue, a walkaround is suggested in the above link.
In my opinion, this could be fixed in node-hid by calling hid_read_timeout instead of hid_read in the read loop. Before emitting the data event, we'll check for either no data (return 0) or an error (return -1). The error usually means the device handle is closed. Repeat until the return value is something other than 0. I'm trying it out now on my Windows 7 x64 box.
To follow up: preliminary results are negative. I'm getting exactly the same behavior as before. I'll post my edits to a fork in case anyone wants to look at it. There's a very good chance I'm just missing something here.
I posted PR signal11/hidapi#208 to address this issue. This will only improve the experience on Vista and above. On Windows XP, if you have a way to force the device to send data by writing to it, performing a write AFTER removing event listeners but BEFORE calling close() seems to work. Here's an example for my device:
varhid=require("node-hid");vardev=newhid.HID(myVendorId,myProductId);dev.on("data",myListener);// ... Communication happening ...dev.removeAllListeners("data");dev.write([EndPoint,1,EndOfCommandToken]);// Makes device respond with an error messagedev.close();
The problem has to do with the read thread waiting for data that never arrives. As long as the read thread is alive, the system can't release the device handle. Getting the device to send data will let the read thread exit, which lets the device close properly.
Any open device with an installed event listener will block process exit until close.
Unfortunately, even calling dev.removeAllListeners("data") and dev.close() is not sufficient to avoid the hang here (this was on a ubuntu xenial x32 armv7 raspberry pi based device).
Basically, without setting non-blocking, the nodejs process would always hang during shutdown until I terminated it (i.e., SIGTERM) 😞
Hi @peitschie,
Do you have a small self-contained script that demonstrates this?
What hardware are you talking to?
The Ubuntu you're running on your Pi isn't Raspbian, but something else? (I'm a bit confused by the "x32 amv7" designation)
I'd like to try to replicate what you're seeing. I have plenty of Pis and hopefully can get the HID hardware you're talking to.
(The ^C is where I interrupted, as the process wasn't closing)
Checking the open file handles, the /dev/hidraw1 device has been closed at this stage, but still the process won't exit:
root@ubuntu:~# cat /proc/cpuinfo
Processor : ARMv7 Processor rev 1 (v7l)
processor : 0
BogoMIPS : 1.93
processor : 1
BogoMIPS : 1.93
processor : 2
BogoMIPS : 1.93
processor : 3
BogoMIPS : 1.93
Features : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xc05
CPU revision : 1
Hardware : Amlogic Meson8B
Revision : 000a
Serial : 1b00000000000000
root@ubuntu:~# lscpu
Architecture: armv7l
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 4
Socket(s): 1
CPU max MHz: 1536.0000
CPU min MHz: 24.0000
Hypervisor vendor: (null)
Virtualization type: full
Activity
shawnhsy commentedon Apr 24, 2014
looked into the code. i think it's caused by a hidapi issue.
signal11/hidapi#48
to all that suffered from this issue, a walkaround is suggested in the above link.
amullins83 commentedon Jan 8, 2015
In my opinion, this could be fixed in node-hid by calling
hid_read_timeoutinstead ofhid_readin the read loop. Before emitting thedataevent, we'll check for either no data (return 0) or an error (return -1). The error usually means the device handle is closed. Repeat until the return value is something other than 0. I'm trying it out now on my Windows 7 x64 box.amullins83 commentedon Jan 8, 2015
To follow up: preliminary results are negative. I'm getting exactly the same behavior as before. I'll post my edits to a fork in case anyone wants to look at it. There's a very good chance I'm just missing something here.
amullins83 commentedon Jan 9, 2015
I posted PR signal11/hidapi#208 to address this issue. This will only improve the experience on Vista and above. On Windows XP, if you have a way to force the device to send data by writing to it, performing a write AFTER removing event listeners but BEFORE calling
close()seems to work. Here's an example for my device:The problem has to do with the read thread waiting for data that never arrives. As long as the read thread is alive, the system can't release the device handle. Getting the device to send data will let the read thread exit, which lets the device close properly.
peitschie commentedon Jun 19, 2019
I had this issue also, and found that I could workaround it by calling
setNonBlocking(true)on the HID device prior to subscribing to any data.There's an open issue from 2017 about this undocumented function: #216
todbot commentedon Jun 19, 2019
This is expected behavior. Any open device with an installed event listener will block process exit until close.
peitschie commentedon Jun 19, 2019
Unfortunately, even calling
dev.removeAllListeners("data")anddev.close()is not sufficient to avoid the hang here (this was on a ubuntu xenial x32 armv7 raspberry pi based device).Basically, without setting non-blocking, the nodejs process would always hang during shutdown until I terminated it (i.e.,
SIGTERM) 😞todbot commentedon Jun 19, 2019
Hi @peitschie,
Do you have a small self-contained script that demonstrates this?
What hardware are you talking to?
The Ubuntu you're running on your Pi isn't Raspbian, but something else? (I'm a bit confused by the "x32 amv7" designation)
I'd like to try to replicate what you're seeing. I have plenty of Pis and hopefully can get the HID hardware you're talking to.
peitschie commentedon Jun 19, 2019
Hi @todbot
For me, something as simple as this is sufficient to cause the hang:
The output from that is:
(The ^C is where I interrupted, as the process wasn't closing)
Checking the open file handles, the
/dev/hidraw1device has been closed at this stage, but still the process won't exit:I realise this morning there are a bunch of quirks which might be contributing to what I'm seeing though. I'll let you be the judge 😃 .
mknod /dev/hidraw1 c 247 1to create the deviceThe container details are (sorry for the slab of data... I am not sure which is relevant to you):
todbot commentedon Jun 19, 2019
Awesome, thanks for the detailed response.
What is the HID device you are communicating with? Is there a way I can obtain or recreate it?
Also, what does
lsusb -vreport?Is there a quick "getting started with lxc" that I can follow to create a container similar to what you're using?
todbot commentedon Jun 19, 2019
Oh another question. If you change the top of your script to set the driverType to be 'libusb':
is the effect any different?
peitschie commentedon Jun 20, 2019
Thanks for looking at this 😺
Changing the driver to
libusbhad no effect.The output from
lsusb -vis:Regarding the exact hardware, it is a relatively cheap button purchased from http://usbbutton.com/, running on a proprietary hardware system (though I have no way to discover the exact hardware, sorry).
Regarding LXC usage, I did a quick google and browse, and seems like something such as https://linuxcontainers.org/lxd/getting-started-cli/#creating-and-using-your-first-container is decently straightfoward if you have a linux system somewhere.
I followed https://medium.com/@konpat/usb-passthrough-to-an-lxc-proxmox-15482674f11d to figure out how to allow the USB device through to the container. For the USB button, the exact lines I added to the container's config were:
I suspect there's a way to do that for a running, but I don't know exactly how to do it, unfortunately 😿 . It looks like something akin to https://discuss.linuxcontainers.org/t/providing-access-to-loop-and-other-devices-in-containers/1352... but I have no way of discovering the exact commands, sorry, as I only get access from within the container on the proprietary system.