From 4a8445dd02f59282ba05adaa71a8316d37538e5e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Tue, 21 Mar 2023 12:47:55 -0600 Subject: [PATCH] gh-98608: Fix Failure-handling in new_interpreter() (gh-102658) The error-handling code in new_interpreter() has been broken for a while. We hadn't noticed because those code mostly doesn't fail. (I noticed while working on gh-101660.) The problem is that we try to clear/delete the newly-created thread/interpreter using itself, which just failed. The solution is to switch back to the calling thread state first. (cherry picked from commit d1b883b52a99427d234c20e4a92ddfa6a1da8880) Co-authored-by: Eric Snow https: //github.com/python/cpython/issues/98608 --- Python/pylifecycle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index eeaf20b4617a20..b0c5da56c4fff9 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1962,10 +1962,10 @@ new_interpreter(PyThreadState **tstate_p, int isolated_subinterpreter) /* Oops, it didn't work. Undo it all. */ PyErr_PrintEx(0); + PyThreadState_Swap(save_tstate); PyThreadState_Clear(tstate); PyThreadState_Delete(tstate); PyInterpreterState_Delete(interp); - PyThreadState_Swap(save_tstate); return status; }