BUG: fix refcount leak on overlapping copyto with where=False#31968
Open
Ethan0456 wants to merge 3 commits into
Open
BUG: fix refcount leak on overlapping copyto with where=False#31968Ethan0456 wants to merge 3 commits into
copyto with where=False#31968Ethan0456 wants to merge 3 commits into
Conversation
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.
ikrommyd
suggested changes
Jul 12, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
This PR fixes a reference leak in
PyArray_AssignArray(used bynp.copytoand related assign paths) when:src/dstoverlap, so an intermediate temp is allocated (copied_src = 1), andwhere=Falsetriggers an early return that used to skipPy_DECREFof 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.Fix:
Py_DECREF(src)on thewhere=Falseearly return whencopied_srcis set. No change to assignment semantics (where=Falsestill writes nothing).Test:
Regression test in
test_api.pythat checks object refcounts after overlappingnp.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.