Skip to content

Commit c75c72b

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

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
@@ -2389,9 +2389,10 @@ visit_get_referrers(const mi_heap_t *heap, const mi_heap_area_t *area,
23892389
return true;
23902390
}
23912391
if (Py_TYPE(op)->tp_traverse(op, referrersvisit, arg->objs)) {
2392-
if (_PyObjectStack_Push(&arg->results, Py_NewRef(op)) < 0) {
2392+
if (_PyObjectStack_Push(&arg->results, op) < 0) {
23932393
return false;
23942394
}
2395+
Py_INCREF(op);
23952396
}
23962397

23972398
return true;
@@ -2436,9 +2437,10 @@ visit_get_objects(const mi_heap_t *heap, const mi_heap_area_t *area,
24362437
}
24372438

24382439
struct get_objects_args *arg = (struct get_objects_args *)args;
2439-
if (_PyObjectStack_Push(&arg->objects, Py_NewRef(op)) < 0) {
2440+
if (_PyObjectStack_Push(&arg->objects, op) < 0) {
24402441
return false;
24412442
}
2443+
Py_INCREF(op);
24422444
return true;
24432445
}
24442446

0 commit comments

Comments
 (0)