Skip to content

BUG: fix refcount leak on overlapping copyto with where=False - #32114

Merged
charris merged 3 commits into
numpy:maintenance/2.5.xfrom
charris:backport-31968
Jul 27, 2026
Merged

BUG: fix refcount leak on overlapping copyto with where=False#32114
charris merged 3 commits into
numpy:maintenance/2.5.xfrom
charris:backport-31968

Conversation

@charris

@charris charris commented Jul 27, 2026

Copy link
Copy Markdown
Member

Backport of #31968.

PR summary

This PR fixes a reference leak in PyArray_AssignArray (used by np.copyto and related assign paths) when:

  1. src/dst overlap, so an intermediate temp is allocated (copied_src = 1), and
  2. a scalar boolean where=False triggers an early return that used to skip Py_DECREF of that temp.

Success and failure paths already released the temp; the early-return path did not. With object dtypes, filling the temp INCREFs every element, so those references (and the temp array) were leaked.

Backport of #31968.

Fix:
Py_DECREF(src) on the where=False early return when copied_src is set. No change to assignment semantics (where=False still writes nothing).

Test:
Regression test in test_api.py that checks object refcounts after overlapping np.copyto(..., where=False).

No release note (internal leak only; no user-visible API/behavior change).

First time committer introduction

I've been exploring the NumPy codebase recently, finding and fixing bugs. I had one PR merged last week and am continuing with small correctness fixes like this.

AI Disclosure

AI was used to help locate and draft the fix. The bug, fix, and tests were reviewed and validated by a human, and this PR was prepared and submitted by human.

NumPy Bugfix added 3 commits July 27, 2026 07:25
PyArray_AssignArray allocated an overlap temporary then early-returned
on scalar where=False without Py_DECREF of that temp. Free the temp on
that path and add a regression test.
Drop the direct getrefcount assertion in favor of a path-exercising
regression test that leak sanitizer builds can catch, matching the
style used in similar recent leak fixes.
The free on the early-return path is self-explanatory; restore the
prior comment wording per review.
@charris charris added this to the 2.5.2 release milestone Jul 27, 2026
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Jul 27, 2026
@charris
charris merged commit 4405d9b into numpy:maintenance/2.5.x Jul 27, 2026
85 checks passed
@charris
charris deleted the backport-31968 branch July 27, 2026 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant