node-hid: Possible race condition on close
Hello,
like some other people, I sometimes get an error when closing a device. It’s a very rare error on my system, however I managed to catch it and it was about access violation at 0x00000000. Basically, this means that something that has been freed is being used again (could be related to #229 and maybe #210).
After analyzing code I found a possible race condition with HID::close and HID::recvAsync. If _hidHandle is freed by HID::close just before hid_read(hid->_hidHandle, buf, sizeof buf) is called, we will try to access already freed memory. hid_read itself contains no null handle check whatsoever.
Given that hid_read is not thread safe, I believe that the only possible solution is an active pooling with hid_read_timeout(dev, data, length, 0) on a separate thread with some sort of timeout or thread sleep duration.
Using separate threads would also solve a problem with node’s 3 remaining threads being locked up by node-hid when reading more than 3 devices that are not sending data.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Comments: 15 (6 by maintainers)
Commits related to this issue
- Change async "data" read use hid_read_timeout and check for closed handle.\nPerhaps addresses issues #358, #210, #153, maybe even #230 — committed to node-hid/node-hid by todbot 5 years ago
@todbot done https://github.com/libusb/hidapi/issues/133