bpo-40010: COMPUTE_EVAL_BREAKER() checks for subinterpreter (GH-19087)

COMPUTE_EVAL_BREAKER() now also checks if the Python thread state
belongs to the main interpreter. Don't break the evaluation loop if
there are pending signals but the Python thread state it belongs to a
subinterpeter.

* Add _Py_IsMainThread() function.
* Add _Py_ThreadCanHandleSignals() function.
This commit is contained in:
Victor Stinner 2020-03-20 13:38:58 +01:00 committed by GitHub
parent da2914db4b
commit d2a8e5b42c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 53 deletions

View file

@ -53,12 +53,12 @@ This has consequences:
Signals and threads
^^^^^^^^^^^^^^^^^^^
Python signal handlers are always executed in the main Python thread,
Python signal handlers are always executed in the main Python thread of the main interpreter,
even if the signal was received in another thread. This means that signals
can't be used as a means of inter-thread communication. You can use
the synchronization primitives from the :mod:`threading` module instead.
Besides, only the main thread is allowed to set a new signal handler.
Besides, only the main thread of the main interpreter is allowed to set a new signal handler.
Module contents
@ -266,7 +266,7 @@ The :mod:`signal` module defines the following functions:
same process as the caller. The target thread can be executing any code
(Python or not). However, if the target thread is executing the Python
interpreter, the Python signal handlers will be :ref:`executed by the main
thread <signals-and-threads>`. Therefore, the only point of sending a
thread of the main interpreter <signals-and-threads>`. Therefore, the only point of sending a
signal to a particular Python thread would be to force a running system call
to fail with :exc:`InterruptedError`.
@ -360,7 +360,8 @@ The :mod:`signal` module defines the following functions:
If not -1, *fd* must be non-blocking. It is up to the library to remove
any bytes from *fd* before calling poll or select again.
When threads are enabled, this function can only be called from the main thread;
When threads are enabled, this function can only be called
from :ref:`the main thread of the main interpreter <signals-and-threads>`;
attempting to call it from other threads will cause a :exc:`ValueError`
exception to be raised.
@ -413,7 +414,8 @@ The :mod:`signal` module defines the following functions:
signal handler will be returned (see the description of :func:`getsignal`
above). (See the Unix man page :manpage:`signal(2)` for further information.)
When threads are enabled, this function can only be called from the main thread;
When threads are enabled, this function can only be called
from :ref:`the main thread of the main interpreter <signals-and-threads>`;
attempting to call it from other threads will cause a :exc:`ValueError`
exception to be raised.