Skip to content

Commit 0c3e3da

Browse files
gh-109700: fix interpreter finalization while handling memory error (#136342)
1 parent 85b817d commit 0c3e3da

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
@@ -1702,8 +1702,10 @@ finalize_modules(PyThreadState *tstate)
17021702
#endif
17031703

17041704
// Stop watching __builtin__ modifications
1705-
PyDict_Unwatch(0, interp->builtins);
1706-
1705+
if (PyDict_Unwatch(0, interp->builtins) < 0) {
1706+
// might happen if interp is cleared before watching the __builtin__
1707+
PyErr_Clear();
1708+
}
17071709
PyObject *modules = _PyImport_GetModules(interp);
17081710
if (modules == NULL) {
17091711
// Already done
@@ -2377,15 +2379,13 @@ new_interpreter(PyThreadState **tstate_p,
23772379
error:
23782380
*tstate_p = NULL;
23792381
if (tstate != NULL) {
2380-
PyThreadState_Clear(tstate);
2381-
_PyThreadState_Detach(tstate);
2382-
PyThreadState_Delete(tstate);
2382+
Py_EndInterpreter(tstate);
2383+
} else {
2384+
PyInterpreterState_Delete(interp);
23832385
}
23842386
if (save_tstate != NULL) {
23852387
_PyThreadState_Attach(save_tstate);
23862388
}
2387-
PyInterpreterState_Delete(interp);
2388-
23892389
return status;
23902390
}
23912391

0 commit comments

Comments
 (0)