Skip to content

Possible race condition on close #230

Closed

Description

@FrogTheFrog

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.

Activity

  1. todbot commented on Nov 4, 2017

    @todbot
    Contributor

    Hi @FrogTheFrog, your analysis sounds spot on. Thank you.

    Your proposed change sounds like the better one long-term but it's a pretty big change that I won't have time to implement and test any time soon.

    As a short-term fix, what about if we put a null test on _hidHandle just before any hid_read()s? It won't eliminate the race condition but could shrink the window of a null pointer chance

  2. FrogTheFrog commented on Nov 4, 2017

    @FrogTheFrog
    Author

    @todbot It should work. I'll try to work on a solution myself. Already got separate threads working on my fork, just need to finish properly implement pooling and closing. Basically, I don't know what should be the interval for a while loop or, in other words, for how long should I put worker thread to sleep?

  3. yackermann commented on Mar 7, 2018

    @yackermann

    Hey guys. I am getting electron crashing with error 3221225477(ACCESS_VIOLATION). I think it is related to the current issue. Any idea what I should do?

  4. neppi commented on Feb 7, 2019

    @neppi

    Hey all,

    i have a windows service using version 0.7.4 of node-hid
    I did not debugged the c part of the node module. But we often have the case that the node service kills itself when calling close() on the HID.device.

    I get no exception at all. I just kills the whole node service without any error. Try catch does not help.

    We bypass the issue by not closing the device. But thats not good i think.

    Maybe this is related to the race condition mentioned by @FrogTheFrog
    Any suggestions?

  5. nine-fox commented on Apr 19, 2019

    @nine-fox

    I got the same issue here. By calling close(), segment fault occurred. Just like @neppi said, I removed the close() call, and everything works fine.

    My only concern is, will it cause the memory growing issue, if I am running it in a long running electron application? That is, lots of hid devices are allocated and used then unused, but never closed, due to plug/unplug usb devices several times. These unused devices are waiting for node.js free I guess.

    My current testing shows fine.

  6. Newbrict commented on Dec 17, 2019

    @Newbrict

    I am also experiencing this issue...

  7. todbot commented on Dec 17, 2019

    @todbot
    Contributor

    Hi, if someone can create a small Node program that exhibits this issue repeatedly on easily-available hardware (e.g. a Teensy or Trinket M0 running a RawHID implementation), we'll be able to diagnose it and find a solution. Thanks!

  8. FrogTheFrog commented on Dec 17, 2019

    @FrogTheFrog
    Author

    @todbot The problem is that on windows this piece or code is being used:

    void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev)
    {
    	if (!dev)
    		return;
    	CancelIo(dev->device_handle);
    	free_hid_device(dev);
    }
    

    and CancelIo does not support multi-threading whereas CancelIoEx does.

  9. todbot commented on Dec 17, 2019

    @todbot
    Contributor

    That is a hidapi piece of code, not node-hid. I would recommend opening an issue there.

  10. FrogTheFrog commented on Dec 17, 2019

    @FrogTheFrog
    Author

    I forgot I had this issue open until today 😄 . Anyway, a quick summary - a PR in hidapi was refused, because it would bring min. Windows requirements up to Windows Vista I believe... Long live the bug ☹️

  11. todbot commented on Dec 17, 2019

    @todbot
    Contributor

    Which hidapi PR is this? With libusb taking over hidap in the last few monthsi it's actually getting improved fairly regularly.

  12. FrogTheFrog commented on Dec 17, 2019

    @FrogTheFrog
    Author

    I kinda mixed up an issue and a PR. Anyway, the "refusing" (but not really) part is signal11/hidapi#48 and the never merged PR is signal11/hidapi#208.

  13. todbot commented on Dec 17, 2019

    @todbot
    Contributor

    Yes, the original hidapi owner seemingly has abandoned the project. But a new fork is being updated regularly at libusb/hidapi If you have time, please re-open the issue there. (I'll put some effort toward it too but it really helps if other users come forward). Thanks for your patience.

  14. reopened this on Dec 17, 2019
  15. FrogTheFrog commented on Dec 17, 2019

    @FrogTheFrog
    Author
  16. todbot commented on Dec 18, 2019

    @todbot
    Contributor

    Hey @FrogTheFrog, if you have a repeatable test case for the issue you were originally seeing, can you try it out on latest node-hid repo? I think I may have finally fixed the bug Windows users were having when using device.on('data', ...)

  17. Julusian commented on Jun 3, 2025

    @Julusian
    Collaborator

    The library has been largely rewritten since this was opened, if this is still a problem please open a new issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions