[3.14] gh-109700: fix interpreter finalization while handling memory error (GH-136342) (#136352)

gh-109700: fix interpreter finalization while handling memory error (GH-136342)
(cherry picked from commit 0c3e3da195)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
Miss Islington (bot) 2025-07-07 09:40:13 +02:00 committed by GitHub
parent 3d01565da4
commit 22fad16ebc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1702,8 +1702,10 @@ finalize_modules(PyThreadState *tstate)
#endif
// Stop watching __builtin__ modifications
PyDict_Unwatch(0, interp->builtins);
if (PyDict_Unwatch(0, interp->builtins) < 0) {
// might happen if interp is cleared before watching the __builtin__
PyErr_Clear();
}
PyObject *modules = _PyImport_GetModules(interp);
if (modules == NULL) {
// Already done
@ -2377,15 +2379,13 @@ new_interpreter(PyThreadState **tstate_p,
error:
*tstate_p = NULL;
if (tstate != NULL) {
PyThreadState_Clear(tstate);
_PyThreadState_Detach(tstate);
PyThreadState_Delete(tstate);
Py_EndInterpreter(tstate);
} else {
PyInterpreterState_Delete(interp);
}
if (save_tstate != NULL) {
_PyThreadState_Attach(save_tstate);
}
PyInterpreterState_Delete(interp);
return status;
}