Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Lib/test/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,31 @@ def test_hamt_gc_2(self):

self.assertIsNone(ref())

def test_hamt_gc_3(self):
# gh-154535: the iterators must be tracked by the GC, otherwise a
# cycle running through one is never collected and the HAMT it
# holds -- and everything in it -- leaks.
A = HashKey(100, 'A')

container = []
h = hamt()
h = h.set(A, container)

hi = h.items()
self.assertTrue(gc.is_tracked(hi))

# Close the cycle: hi -> h -> container -> hi.
container.append(hi)
ref = weakref.ref(h)

del h, hi, container

gc.collect()
gc.collect()
gc.collect()

self.assertIsNone(ref())

def test_hamt_in_1(self):
A = HashKey(100, 'A')
AA = HashKey(100, 'A')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Track the internal HAMT iterators, which back iteration over a
:class:`contextvars.Context`, with the garbage collector. A reference cycle
running through such an iterator was never collected, leaking the whole
context it iterated over.
9 changes: 9 additions & 0 deletions Python/hamt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,10 @@ hamt_baseiter_tp_clear(PyObject *op)
{
PyHamtIterator *it = (PyHamtIterator*)op;
Py_CLEAR(it->hi_obj);
/* i_nodes holds borrowed pointers into the tree that hi_obj was keeping
alive, so the cursor must not be used again. A negative i_level makes
hamt_iterator_next() report I_END without touching i_nodes. */
it->hi_iter.i_level = -1;
return 0;
}

Expand Down Expand Up @@ -2496,6 +2500,10 @@ static Py_ssize_t
hamt_baseiter_tp_len(PyObject *op)
{
PyHamtIterator *it = (PyHamtIterator*)op;
if (it->hi_obj == NULL) {
/* tp_clear() ran on this iterator. */
return 0;
}
return it->hi_obj->h_count;
}

Expand All @@ -2516,6 +2524,7 @@ hamt_baseiter_new(PyTypeObject *type, binaryfunc yield, PyHamtObject *o)

hamt_iterator_init(&it->hi_iter, o->h_root);

PyObject_GC_Track(it);
return (PyObject*)it;
}

Expand Down
Loading