Skip to content

bpo-20891: Fix PyGILState_Ensure() #4650

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.

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
merged 2 commits into from
Nov 30, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ The following functions can be safely called before Python is initialized:

The following functions **should not be called** before
:c:func:`Py_Initialize`: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`,
:c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix` and
:c:func:`Py_GetProgramFullPath` and :c:func:`Py_GetPythonHome`.
:c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`,
:c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome` and
:c:func:`PyEval_InitThreads`.


.. _global-conf-vars:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ def test_pre_initialization_api(self):
self.assertEqual(out, '')
self.assertEqual(err, '')

def test_bpo20891(self):
"""
bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
call PyEval_InitThreads() for us in this case.
"""
out, err = self.run_embedded_interpreter("bpo20891")
self.assertEqual(out, '')
self.assertEqual(err, '')


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix PyGILState_Ensure(). When PyGILState_Ensure() is called in a non-Python
thread before PyEval_InitThreads(), only call PyEval_InitThreads() after
calling PyThreadState_New() to fix a crash.
49 changes: 49 additions & 0 deletions Programs/_testembed.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Python.h>
#include "pythread.h"
#include <inttypes.h>
#include <stdio.h>

Expand Down Expand Up @@ -147,6 +148,53 @@ static int test_pre_initialization_api(void)
return 0;
}

static void bpo20891_thread(void *lockp)
{
PyThread_type_lock lock = *((PyThread_type_lock*)lockp);

PyGILState_STATE state = PyGILState_Ensure();
if (!PyGILState_Check()) {
fprintf(stderr, "PyGILState_Check failed!");
abort();
}

PyGILState_Release(state);

PyThread_release_lock(lock);

PyThread_exit_thread();
}

static int test_bpo20891(void)
{
/* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
call PyEval_InitThreads() for us in this case. */
PyThread_type_lock lock = PyThread_allocate_lock();
if (!lock) {
fprintf(stderr, "PyThread_allocate_lock failed!");
return 1;
}

_testembed_Py_Initialize();

unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
if (thrd == PYTHREAD_INVALID_THREAD_ID) {
fprintf(stderr, "PyThread_start_new_thread failed!");
return 1;
}
PyThread_acquire_lock(lock, WAIT_LOCK);

Py_BEGIN_ALLOW_THREADS
/* wait until the thread exit */
PyThread_acquire_lock(lock, WAIT_LOCK);
Py_END_ALLOW_THREADS

PyThread_free_lock(lock);

return 0;
}


/* *********************************************************
* List of test cases and the function that implements it.
Expand All @@ -170,6 +218,7 @@ static struct TestCase TestCases[] = {
{ "forced_io_encoding", test_forced_io_encoding },
{ "repeated_init_and_subinterpreters", test_repeated_init_and_subinterpreters },
{ "pre_initialization_api", test_pre_initialization_api },
{ "bpo20891", test_bpo20891 },
{ NULL, NULL }
};

Expand Down
24 changes: 18 additions & 6 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -922,19 +922,19 @@ PyGILState_Ensure(void)
{
int current;
PyThreadState *tcur;
int need_init_threads = 0;

/* Note that we do not auto-init Python here - apart from
potential races with 2 threads auto-initializing, pep-311
spells out other issues. Embedders are expected to have
called Py_Initialize() and usually PyEval_InitThreads().
*/
/* Py_Initialize() hasn't been called! */
assert(_PyRuntime.gilstate.autoInterpreterState);

tcur = (PyThreadState *)PyThread_tss_get(&_PyRuntime.gilstate.autoTSSkey);
if (tcur == NULL) {
/* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
called from a new thread for the first time, we need the create the
GIL. */
PyEval_InitThreads();
need_init_threads = 1;

/* Create a new thread state for this thread */
tcur = PyThreadState_New(_PyRuntime.gilstate.autoInterpreterState);
Expand All @@ -945,16 +945,28 @@ PyGILState_Ensure(void)
tcur->gilstate_counter = 0;
current = 0; /* new thread state is never current */
}
else
else {
current = PyThreadState_IsCurrent(tcur);
if (current == 0)
}

if (current == 0) {
PyEval_RestoreThread(tcur);
}

/* Update our counter in the thread-state - no need for locks:
- tcur will remain valid as we hold the GIL.
- the counter is safe as we are the only thread "allowed"
to modify this value
*/
++tcur->gilstate_counter;

if (need_init_threads) {
/* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
called from a new thread for the first time, we need the create the
GIL. */
PyEval_InitThreads();
}

return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
}

Expand Down