From 6a0e86bebc72c66e9da4925bb3ec85e9600a8795 Mon Sep 17 00:00:00 2001 From: "zainnadeem(RedOpsCell)" Date: Thu, 25 Jun 2026 12:37:37 +0500 Subject: [PATCH 1/3] gh-151763: Fix free-threaded OOM cleanup for initial thread state --- Python/pystate.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/pystate.c b/Python/pystate.c index fed1df0173bacf1..e0030bf136e05a9 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1505,6 +1505,7 @@ alloc_threadstate(PyInterpreterState *interp) } reset_threadstate(tstate); } + tstate->base.interp = interp; return tstate; } From 7a0289193bb7043c0b3eb1e316178a99b3ae8a3c Mon Sep 17 00:00:00 2001 From: "zainnadeem(RedOpsCell)" Date: Thu, 25 Jun 2026 21:37:37 +0500 Subject: [PATCH 2/3] Move interpreter pointer initialization to new_threadstate --- Python/pystate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/pystate.c b/Python/pystate.c index e0030bf136e05a9..b56ddd93d662de7 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1505,7 +1505,6 @@ alloc_threadstate(PyInterpreterState *interp) } reset_threadstate(tstate); } - tstate->base.interp = interp; return tstate; } @@ -1662,6 +1661,7 @@ new_threadstate(PyInterpreterState *interp, int whence) if (tstate == NULL) { return NULL; } + tstate->base.interp = interp; #ifdef Py_GIL_DISABLED Py_ssize_t qsbr_idx = _Py_qsbr_reserve(interp); From 9d47a7b82d22aec8db46e7ab014c1cf8968bab29 Mon Sep 17 00:00:00 2001 From: "zainnadeem(RedOpsCell)" Date: Fri, 26 Jun 2026 00:11:10 +0500 Subject: [PATCH 3/3] Add comment and NEWS for thread state cleanup fix --- .../2026-06-26-00-06-29.gh-issue-151763.pX4fYq.rst | 2 ++ Python/pystate.c | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-00-06-29.gh-issue-151763.pX4fYq.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-00-06-29.gh-issue-151763.pX4fYq.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-00-06-29.gh-issue-151763.pX4fYq.rst new file mode 100644 index 000000000000000..95bbc9562f1027a --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-26-00-06-29.gh-issue-151763.pX4fYq.rst @@ -0,0 +1,2 @@ +Fixed a possible crash in free-threaded builds when interpreter creation fails +due to memory allocation failure. diff --git a/Python/pystate.c b/Python/pystate.c index b56ddd93d662de7..9986fbde62affb6 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1661,6 +1661,8 @@ new_threadstate(PyInterpreterState *interp, int whence) if (tstate == NULL) { return NULL; } + // Set the back-pointer before init_threadstate() so early cleanup can + // recognize the preallocated initial thread state after allocation failures. tstate->base.interp = interp; #ifdef Py_GIL_DISABLED