bpo-40226: PyInterpreterState_Delete() deletes pending calls (GH-19436)

PyInterpreterState_New() is now responsible to create pending calls,
PyInterpreterState_Delete() now deletes pending calls.

* Rename _PyEval_InitThreads() to _PyEval_InitGIL() and rename
  _PyEval_InitGIL() to _PyEval_FiniGIL().
* _PyEval_InitState() and PyEval_FiniState() now create and delete
  pending calls. _PyEval_InitState() now returns -1 on memory
  allocation failure.
* Add init_interp_create_gil() helper function: code shared by
  Py_NewInterpreter() and Py_InitializeFromConfig().
* init_interp_create_gil() now also calls _PyEval_FiniGIL(),
  _PyEval_InitGIL() and _PyGILState_Init() in subinterpreters, but
  these functions now do nothing when called from a subinterpreter.
This commit is contained in:
Victor Stinner 2020-04-08 17:54:59 +02:00 committed by GitHub
parent ac2cfe6631
commit dda5d6e071
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 117 additions and 60 deletions

View file

@ -222,7 +222,10 @@ PyInterpreterState_New(void)
_PyRuntimeState *runtime = &_PyRuntime;
interp->runtime = runtime;
_PyEval_InitState(&interp->ceval);
if (_PyEval_InitState(&interp->ceval) < 0) {
goto out_of_memory;
}
_PyGC_InitState(&interp->gc);
PyConfig_InitPythonConfig(&interp->config);
@ -267,6 +270,14 @@ PyInterpreterState_New(void)
interp->audit_hooks = NULL;
return interp;
out_of_memory:
if (tstate != NULL) {
_PyErr_NoMemory(tstate);
}
PyMem_RawFree(interp);
return NULL;
}
@ -335,6 +346,8 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
struct pyinterpreters *interpreters = &runtime->interpreters;
zapthreads(interp, 0);
_PyEval_FiniState(&interp->ceval);
/* Delete current thread. After this, many C API calls become crashy. */
_PyThreadState_Swap(&runtime->gilstate, NULL);
@ -352,6 +365,7 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
Py_FatalError("remaining threads");
}
*p = interp->next;
if (interpreters->main == interp) {
interpreters->main = NULL;
if (interpreters->head != NULL) {
@ -359,6 +373,7 @@ PyInterpreterState_Delete(PyInterpreterState *interp)
}
}
HEAD_UNLOCK(runtime);
if (interp->id_mutex != NULL) {
PyThread_free_lock(interp->id_mutex);
}
@ -1198,6 +1213,12 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
PyStatus
_PyGILState_Init(PyThreadState *tstate)
{
if (!_Py_IsMainInterpreter(tstate)) {
/* Currently, PyGILState is shared by all interpreters. The main
* interpreter is responsible to initialize it. */
return _PyStatus_OK();
}
/* must init with valid states */
assert(tstate != NULL);
assert(tstate->interp != NULL);