daynix / UsbDk

Usb Drivers Development Kit for Windows
Apache License 2.0
522 stars 142 forks source link

Can't re-open device after closing it with outstanding OVERLAPPED event #125

Closed gwgill closed 9 months ago

gwgill commented 10 months ago

If a USB device is opened and a transfer started, but the device is then closed before calling GetOverlappedResult() on the OVERLAPPED object, it is not possible to re-open the device. This can happen as a result of the process being terminated. Re-plugging the device doesn't fix it. Attempts to stop and re-start the UsbDk driver fail. The only way to fix it is to re-boot the machine. (Windows 10 22H2, UsbDk 1.00-22)

gwgill commented 10 months ago

A partial work-around is to intercept process termination with (say) a sighandler, keep track of outstanding transactions and their OVERLAPPED objects, and on termination issue UsbDk_AbortPipe() to all end points and then GetOverlappedResult() on all the outstanding OVERLAPPED objects. This doesn't solve the problem if the process is killed in a way that doesn't invoke the sighandler though.

gwgill commented 9 months ago

It seems that kvojacheck's fix for #105 is a 90% fix for this problem. It seems to prevent the need for a system re-boot, but does sometimes require a device re-plug to recover, which is not ideal.