Skip to content

[Python] ConvertToSequenceAndInferSize leaks a reference to a list and the items when iterator raises an Exception #50591

Closed

Description

@raulcd

Describe the bug, including details regarding any error messages, version, and platform.

While reviewing:

I started investigating and I found that we could leak a reference and the items contained on the list when converting an iterator that raises an Exception at ConvertToSequenceAndInferSize.

I created a test that currently fails:

def test_failing_iterator_does_not_leak():
    # ConvertToSequenceAndInferSize builds an intermediate list.  If the
    # iterator raises, that list and everything already stored in it leaks.
    import gc

    # Create an arbitrary long int that is probably not cached by the interpreter
    value = 10**20

    def raising_iter():
        for _ in range(5):
            yield value
        raise ValueError("boom")

    gc.collect()
    original_refcount = sys.getrefcount(value)

    with pytest.raises(ValueError, match="boom"):
        pa.array(raising_iter(), size=100)

    gc.collect()
    assert sys.getrefcount(value) == original_refcount

The issue is in RETURN_IF_PYERROR(); error case where lst is not Py_DECREF:

PyObject* lst = PyList_New(n);
RETURN_IF_PYERROR();
for (i = 0; i < n; i++) {
PyObject* item = PyIter_Next(iter);
if (!item) {
// either an error occurred or the iterator ended
RETURN_IF_PYERROR();
break;
}
PyList_SET_ITEM(lst, i, item);
}
// Shrink list if len(iterator) < size
if (i < n && PyList_SetSlice(lst, i, n, NULL)) {
Py_DECREF(lst);
RETURN_IF_PYERROR();
}
*seq = lst;

The failing iterator case was detected here:

Component(s)

Python

Activity

  1. changed the title [-][Python] ConvertToSequenceAndInferSize leaks a reference when iterator raises an Exception[/-] [+][Python] ConvertToSequenceAndInferSize leaks a reference to a list and the items when iterator raises an Exception[/+] on Jul 22, 2026
  2. added a commit that references this issue on Jul 22, 2026
  3. added this to the 26.0.0 milestone on Jul 22, 2026
  4. raulcd commented on Jul 22, 2026

    @raulcd
    MemberAuthor

    Issue resolved by pull request 50594
    #50594

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions