pywinusb icon indicating copy to clipboard operation
pywinusb copied to clipboard

Solved issue of pywinusb Input threads not closing or terminating.

Open warrenarea opened this issue 5 years ago • 1 comments

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())

warrenarea avatar Feb 07 '19 07:02 warrenarea

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?

rene-aguirre avatar Feb 08 '19 04:02 rene-aguirre