-
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
threading.current_thread does not work as expected for plain greenlets #731
Comments
Right, It could be possible to bend current_thread to raw greenlets, but there may be dragons as well. I'd most likely merge any relevant work that doesn't hurt performance; sorry, don't have time to actually code it. So if |
Thank you very much for the reply, this info will definitely help me find a way forward. Much appreciated! |
Apologies that I forgot to mention this earlier ... but the scenario I was most interested in is when After thinking about it, I realized that is the problem I'm running into. |
@melwitt why no, |
It does ... I'm not sure whether what I'm about to say will make sense, but what I'm referring to is the fact that when called from the main greenlet, I did some testing and found that the issues do indeed disappear when I replace all Hope that makes sense. Either way, thank you for the helpful guidance. |
@melwitt I understand the inconsistency, it's a shame. I don't have a fast solution though. Sorry. This would be a very weird workaround crutch: import eventlet
eventlet.spawn_n = eventlet.spawn
eventlet.convenient.spawn_n = eventlet.spawn
eventlet.monkey_patch() wouldn't recommend running it in production with random other libraries set, which could depend on spawn_n specifics, unlikely. |
It's somewhat redundant to call |
Thank you @temoto, that all makes sense. Meanwhile, I found something else interesting. I tried a local test where I replaced all use of [1] https://github.com/eventlet/eventlet/blob/v0.32.0/eventlet/green/thread.py#L72 |
The fasteners lib in version 0.15.0 removed the threading.current_thread workaround for eventlet[1] because eventlet seemed to fixed the current_thread issues tracked in [2]. However the fix for [2] only fixed half of the problem. The threading.current_thread call works if it is called from thread created by eventlet.spawn. However if the thread is created with eventlet.spawn_n then threading.current_thread is still broken and returns the ID of the python native thread. The fasteners' ReaderWriterLock depends heavily on threading.current_thread to decide which thread holds a lock and to allow re-entry of that thread. This leads to the situation that multiple threads created from spawn_n could take the same ReaderWriterLock at the same time. The fair internal lock in oslo.concurrency uses ReaderWriterLock and as a result such lock is broken for threads created with spawn_n. Note that this issue was raised with eventlet in [3] when the nova team detected it via a direct usage of ReaderWriterLock in the nova test code. As [3] did not lead to a solution in eventlet nova implemented a nova local fix for the test code in [4]. However now we detected that oslo.concurrency is affected by this issue as well. This patch adds tests to show the scope of the problem. Note that the coverage tox target is changed to explicitly enable native threading otherwise it runs eventlet specific tests in a native environment. Also note that [5] was opened to reintroduce the workaround[1] in fasteners. [1] harlowja/fasteners@467ed75 [2] eventlet/eventlet#172 [3] eventlet/eventlet#731 [4] https://review.opendev.org/c/openstack/nova/+/813114 [5] harlowja/fasteners#96 Related-Bug: #1988311 Change-Id: Ibc193c855b49b95b46ebd2aac82ea89e33f885f0
* Update oslo.concurrency from branch 'master' to 796203c94846d44237d869e3b20a6cd8a3602e36 - Prove that spawn_n with fair lock is broken The fasteners lib in version 0.15.0 removed the threading.current_thread workaround for eventlet[1] because eventlet seemed to fixed the current_thread issues tracked in [2]. However the fix for [2] only fixed half of the problem. The threading.current_thread call works if it is called from thread created by eventlet.spawn. However if the thread is created with eventlet.spawn_n then threading.current_thread is still broken and returns the ID of the python native thread. The fasteners' ReaderWriterLock depends heavily on threading.current_thread to decide which thread holds a lock and to allow re-entry of that thread. This leads to the situation that multiple threads created from spawn_n could take the same ReaderWriterLock at the same time. The fair internal lock in oslo.concurrency uses ReaderWriterLock and as a result such lock is broken for threads created with spawn_n. Note that this issue was raised with eventlet in [3] when the nova team detected it via a direct usage of ReaderWriterLock in the nova test code. As [3] did not lead to a solution in eventlet nova implemented a nova local fix for the test code in [4]. However now we detected that oslo.concurrency is affected by this issue as well. This patch adds tests to show the scope of the problem. Note that the coverage tox target is changed to explicitly enable native threading otherwise it runs eventlet specific tests in a native environment. Also note that [5] was opened to reintroduce the workaround[1] in fasteners. [1] harlowja/fasteners@467ed75 [2] eventlet/eventlet#172 [3] eventlet/eventlet#731 [4] https://review.opendev.org/c/openstack/nova/+/813114 [5] harlowja/fasteners#96 Related-Bug: #1988311 Change-Id: Ibc193c855b49b95b46ebd2aac82ea89e33f885f0
* Update oslo.concurrency from branch 'master' to ee3f73a13379a79282325906787aae7da0f6ac27 - Fix fair internal lock used from eventlet.spawn_n The fasteners lib in version 0.15.0 removed the threading.current_thread workaround for eventlet[1] because eventlet seemed to fixed the current_thread issues tracked in [2]. However the fix for [2] only fixed half of the problem. The threading.current_thread call works if it is called from thread created by eventlet.spawn. However if the thread is created with eventlet.spawn_n then threading.current_thread is still broken and returns the ID of the python native thread. The fasteners' ReaderWriterLock depends heavily on threading.current_thread to decide which thread holds a lock and to allow re-entry of that thread. This leads to the situation that multiple threads created from spawn_n could take the same ReaderWriterLock at the same time. The fair internal lock in oslo.concurrency uses ReaderWriterLock and as a result such lock is broken for threads created with spawn_n. Note that this issue was raised with eventlet in [3] when the nova team detected it via a direct usage of ReaderWriterLock in the nova test code. As [3] did not lead to a solution in eventlet nova implemented a nova local fix for the test code in [4]. However now we detected that oslo.concurrency is affected by this issue as well. This patch restores the workaround that was removed by [1]. Note that a fasteners issue [5] also opened to restore the workaround[1]. [1] harlowja/fasteners@467ed75 [2] eventlet/eventlet#172 [3] eventlet/eventlet#731 [4] https://review.opendev.org/c/openstack/nova/+/813114 [5] harlowja/fasteners#96 Closes-Bug: #1988311 Change-Id: Ia873bcc6b07121c9bd0b94c593567d537b4c1112
The fasteners lib in version 0.15.0 removed the threading.current_thread workaround for eventlet[1] because eventlet seemed to fixed the current_thread issues tracked in [2]. However the fix for [2] only fixed half of the problem. The threading.current_thread call works if it is called from thread created by eventlet.spawn. However if the thread is created with eventlet.spawn_n then threading.current_thread is still broken and returns the ID of the python native thread. The fasteners' ReaderWriterLock depends heavily on threading.current_thread to decide which thread holds a lock and to allow re-entry of that thread. This leads to the situation that multiple threads created from spawn_n could take the same ReaderWriterLock at the same time. The fair internal lock in oslo.concurrency uses ReaderWriterLock and as a result such lock is broken for threads created with spawn_n. Note that this issue was raised with eventlet in [3] when the nova team detected it via a direct usage of ReaderWriterLock in the nova test code. As [3] did not lead to a solution in eventlet nova implemented a nova local fix for the test code in [4]. However now we detected that oslo.concurrency is affected by this issue as well. This patch restores the workaround that was removed by [1]. Note that a fasteners issue [5] also opened to restore the workaround[1]. [1] harlowja/fasteners@467ed75 [2] eventlet/eventlet#172 [3] eventlet/eventlet#731 [4] https://review.opendev.org/c/openstack/nova/+/813114 [5] harlowja/fasteners#96 Closes-Bug: #1988311 Change-Id: Ia873bcc6b07121c9bd0b94c593567d537b4c1112
I'm running into an issue while using locks from the fasteners library [1][2] in an eventlet monkey patched application.
The fasteners library uses the
threading.current_thread
method to track which thread is holding a particular lock [3] and it works well for green threads of type GreenThread that are created by callingspawn
[4]. However, the application I'm working with creates greenlets by callingspawn_n
[5] and in this casethreading.current_thread
falls back to the standardthreading.current_thread
method [6] instead of callingeventlet.getcurrent
.Is this expected that the eventlet
threading.current_thread
method cannot be used with plain greenlets created byspawn_n
?Here is an attempt at sample code demonstrating the above:
Output:
Any input would be appreciated.
[1] https://fasteners.readthedocs.io/en/latest
[2] https://pypi.org/project/fasteners
[3] https://github.com/harlowja/fasteners/blob/e1764c7/fasteners/lock.py#L119
[4] https://eventlet.net/doc/modules/greenthread.html#eventlet.greenthread.spawn
[5] https://eventlet.net/doc/modules/greenthread.html#eventlet.greenthread.spawn_n
[6] https://github.com/eventlet/eventlet/blob/8904a33/eventlet/green/threading.py#L125-L128
The text was updated successfully, but these errors were encountered: