Skip to content

GH-50591: [Python] Fix reference in ConvertToSequenceAndInferSize when an iterator raises on array conversion - #50594

Merged
raulcd merged 1 commit into
apache:mainfrom
raulcd:GH-50591
Jul 22, 2026
Merged

GH-50591: [Python] Fix reference in ConvertToSequenceAndInferSize when an iterator raises on array conversion#50594
raulcd merged 1 commit into
apache:mainfrom
raulcd:GH-50591

Conversation

@raulcd

@raulcd raulcd commented Jul 22, 2026

Copy link
Copy Markdown
Member

Rationale for this change

We currently leak the list and items in the case of an iterator raising an Exception and size is present.

What changes are included in this PR?

Use OwnedRef instead of relying on manual Py_DECREF in order to avoid exception scenario returning early in RETURN_IF_PYERROR.

Are these changes tested?

Yes, I've created a new test that fails previously to the fix and passes after the fix

Are there any user-facing changes?

No

…ze when an iterator raises on array conversion
@github-actions github-actions Bot added the awaiting committer review Awaiting committer review label Jul 22, 2026
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #50591 has been automatically assigned in GitHub to PR creator.

@raulcd

raulcd commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g python

@github-actions

Copy link
Copy Markdown

Revision: afc8c10

Submitted crossbow builds: ursacomputing/crossbow @ actions-1dcc69b782

Task Status
example-python-minimal-build-fedora-conda GitHub Actions
example-python-minimal-build-ubuntu-venv GitHub Actions
test-conda-python-3.11 GitHub Actions
test-conda-python-3.11-dask-latest GitHub Actions
test-conda-python-3.11-dask-upstream_devel GitHub Actions
test-conda-python-3.11-hdfs-2.9.2 GitHub Actions
test-conda-python-3.11-hdfs-3.2.1 GitHub Actions
test-conda-python-3.11-hypothesis GitHub Actions
test-conda-python-3.11-pandas-2.0.3-numpy-1.23.2 GitHub Actions
test-conda-python-3.11-spark-master GitHub Actions
test-conda-python-3.12 GitHub Actions
test-conda-python-3.12-pandas-latest-numpy-latest GitHub Actions
test-conda-python-3.13 GitHub Actions
test-conda-python-3.13-pandas-latest-numpy-1.26.2 GitHub Actions
test-conda-python-3.13-pandas-latest-numpy-latest GitHub Actions
test-conda-python-3.14 GitHub Actions
test-conda-python-3.14-cpython-debug GitHub Actions
test-conda-python-3.14-pandas-nightly-numpy-nightly GitHub Actions
test-conda-python-3.14-pandas-upstream_devel-numpy-nightly GitHub Actions
test-conda-python-emscripten GitHub Actions
test-debian-13-python-3-amd64 GitHub Actions
test-debian-13-python-3-i386 GitHub Actions
test-fedora-42-python-3 GitHub Actions
test-ubuntu-24.04-python-3 GitHub Actions

@raulcd
raulcd marked this pull request as ready for review July 22, 2026 09:40
@raulcd
raulcd requested a review from pitrou as a code owner July 22, 2026 09:40
Copilot AI review requested due to automatic review settings July 22, 2026 09:40
@raulcd
raulcd requested review from AlenkaF and rok as code owners July 22, 2026 09:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a Python reference leak in ConvertToSequenceAndInferSize when converting a sized iterator that raises during iteration (GH-50591). The change improves exception-safety in the C++/Python bridge by using RAII (OwnedRef) so early returns on Python exceptions don’t skip required decrefs.

Changes:

  • Wrap the intermediate PyList_New(n) result in an OwnedRef and detach() it only on success to avoid leaking the list/items on exception paths.
  • Remove manual Py_DECREF(lst) in the slice-shrinking error path since RAII now handles cleanup.
  • Add a regression test that asserts refcounts don’t change when an iterator raises during pa.array(..., size=...).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/pyarrow/src/arrow/python/python_to_arrow.cc Make sized-iterator conversion exception-safe by managing the temporary list with OwnedRef and detaching only on success.
python/pyarrow/tests/test_convert_builtin.py Add a regression test ensuring no reference leak when a raising iterator is converted with size specified.

@raulcd

raulcd commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

@pitrou I think this is another reference leak

Comment on lines 1266 to 1267
// either an error occurred or the iterator ended
RETURN_IF_PYERROR();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw the leak was here

@github-actions github-actions Bot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Jul 22, 2026
@raulcd
raulcd merged commit 826ac37 into apache:main Jul 22, 2026
40 checks passed
@raulcd raulcd removed the awaiting changes Awaiting changes label Jul 22, 2026
@raulcd
raulcd deleted the GH-50591 branch July 22, 2026 11:37
@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit 826ac37.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants