rene-aguirre / pywinusb

USB / HID windows helper library
BSD 3-Clause "New" or "Revised" License
207 stars 63 forks source link

Solved issue of pywinusb Input threads not closing or terminating. #54

Open warrenarea opened 5 years ago

warrenarea commented 5 years ago

I have had issues in the past of threads not closing properly. When you set up a raw data handler, pywinusb creates two threads:

InputReportProcessing
InputReportReader

and in theory, if you script everything correctly, and all your functions get returned, and you do the proper cleanup, that these threads will (should) close on their own.

However, i have had instances where this did not take place. I have seen on stackoverflow there are many people who try to terminate/abort threads, and because of the locks, it just doesn't seem to be effective.

threading._after_fork()

I discovered this function, that... doesn't seem to be documented, but... the nearest i can tell, it calls an additional function that is designed to "reset all locks" for python. This function seems to work wonderfully, and is a good way of freeing up our threads.

I'm not sure if it is something that could be incorporated into the project, because... you would hate to close all threads or locks that should be in place.... but, maybe it could still be incorporated... or at very least documented as a last resort option.

Here is code also, to list the threads (for troubleshooting).

for t in threading.enumerate():
    print(t.getName())
rene-aguirre commented 5 years ago

Thanks @warrenarea, do you have more details of what version of os, python and pyWinUsb you had when hitting the threading issues?

@c1728p9 invested a lot of time tuning the threading, specially on scenarios involving removing devices. So any hints about reproducing the failures would be appreciated.

I wouldn't discard using a known workaround for sure, can you point us to such discussion threads?