diff options
author | Sam Gross <colesbury@gmail.com> | 2024-03-29 13:33:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-29 13:33:04 -0400 |
commit | f05fb2e65c2dffdfae940f2707765c4994925205 (patch) | |
tree | d856903be5f6ab281900fbc2a68ccf7c861ce802 /Python/gc_free_threading.c | |
parent | ddf95b5f16031cdbd0d728e55eb06dff002a8678 (diff) | |
download | cpython-f05fb2e65c2dffdfae940f2707765c4994925205.tar.gz cpython-f05fb2e65c2dffdfae940f2707765c4994925205.zip |
gh-112529: Don't untrack tuples or dicts with zero refcount (#117370)
The free-threaded GC sometimes sees objects with zero refcount. This can
happen due to the delay in merging biased reference counting fields,
and, in the future, due to deferred reference counting. We should not
untrack these objects or they will never be collected.
This fixes the refleaks in the free-threaded build.
Diffstat (limited to 'Python/gc_free_threading.c')
-rw-r--r-- | Python/gc_free_threading.c | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index 69ce22a1e83..4524382e4f6 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -374,25 +374,28 @@ update_refs(const mi_heap_t *heap, const mi_heap_area_t *area, return true; } - // Untrack tuples and dicts as necessary in this pass. - if (PyTuple_CheckExact(op)) { - _PyTuple_MaybeUntrack(op); - if (!_PyObject_GC_IS_TRACKED(op)) { - gc_restore_refs(op); - return true; + Py_ssize_t refcount = Py_REFCNT(op); + _PyObject_ASSERT(op, refcount >= 0); + + if (refcount > 0) { + // Untrack tuples and dicts as necessary in this pass, but not objects + // with zero refcount, which we will want to collect. + if (PyTuple_CheckExact(op)) { + _PyTuple_MaybeUntrack(op); + if (!_PyObject_GC_IS_TRACKED(op)) { + gc_restore_refs(op); + return true; + } } - } - else if (PyDict_CheckExact(op)) { - _PyDict_MaybeUntrack(op); - if (!_PyObject_GC_IS_TRACKED(op)) { - gc_restore_refs(op); - return true; + else if (PyDict_CheckExact(op)) { + _PyDict_MaybeUntrack(op); + if (!_PyObject_GC_IS_TRACKED(op)) { + gc_restore_refs(op); + return true; + } } } - Py_ssize_t refcount = Py_REFCNT(op); - _PyObject_ASSERT(op, refcount >= 0); - // We repurpose ob_tid to compute "gc_refs", the number of external // references to the object (i.e., from outside the GC heaps). This means // that ob_tid is no longer a valid thread id until it is restored by |