https://github.com/python/cpython/commit/8fad07c0f1d6c84ca70ce13d543f99a60a0d4a28
commit: 8fad07c0f1d6c84ca70ce13d543f99a60a0d4a28
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-21T12:22:28-07:00
summary:

[3.15] gh-159739: Clear free-threaded GC worklists after early errors 
(GH-159979) (#160016)


(cherry picked from commit 2315d94664f0a1c93ef43272b9c200ad4b04c709)

Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-10-20-00-00-00.gh-issue-159739.da0f04.rst
M Python/gc_free_threading.c

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 00000000000000..386eb5535b7025
--- /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 abbfc1aef08056..36b4fada23d6b8 100644
--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -2113,6 +2113,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;
         }
@@ -2123,6 +2124,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;
     }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to