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:
- Create a program where
usb.on("attach")
andusb.on("detach")
is used. - Run the program and disconnect or connect a board.
- Notice the failure.
Additional info:
I downgraded to 2.4.3 and the issue went away.
Exception:
Call stack:
Additional Information
- Operating System: Windows 10
- Runtime (
node -v
orelectron -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
- fix: windows hotplug cmNotifyFilter access violation #540 — committed to Julusian/node-usb by Julusian 2 years ago
- fix: windows hotplug cmNotifyFilter access violation #540 (#541) * fix: windows hotplug cmNotifyFilter access violation #540 * Update windows.cc — committed to node-usb/node-usb by Julusian 2 years ago
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