https://github.com/python/cpython/commit/c75c72b64ac4cdb58e81b4b4cf434129fdcab684
commit: c75c72b64ac4cdb58e81b4b4cf434129fdcab684
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-20T18:37:02Z
summary:

[3.15] gh-159739: Avoid leaking references during free-threaded GC enumeration 
(GH-159781) (#159907)



(cherry picked from commit 7652a6daccc39d7fb051d54d9f804df535bb17b0)

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

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.55fd08.rst
M Python/gc_free_threading.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.55fd08.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.55fd08.rst
new file mode 100644
index 00000000000000..e574bab861b740
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-19-20-38-17.gh-issue-159739.55fd08.rst
@@ -0,0 +1,2 @@
+Fix a reference leak in :func:`gc.get_objects` and :func:`gc.get_referrers`
+when an internal allocation fails in the free-threaded build.
diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c
index dc31906fecc5d4..abbfc1aef08056 100644
--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -2389,9 +2389,10 @@ visit_get_referrers(const mi_heap_t *heap, const 
mi_heap_area_t *area,
         return true;
     }
     if (Py_TYPE(op)->tp_traverse(op, referrersvisit, arg->objs)) {
-        if (_PyObjectStack_Push(&arg->results, Py_NewRef(op)) < 0) {
+        if (_PyObjectStack_Push(&arg->results, op) < 0) {
             return false;
         }
+        Py_INCREF(op);
     }
 
     return true;
@@ -2436,9 +2437,10 @@ visit_get_objects(const mi_heap_t *heap, const 
mi_heap_area_t *area,
     }
 
     struct get_objects_args *arg = (struct get_objects_args *)args;
-    if (_PyObjectStack_Push(&arg->objects, Py_NewRef(op)) < 0) {
+    if (_PyObjectStack_Push(&arg->objects, op) < 0) {
         return false;
     }
+    Py_INCREF(op);
     return true;
 }
 

_______________________________________________
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