-
Notifications
You must be signed in to change notification settings - Fork 320
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
Monkey patching interferes with threading in Python 3.7 #592
Comments
Oh, and the |
* Update nova from branch 'master' - Merge "Monkey patch original current_thread _active" - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I4872169413f27aeaff8d8fdfa5cdaf6ee32f4680 Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I4872169413f27aeaff8d8fdfa5cdaf6ee32f4680 Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I95a8d8cf02a0cb923418c0b5655442b8d7bc6b08 Closes-Bug: #1863021
* Update glance from branch 'master' - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I95a8d8cf02a0cb923418c0b5655442b8d7bc6b08 Closes-Bug: #1863021
@tipabu do you think this issue could be addressed in eventlet? Frankly forking with running greenthreads is terrifying (or even just after starting the hub at ALL). Maybe if we could define some sort of sane way to fork? Like maybe kill all running greenthreads abandoning them to the parent with the option to bootstrap a new hub in the child? I'm sure the threading module assert is doing exactly what it intends to and the running greenthreads are worth blowing up over. But without a solid use-case I'm not sure how to evaluate what the behavior SHOULD be. |
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Icc2277b72f6f8f7812be22c43bbc281334aa2373 Closes-Bug: #1863021
* Update oslo.service from branch 'master' - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Icc2277b72f6f8f7812be22c43bbc281334aa2373 Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I8e8a9ce5085b7915994317af5ea903cf8a39e809 Closes-Bug: #1863021
* Update neutron-dynamic-routing from branch 'master' - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I8e8a9ce5085b7915994317af5ea903cf8a39e809 Closes-Bug: #1863021
* Update neutron from branch 'master' - Merge "Monkey patch original current_thread _active" - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I49bfd9673abc7602b27dc48b8b490daaded2882c Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I49bfd9673abc7602b27dc48b8b490daaded2882c Closes-Bug: #1863021
* Update cinder from branch 'master' - Merge "Monkey patch original current_thread _active" - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Ida548b4bec00530418fd3d7ab254e971af77d3fe Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Ida548b4bec00530418fd3d7ab254e971af77d3fe Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I0e8f80bb2e7acfb00e5ff47e0aa983afb7ee6e38 Closes-Bug: #1863021
* Update murano-agent from branch 'master' - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I0e8f80bb2e7acfb00e5ff47e0aa983afb7ee6e38 Closes-Bug: #1863021
So I think all of these suggestions are definitely implementable in eventlet -- I even took a stab at addressing the 3x Buuuut... running the existing tests, I see two new errors in
...which seems sensible enough: if you're going to monkey-patch threading to use greenthreads, it makes sense that But even without that, I see
which also doesn't seem great -- it sure feels to me like the code at https://github.com/eventlet/eventlet/blob/v0.25.1/tests/patcher_test.py#L320-L327 ought to print Does that mean that the subprocess is sharing Maybe the better thing to do would be to embrace
shows some promise... I'll try to play with it some more... |
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Also disable E402 to allow the change in. Change-Id: I508fcd0707ecdd2bf720303f6cbb4087a38aaadd Closes-Bug: #1863021
* Update murano from branch 'master' - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Also disable E402 to allow the change in. Change-Id: I508fcd0707ecdd2bf720303f6cbb4087a38aaadd Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I49bfd9673abc7602b27dc48b8b490daaded2882c Closes-Bug: #1863021 (cherry picked from commit ec7a5aa)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Ide125add76d42615c8d828870a2def997225090e Story: 2007614
* Update magnum from branch 'master' - Merge "Monkey patch original current_thread _active" - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Ide125add76d42615c8d828870a2def997225090e Story: 2007614
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I93ce7f11dcd8c1f01616580b65e5e581b775e8c5 Story: 2007614
* Update sahara from branch 'master' - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I93ce7f11dcd8c1f01616580b65e5e581b775e8c5 Story: 2007614
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I97c2a756076299a01170beb5bec3fa0e49593146 Story: 2007614
* Update ironic from branch 'master' - Merge "Monkey patch original current_thread _active" - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I97c2a756076299a01170beb5bec3fa0e49593146 Story: 2007614
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I93ce7f11dcd8c1f01616580b65e5e581b775e8c5 Story: 2007614 (cherry picked from commit 9c83e69)
* Update cyborg from branch 'master' - Merge "Monkey patch original current_thread _active" - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Closes-Bug: #1863021 Change-Id: I3ae99e3cae6fdfe430040ca7b4eb531ea4c22b18
Recent changes [1] introduced an eventlet fix to monkey patch original current_thread _active. The goal was to monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 I think we need this patch on unit test too to ensure a consistent behavior, so these changes doing that. [1] https://review.opendev.org/#/c/725359/ Change-Id: I7b6cca86e44261bf2f953be74e9738ac09507649
* Update oslo.service from branch 'master' - Merge "Align tests with monkey patch original current_thread _active" - Align tests with monkey patch original current_thread _active Recent changes [1] introduced an eventlet fix to monkey patch original current_thread _active. The goal was to monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 I think we need this patch on unit test too to ensure a consistent behavior, so these changes doing that. [1] https://review.opendev.org/#/c/725359/ Change-Id: I7b6cca86e44261bf2f953be74e9738ac09507649
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Icb26c43a71accdb4658ad6087fb3226f9a1090c0 Story: 2007614
* Update os-ken from branch 'master' - Merge "Monkey patch original current_thread _active" - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Icb26c43a71accdb4658ad6087fb3226f9a1090c0 Story: 2007614
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I61e5e270bf66b0355da3282c19cbc9fd42c4090b Closes-Bug: #1863021
* Update trove from branch 'master' - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I61e5e270bf66b0355da3282c19cbc9fd42c4090b Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I0540923755ac3969b584eeba2e19c037a7f2c261 Story: 2007614 (cherry picked from commit 8fd1721)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I5c2fbe1827195d670a67dd62cdadce5ee3513ace Closes-Bug: #1863021 (cherry picked from commit 666fd70)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Ifc6420d927c0ce9e04ff3b3253e81a474591e9bb Closes-Bug: #1863021 (cherry picked from commit 5e9f694) (cherry picked from commit 7196cfc)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I5ea4bb27361df3c489deedb51d7ca8ea64bb923b Closes-Bug: #1863021 (cherry picked from commit f0db421)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I8e8a9ce5085b7915994317af5ea903cf8a39e809 Closes-Bug: #1863021 (cherry picked from commit 44e77ea)
Previously, if we patched threading then forked (or, in some cases, used the subprocess module), Python would log an ignored exception like Exception ignored in: <function _after_fork at 0x7f16493489d8> Traceback (most recent call last): File "/usr/lib/python3.7/threading.py", line 1335, in _after_fork assert len(_active) == 1 AssertionError: This comes down to threading in Python 3.7+ having an import side-effect of registering an at-fork callback. When we re-import threading to patch it, the old (but still registered) callback still points to the old thread-tracking dict, rather than the new dict that's actually doing the tracking. Now, register our own at_fork hook that will fix up the dict reference before threading's _at_fork runs and put it back afterwards. Closes eventlet#592
* Update masakari from branch 'master' - Merge "Monkey patch original current_thread _active" - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I4929bfd4dc60f97b27459a4c6e7ed649c5e7f645 Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I4929bfd4dc60f97b27459a4c6e7ed649c5e7f645 Closes-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I0a2c1e0d8a8cad99d68100d25e88e0d3a2eb8f5c Related-Bug: #1863021
* Update oslo.concurrency from branch 'master' - Monkey patch original current_thread _active in processutils Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I0a2c1e0d8a8cad99d68100d25e88e0d3a2eb8f5c Related-Bug: #1863021
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I0a2c1e0d8a8cad99d68100d25e88e0d3a2eb8f5c Related-Bug: #1863021 (cherry picked from commit 6533958)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I0a2c1e0d8a8cad99d68100d25e88e0d3a2eb8f5c Related-Bug: #1863021 (cherry picked from commit 6533958)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I5b00ee328f83cec8375ad1538be3a16059af08a3 Closes-Bug: #1863021 (cherry picked from commit 0b78dad)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I0a2c1e0d8a8cad99d68100d25e88e0d3a2eb8f5c Related-Bug: #1863021 (cherry picked from commit 6533958)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I5ea4bb27361df3c489deedb51d7ca8ea64bb923b Closes-Bug: #1863021 (cherry picked from commit f0db421)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I5b00ee328f83cec8375ad1538be3a16059af08a3 Closes-Bug: #1863021 (cherry picked from commit 0b78dad)
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I35335325828c10f7a0a1c97edfd1a842dda77577 Story: 2007614
* Update barbican from branch 'master' to 59aa7f71b9c34170648236b52e00fb7eaf3737bd - Merge "Monkey patch original current_thread _active" - Monkey patch original current_thread _active Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: I35335325828c10f7a0a1c97edfd1a842dda77577 Story: 2007614
Monkey patch the original current_thread to use the up-to-date _active global variable. This solution is based on that documented at: eventlet/eventlet#592 Change-Id: Ifc6420d927c0ce9e04ff3b3253e81a474591e9bb Closes-Bug: #1863021
Environment
Steps to reproduce the error
Cause of the error
Eventlet replaces
threading.current_thread
witheventlet.green.threading.current_thread
, which falls back to the originalcurrent_thread
in some cases. The originalcurrent_thread
is operating with the original_active
global variable. The original_active
global variable is not in use, as the entirethreading
module has been re-imported, so it contains outdated values. As a result,threading._after_fork
is unable to find a thread inthreading._active
with the sameid
asthreading.current_thread()
.Workaround
This can be fixed by monkey patching the original
current_thread
to use the up-to-date_active
global variable, as in:Special case
That workaround doesn't solve every case. Consider what happens when a greenthread forks:
Cause of the error for special case
Eventlet's implementation of
current_thread
uses its own thread-localactive
variable to track greenthreads.threading._after_fork
doesn't know about that, so it gets its list of threads fromthreading._active
viathreading._enumerate
, and its "current thread" fromactive
viaeventlet.green.threading.current_thread
. This mismatch again causes the assertion error.Workaround for the special case
I'm not sure why eventlet is co-opting the
threading
library for greenthreads, since it seems that breaks any code that assumes it's working with real threads -- but anways, this particular breakage can be fixed by monkey patchingthreading._after_fork
to simply not useeventlet.green.threading.current_thread
. The purpose ofthreading._after_fork
is to clean up left-over thread state in the child process after a fork, so it really doesn't care about greenthreads.This workaround also requires removing
_after_fork
from__patched__
. I don't know why this is necessary, but I also don't know why it's there in the first place (the commit that added it offers no explanation).Yet another issue...
With the second workaround, there are no assertion errors. However, there is still some undesirable behavior due to eventlet's monkey patching. Since Python 3.7's
threading
library introduced a new import side-effect (... unfortunately), and eventlet re-importsthreading
twice in the process of monkey patching it, the side-effect is run 3 times. The side-effect in this case isos.register_at_fork(after_in_child=_after_fork)
, which registers_after_fork
as an after fork hook. That means it's registered 3 times. So, 3 different versions of_after_fork
are run after every fork.Unfortunately, the Python documentation states that it's impossible to unregister a function, meaning the
threading
library now has an unrevertable import side-effect (... which is fun). Thus, I don't know of any simple workaround. I also currently don't know of any symptoms caused by the triple-registering issue.The text was updated successfully, but these errors were encountered: