BUG: fix memory leak in np.zeros when fill-zero loop raises#31320
Merged
Conversation
seberg
approved these changes
Apr 25, 2026
Member
seberg
left a comment
There was a problem hiding this comment.
Thanks a lot, looks good to me! (Will hold off merging a moment.)
I guess adding a test that fails during the zero filling is annoying, but your DType tested this (maybe accidentally, so?).
(Our CI now has leak sanitizer running even, but we have no dtype that can be made to fail unless we write one just for this...)
|
|
||
| fail: | ||
| NPY_traverse_info_xfree(&fill_zero_info); | ||
| Py_XDECREF(fa->mem_handler); |
Member
There was a problem hiding this comment.
Hah, "fun" that the old logic worked out here because the owndata flag wasn't set yet and mem_handler was never deallocated.
Member
|
OK, let's put this in, thanks again. |
charris
pushed a commit
that referenced
this pull request
Apr 28, 2026
BUG: fix memory leak in np.zeros when fill-zero loop raises
charris
added a commit
that referenced
this pull request
Apr 28, 2026
BUG: fix memory leak in np.zeros when fill-zero loop raises (#31320)
MaanasArora
pushed a commit
to MaanasArora/numpy
that referenced
this pull request
May 7, 2026
BUG: fix memory leak in np.zeros when fill-zero loop raises
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 memory leak in np.zeros that occurs when the fill-zero loop raises an exception (which can happen for custom user-defined dtypes). It does this by setting NPY_ARRAY_OWNDATA earlier to ensure the data will be deallocated, combined with changes to mem_handler cleanup to avoid a double-decref.
I'm not entirely sure whether this is the most appropriate way to fix the leak, but it does seem to work correctly based on testing with
tracemalloc.AI Disclosure
The bug was detected through testing with
tracemalloc, then located and fixed by Claude Opus 4.7.Full AI-generated description: https://gist.github.com/MaartenBaert/f3eadd280f84ef6896586025f59788ec