Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use threading instead of thread. Resolve _DummyThread bug. #333

Merged
merged 3 commits into from May 27, 2013

Conversation

calvinchengx
Copy link
Contributor

  1. Upgrade middleware to using threading module instead of thread module and
  2. resolve python threading module bug - Exception AttributeError: AttributeError("_DummyThread object has no attribute _Thread__block",) in <module threading from

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

…module. Fix this threading related bug - Exception AttributeError: AttributeError("_DummyThread object has no attribute _Thread__block",) in <module threading from
…currentThread().ident, which is an integer, and not the threading.currentThread() instance itself.
@calvinchengx
Copy link
Contributor Author

Original commit 9b1fbf2 broke the unit test. My bad.

Our middleware functions are expecting the actual identity of the thread instance as an integer and not the thread instance itself. Fixed as f3d5bb7.

@calvinchengx
Copy link
Contributor Author

Full explanation detailing the origins of this problem here - http://bit.ly/WtFfOa

jezdez added a commit that referenced this pull request May 27, 2013
Use threading instead of thread. Resolve _DummyThread bug.
@jezdez jezdez merged commit 25ea0b4 into jazzband:master May 27, 2013
ryneeverett pushed a commit to ryneeverett/django-debug-toolbar that referenced this pull request Oct 2, 2016
Use threading instead of thread. Resolve _DummyThread bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants