GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-36475: Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly by nanjekyejoannah · Pull Request #12667 · python/cpython

bpo-36475: Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly #12667

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.

Sign up for GitHub

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

Merged
vstinner merged 7 commits into python:master from nanjekyejoannah:issue36475
Apr 29, 2019

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-36475: Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly #12667

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
4f6b32d
Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly
nanjekyejoannah Apr 25, 2019
2c02944
Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly
nanjekyejoannah Apr 25, 2019
b8a1690
space fix
nanjekyejoannah Apr 25, 2019
f2337be
Remove new line.
nanjekyejoannah Apr 25, 2019
5daac23
Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly
nanjekyejoannah Apr 26, 2019
d14cedb
Merge branch 'issue36475' of https://github.com/nanjekyejoannah/cpyth…
nanjekyejoannah Apr 26, 2019
4e1d820
Finalize PyEval_AcquireLock() and PyEval_AcquireThread() properly
nanjekyejoannah Apr 26, 2019
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

24 changes: 24 additions & 0 deletions 24 Doc/c-api/init.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,18 @@ 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).

Expand All @@ -1106,6 +1118,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 7 Doc/whatsnew/3.8.rst
Show comments
View file
Open in desktop
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
4 changes: 4 additions & 0 deletions 4 Misc/NEWS.d/next/Core and Builtins/2019-04-02-20-02-22.bpo-36475.CjRps3.rst
Show comments
View file
Open in desktop
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 28 Python/ceval.c
Show comments
View file
Open in desktop
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 @@ -203,13 +204,25 @@ _PyEval_FiniThreads(void)
}
}

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

Uh oh!

There was an error while loading. Please reload this page.

}
}

void
PyEval_AcquireLock(void)
{
PyThreadState *tstate = _PyThreadState_GET();
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 +243,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 +312,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 +1092,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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.