bpo-36475: Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly#12667
bpo-36475: Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly#12667vstinner merged 7 commits into
Conversation
ncoghlan
left a comment
There was a problem hiding this comment.
LGTM, but I'd like Eric to take a look as well. In particular, I'm not sure if we have any existing test cases for this behaviour, and whether it might make sense to add one to the embedding tests.
Sorry, something went wrong.
|
@pablogsal @pitrou: Would you mind to review this change? It seems like you know the modified functions :-) |
Sorry, something went wrong.
|
I would recommend adding a note in the doc for these functions as we did in fde9b33 . Also, this code appears 3 times now in |
Sorry, something went wrong.
|
Hmm... this will exit any thread during finalization, not just daemon threads? I'm not sure why this is a good idea. |
Sorry, something went wrong.
|
I have made the requested changes; please review again. |
Sorry, something went wrong.
|
Thanks for making the requested changes! @ncoghlan: please review the changes made to this pull request. |
Sorry, something went wrong.
|
@pitrou Line 1150 in 0ef8c15 This PR just makes the lower level GIL acquisition APIs behave the same way PyEval_RestoreThread already does so that surviving daemon threads will exit rather than risking deadlocking based on exactly which API they call. This does highlight that the change is worth mentioning in the porting section of the Python 3.8 What's New, though, along with "versionchanged" notes on the affected APIs. |
Sorry, something went wrong.
|
I see. I'm always wary of the potential issues with such low-level changes, but you're right that we're already half way to the behaviour established by the PR, so I guess it's fine. |
Sorry, something went wrong.
pitrou
left a comment
There was a problem hiding this comment.
I guess it's ok on the principle. Please see comments below.
Sorry, something went wrong.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Sorry, something went wrong.
ncoghlan
left a comment
There was a problem hiding this comment.
The core functionality of the change still looks good to me, just some refactoring suggestions for the code, and a couple more documentation tweaks.
My proposed wording for the NEWS entry can be re-used as a bullet point in the What's New porting section, with an extra sentence on what to do about it:
PyEval_AcquireLockandPyEval_AcquireThreadnow terminate the current thread if called while the interpreter is finalizing, making them consistent withPyEval_RestoreThread,Py_END_ALLOW_THREADS, andPyGILState_Ensure. If this behaviour is not desired, guard the call by checking_Py_IsFinalizingorsys.is_finalizing.
Sorry, something went wrong.
fe8a3aa to
4f6b32d
Compare
April 25, 2019 21:49
|
I have made the requested changes; please review again |
Sorry, something went wrong.
|
I have made the requested changes; please review again |
Sorry, something went wrong.
|
I am very scared by this change touching critical parts of Python, but I'm somehow confident that it's the right thing to do. Antoine: Py_Finalize() explicitly waits until all non-daemon threads complete, so this change should impact "regular" threads: only daemon threads. Note: I have no idea of the impact on performances, but IMHO correctness matters more than performance here. If there is an overhead, I expect it to be not significant on macro benchmarks, and low on micro-benchmarks. -- Side note. I really hate the whole concept of daemon threads. Kill a thread anytime is just a very high risk of inconsistency. The thread can be killed while writing in the middle of a file, it can leave temporary files, etc. I would prefer to remove the concept of daemon threads and require developers to call pthread_kill() to make them understand what they are doing (something stupid IMHO :-)). Daemon threads make Python finalization very fragile, but the GIL + this PR make the finalization less fragile. Hint: search for "daemon" in Google Image. IMHO the name is well chosen. |
Sorry, something went wrong.
|
I am not comfortable to backport this change to Python 3.7. It's too early to know how it will impact applications and how many complains we will get :-) If someone really wants to backport this scary change to 3.7, I would suggest to wait for 1 month after Python 3.8.0 final release. |
Sorry, something went wrong.
I have added changes to finalize finalize
PyEval_AcquireLock()andPyEval_AcquireThread()properly.https://bugs.python.org/issue36475