Skip to content

Commit 2362cb6

Browse files
[3.13] gh-109700: fix interpreter finalization while handling memory error (GH-136342) (#136353)
gh-109700: fix interpreter finalization while handling memory error (GH-136342) (cherry picked from commit 0c3e3da) Co-authored-by: Kumar Aditya <[email protected]>
1 parent bc3390c commit 2362cb6

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Python/pylifecycle.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,8 +1717,10 @@ finalize_modules(PyThreadState *tstate)
17171717
#endif
17181718

17191719
// Stop watching __builtin__ modifications
1720-
PyDict_Unwatch(0, interp->builtins);
1721-
1720+
if (PyDict_Unwatch(0, interp->builtins) < 0) {
1721+
// might happen if interp is cleared before watching the __builtin__
1722+
PyErr_Clear();
1723+
}
17221724
PyObject *modules = _PyImport_GetModules(interp);
17231725
if (modules == NULL) {
17241726
// Already done
@@ -2385,15 +2387,13 @@ new_interpreter(PyThreadState **tstate_p,
23852387
error:
23862388
*tstate_p = NULL;
23872389
if (tstate != NULL) {
2388-
PyThreadState_Clear(tstate);
2389-
_PyThreadState_Detach(tstate);
2390-
PyThreadState_Delete(tstate);
2390+
Py_EndInterpreter(tstate);
2391+
} else {
2392+
PyInterpreterState_Delete(interp);
23912393
}
23922394
if (save_tstate != NULL) {
23932395
_PyThreadState_Attach(save_tstate);
23942396
}
2395-
PyInterpreterState_Delete(interp);
2396-
23972397
return status;
23982398
}
23992399

0 commit comments

Comments
 (0)