Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1080,9 +1080,27 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
*tstate*, which should not be *NULL*. The lock must have been created earlier.
If this thread already has the lock, deadlock ensues.

.. note::
Calling this function from a thread when the runtime is finalizing
will terminate the thread, even if the thread was not created by Python.
You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to
check if the interpreter is in process of being finalized before calling
this function to avoid unwanted termination.

.. versionchanged:: 3.8
Updated to be consistent with :c:func:`PyEval_RestoreThread`,
:c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`,
and terminate the current thread if called while the interpreter is finalizing.

:c:func:`PyEval_RestoreThread` is a higher-level function which is always
available (even when threads have not been initialized).

.. note::

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ended up with two copies of this note.

Calling this function from a thread when the runtime is finalizing
will terminate the thread, even if the thread was not created by Python.
You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vstinner Are you aware of any particular reason for _Py_IsFinalizing being a nominally private API? If we're recommending that folks call it in the documentation, that's a pretty strong sign that it should really be public.

(That can be broken out to a separate issue, though - it doesn't need to be resolved here)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. Feel free to open an issue to propose to make it public if you want.

In my PR #12934, I modified the internal _Py_CURRENTLY_FINALIZING() macro to add a runtime parameter :-) (PR currently under review.)

check if the interpreter is in process of being finalized before calling
this function to avoid unwanted termination.

Comment thread
nanjekyejoannah marked this conversation as resolved.
Outdated
.. c:function:: void PyEval_ReleaseThread(PyThreadState *tstate)

Expand All @@ -1106,6 +1124,18 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
:c:func:`PyEval_RestoreThread` or :c:func:`PyEval_AcquireThread`
instead.

.. note::
Calling this function from a thread when the runtime is finalizing
will terminate the thread, even if the thread was not created by Python.
You can use :c:func:`_Py_IsFinalizing` or :func:`sys.is_finalizing` to
check if the interpreter is in process of being finalized before calling
this function to avoid unwanted termination.

.. versionchanged:: 3.8
Updated to be consistent with :c:func:`PyEval_RestoreThread`,
:c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`,
and terminate the current thread if called while the interpreter is finalizing.


.. c:function:: void PyEval_ReleaseLock()

Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,13 @@ Changes in Python behavior
always use the ``sys.platform.startswith('aix')``.
(Contributed by M. Felt in :issue:`36588`.)

* :c:func:`PyEval_AcquireLock` and :c:func:`PyEval_AcquireThread` now
terminate the current thread if called while the interpreter is
finalizing, making them consistent with :c:func:`PyEval_RestoreThread`,
:c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`. If this
behaviour is not desired, guard the call by checking :c:func:`_Py_IsFinalizing`
or :c:func:`sys.is_finalizing`.

Changes in the Python API
-------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:c:func:`PyEval_AcquireLock` and :c:func:`PyEval_AcquireThread` now
terminate the current thread if called while the interpreter is
finalizing, making them consistent with :c:func:`PyEval_RestoreThread`,
:c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`.
28 changes: 16 additions & 12 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static PyObject * special_lookup(PyObject *, _Py_Identifier *);
static int check_args_iterable(PyObject *func, PyObject *vararg);
static void format_kwargs_error(PyObject *func, PyObject *kwargs);
static void format_awaitable_error(PyTypeObject *, int);
static inline void exit_thread_if_finalizing(PyThreadState *);

#define NAME_ERROR_MSG \
"name '%.200s' is not defined"
Expand Down Expand Up @@ -210,6 +211,7 @@ PyEval_AcquireLock(void)
if (tstate == NULL)
Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
take_gil(tstate);
exit_thread_if_finalizing(tstate);
}

void
Expand All @@ -230,6 +232,7 @@ PyEval_AcquireThread(PyThreadState *tstate)
/* Check someone has called PyEval_InitThreads() to create the lock */
assert(gil_created());
take_gil(tstate);
exit_thread_if_finalizing(tstate);
if (PyThreadState_Swap(tstate) != NULL)
Py_FatalError(
"PyEval_AcquireThread: non-NULL old thread state");
Expand Down Expand Up @@ -298,12 +301,7 @@ PyEval_RestoreThread(PyThreadState *tstate)

int err = errno;
take_gil(tstate);
/* _Py_Finalizing is protected by the GIL */
if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
drop_gil(tstate);
PyThread_exit_thread();
Py_UNREACHABLE();
}
exit_thread_if_finalizing(tstate);
errno = err;

PyThreadState_Swap(tstate);
Expand Down Expand Up @@ -1083,12 +1081,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
take_gil(tstate);

/* Check if we should make a quick exit. */
if (_Py_IsFinalizing() &&
!_Py_CURRENTLY_FINALIZING(tstate))
{
drop_gil(tstate);
PyThread_exit_thread();
}
exit_thread_if_finalizing(tstate);

if (PyThreadState_Swap(tstate) != NULL)
Py_FatalError("ceval: orphan tstate");
Expand Down Expand Up @@ -5199,6 +5192,17 @@ format_awaitable_error(PyTypeObject *type, int prevopcode)
}
}

static inline void
exit_thread_if_finalizing(PyThreadState *tstate)
Comment thread
ncoghlan marked this conversation as resolved.
Outdated
{
/* _Py_Finalizing is protected by the GIL */
if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
drop_gil(tstate);
PyThread_exit_thread();
Py_UNREACHABLE();
}
}

static PyObject *
unicode_concatenate(PyObject *v, PyObject *w,
PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Expand Down