node-usb: Native code crashing

Bug Description:

I recently upgraded to node-usb 2.5.*, and I started noticing that the native code was crashing my program when plugging in or unplugging hardware. I was able to debug it with VS and found it hit a call stack inside of PNP (plug and play) in native code.

Steps to Reproduce:

  1. Create a program where usb.on("attach") and usb.on("detach") is used.
  2. Run the program and disconnect or connect a board.
  3. Notice the failure.

Additional info: I downgraded to 2.4.3 and the issue went away. Exception: image

Call stack: image

Additional Information

  • Operating System: Windows 10
  • Runtime (node -v or electron -v): 16.13.1, node.
  • Node USB Version (npm ls usb): 2.5.1

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 18 (12 by maintainers)

Commits related to this issue

Most upvoted comments

OK, the cause of this problem is a race condition, thanks @arekzaluski!

For some reason, Windows is firing attached events into the JavaScript code and libusb can get the details, but the device isn’t yet ready to write.

@Julusian is there a later Windows event we can use to indicate hotplug or can we wait for something to be ready in the native Windows code?

In the meantime, I’ve introduced a dirty delay over at #577 which fixes the issue for now.

No, not something I’ve seen there, but the code here was written differently and with a newer api.

I have a theory, with a hopeful fix #541