]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-109700: fix interpreter finalization while handling memory error (#136342)
authorKumar Aditya <kumaraditya@python.org>
Mon, 7 Jul 2025 07:15:22 +0000 (12:45 +0530)
committerGitHub <noreply@github.com>
Mon, 7 Jul 2025 07:15:22 +0000 (12:45 +0530)
Python/pylifecycle.c

index 724fda6351128299cedab539b4199e33d325b356..00e8d030765560aa17a52154480ad9ca23be5d0c 100644 (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;
 }