Skip to content

bpo-39877: PyGILState_Ensure() don't call PyEval_InitThreads() #18891

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 1 commit into from
Mar 9, 2020
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
1 change: 1 addition & 0 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extern PyObject *_PyEval_EvalCode(
PyObject *kwdefs, PyObject *closure,
PyObject *name, PyObject *qualname);

extern int _PyEval_ThreadsInitialized(_PyRuntimeState *runtime);
extern PyStatus _PyEval_InitThreads(PyThreadState *tstate);

#ifdef __cplusplus
Expand Down
8 changes: 7 additions & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,17 @@ ensure_tstate_not_null(const char *func, PyThreadState *tstate)
}


int
_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
{
return gil_created(&runtime->ceval.gil);
}

int
PyEval_ThreadsInitialized(void)
{
_PyRuntimeState *runtime = &_PyRuntime;
return gil_created(&runtime->ceval.gil);
return _PyEval_ThreadsInitialized(runtime);
}

PyStatus
Expand Down
32 changes: 13 additions & 19 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,27 +1280,28 @@ PyGILState_Check(void)
PyGILState_STATE
PyGILState_Ensure(void)
{
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
int current;
PyThreadState *tcur;
int need_init_threads = 0;
_PyRuntimeState *runtime = &_PyRuntime;
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;

/* 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! */
called Py_Initialize(). */

/* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been
called by Py_Initialize() */
assert(_PyEval_ThreadsInitialized(runtime));
assert(gilstate->autoInterpreterState);

tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
PyThreadState *tcur = (PyThreadState *)PyThread_tss_get(&gilstate->autoTSSkey);
int current;
if (tcur == NULL) {
need_init_threads = 1;

/* Create a new thread state for this thread */
/* Create a new Python thread state for this thread */
tcur = PyThreadState_New(gilstate->autoInterpreterState);
if (tcur == NULL)
if (tcur == NULL) {
Py_FatalError("Couldn't create thread-state for new thread");
}

/* This is our thread state! We'll need to delete it in the
matching call to PyGILState_Release(). */
tcur->gilstate_counter = 0;
Expand All @@ -1321,13 +1322,6 @@ PyGILState_Ensure(void)
*/
++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