From ff3c7ccc8f6db3b03f9f3c13871a5c61a67bec07 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 20 Oct 2026 21:32:19 -0700 Subject: [PATCH] gh-159739: Clear free-threaded GC worklists after early errors (GH-159979) Backport to 3.15 and 3.14. (cherry picked from commit 2315d94664f0a1c93ef43272b9c200ad4b04c709) Co-authored-by: Jelle Zijlstra --- .../2026-10-20-00-00-00.gh-issue-159739.da0f04.rst | 2 ++ Python/gc_free_threading.c | 4 ++++ 2 files changed, 6 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.da0f04.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.da0f04.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.da0f04.rst new file mode 100644 index 000000000000000..386eb5535b70252 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.da0f04.rst @@ -0,0 +1,2 @@ +Fix reference leaks when free-threaded garbage collection aborts early because +of an allocation error. diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index 032d543f702d1c9..61634590959516b 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -2229,6 +2229,7 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state, int err = gc_mark_alive_from_roots(interp, state); if (err < 0) { _PyEval_StartTheWorld(interp); + cleanup_worklist(&state->objs_to_decref); PyErr_NoMemory(); return; } @@ -2239,6 +2240,9 @@ gc_collect_internal(PyInterpreterState *interp, struct collection_state *state, int err = deduce_unreachable_heap(interp, state); if (err < 0) { _PyEval_StartTheWorld(interp); + cleanup_worklist(&state->unreachable); + cleanup_worklist(&state->legacy_finalizers); + cleanup_worklist(&state->objs_to_decref); PyErr_NoMemory(); return; }