[3.12] gh-116510: Fix a Crash Due to Shared Immortal Interned Strings (gh-125205)

Fix a crash caused by immortal interned strings being shared between
sub-interpreters that use basic single-phase init. In that case, the string
can be used by an interpreter that outlives the interpreter that created and
interned it. For interpreters that share obmalloc state, also share the
interned dict with the main interpreter.

This is an un-revert of gh-124646 that then addresses the Py_TRACE_REFS
failures identified by gh-124785 (i.e. backporting gh-125709 too).

(cherry picked from commit f2cb399470, AKA gh-124865)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-12-03 18:26:25 +01:00 committed by GitHub
parent b49e902b81
commit 49da170709
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 124 additions and 20 deletions

View file

@ -650,6 +650,10 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
return status;
}
// This could be done in init_interpreter() (in pystate.c) if it
// didn't depend on interp->feature_flags being set already.
_PyObject_InitState(interp);
PyThreadState *tstate = _PyThreadState_New(interp);
if (tstate == NULL) {
return _PyStatus_ERR("can't make first thread");
@ -2103,6 +2107,10 @@ new_interpreter(PyThreadState **tstate_p, const PyInterpreterConfig *config)
goto error;
}
// This could be done in init_interpreter() (in pystate.c) if it
// didn't depend on interp->feature_flags being set already.
_PyObject_InitState(interp);
status = init_interp_create_gil(tstate, config->gil);
if (_PyStatus_EXCEPTION(status)) {
goto error;

View file

@ -686,7 +686,9 @@ init_interpreter(PyInterpreterState *interp,
_obmalloc_pools_INIT(interp->obmalloc.pools);
memcpy(&interp->obmalloc.pools.used, temp, sizeof(temp));
}
_PyObject_InitState(interp);
// We would call _PyObject_InitState() at this point
// if interp->feature_flags were alredy set.
_PyEval_InitState(interp, pending_lock);
_PyGC_InitState(&interp->gc);