Skip to content

Commit 3ef1c81

Browse files
miss-islingtonjelle-openaiJelleZijlstra
authored
[3.14] gh-159739: Avoid leaking references during free-threaded GC enumeration (GH-159781) (#159908)
(cherry picked from commit 7652a6d) Co-authored-by: Jelle Zijlstra <jelle@openai.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 55e40b6 commit 3ef1c81

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a reference leak in :func:`gc.get_objects` and :func:`gc.get_referrers`
2+
when an internal allocation fails in the free-threaded build.

‎Python/gc_free_threading.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,9 +2479,10 @@ visit_get_referrers(const mi_heap_t *heap, const mi_heap_area_t *area,
24792479
return true;
24802480
}
24812481
if (Py_TYPE(op)->tp_traverse(op, referrersvisit, arg->objs)) {
2482-
if (_PyObjectStack_Push(&arg->results, Py_NewRef(op)) < 0) {
2482+
if (_PyObjectStack_Push(&arg->results, op) < 0) {
24832483
return false;
24842484
}
2485+
Py_INCREF(op);
24852486
}
24862487

24872488
return true;
@@ -2526,9 +2527,10 @@ visit_get_objects(const mi_heap_t *heap, const mi_heap_area_t *area,
25262527
}
25272528

25282529
struct get_objects_args *arg = (struct get_objects_args *)args;
2529-
if (_PyObjectStack_Push(&arg->objects, Py_NewRef(op)) < 0) {
2530+
if (_PyObjectStack_Push(&arg->objects, op) < 0) {
25302531
return false;
25312532
}
2533+
Py_INCREF(op);
25322534
return true;
25332535
}
25342536

0 commit comments

Comments
 (0)