Gregory P. Smith <g...@krypto.org> added the comment:

**If** we add a callback API for this, I agree with vstinner's 
https://bugs.python.org/issue42969#msg402558 commentary on the other callback 
APIs.  We can do this one similarly and keep it simple. - Why? It's the initial 
simplicity that provides those running into this problem a way to solve their 
problem today.  Without getting hung up on the details of the new default 
behavior.

**That said** the only conclusion I'm coming to for what the safest behavior of 
Python is in this situation is to hang the threads, effectively as PR 28525 is 
proposing.

So even if we added the callback API, I'd expect code using Python with C++ 
where these issues might ever crop up to always register a thread hanging 
callback.  Which really suggests it is the good default.  So do we even need 
the callback?

...But lets put the default behavior choice aside for a moment, there's 
something valuable regardless...

There are **New C APIs to enhance PyGILState_Ensure** proposed in PR 28525 to 
do with GIL acquisition from threads.  These appear useful as is.  They provide 
a way for threads to discover that they will never be able to get the GIL and 
re-enter the Python interpreter.  Rather than today's unexpected behavior of 
PyGILState_Ensure mercilessly terminating their thread, or given a Callback API 
whatever effect such a Callback API would have.  That allows code to be written 
with pre-problem-detection that avoids entering this point of no return state.  
That is good for everyone.  **We should add those "GIL-curious" APIs no matter 
what.**  This bit could become its own PR separate from the other parts.

If we cannot agree that blocking a non-main daemon-or-C/C++ thread forever that 
is guaranteed to not acquire the GIL because the Python interpreter is going 
away is the right default behavior instead of blindly killing it under the 
unsupportable assumption that it has nothing to clean up: We can proceed with 
such a callback API. I'm having a hard time imagining any other behavior that 
makes sense, so I'd expect lots of Python extension interface code to start 
carrying a copy of an implementation of a hang callback and sporting a 
Py_SetThreadExitCallback(xxx) call in their module Init function. (especially 
if pybind11 finds needs to generate this as boilerplate)

I think the pthread_exit() call added in issue1856's 
https://hg.python.org/cpython/rev/c892b0321d23 were well intentioned, but not 
aware of the full unsupportable ramifications of that API call. That the 
attempt at releasing a backport of the pthread_exit call to 2.7.8 broke an 
application (that was admittedly doing something unwise) and had to be reverted 
as 2.7.9 was a sign for us to revisit this in 3.x releases as well.  Which 
we're finally doing here.

----------
nosy: +pitrou

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42969>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to