Use threading instead of thread. Resolve _DummyThread bug. #333
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reference notes:-
http://stackoverflow.com/questions/13193278/understand-python-threading-bug
The cause of the bug is best narrowed in comments by Richard Oudkerk and cooyeah. What happens is the following:
The threading API has a feature that you can call threading.currentThread() even from a thread not created by the threading API. What you get back is an instance of a "dummy thread", which supports a very limited subset of the Thread API, but is still useful for identifying the current thread.
threading._DummyThread is implemented as a subclass of Thread. Thread instances normally contain an internal callable (self.__block) that keeps reference to an OS-level lock allocated for the instance. Since the Thread methods that would end up calling self.__block are overridden by _DummyThread to not do that, _DummyThread's constructor releases the OS lock by deleting self.__block.
Because of a bug in threading._after_fork, the private __stop method gets called on all the registered threads, including the dummy threads. The correct patch is to change threading._after_fork to no longer do that, which is what the final patch in the issue does. (IMHO it would be even better to change _DummyThread to inherit from a thread base rather than from Thread, but that change would be backward-incompatible.)