Skip to content

BUG: fix memory leak in np.zeros when fill-zero loop raises#31320

Merged
seberg merged 2 commits into
numpy:mainfrom
MaartenBaert:main
Apr 27, 2026
Merged

BUG: fix memory leak in np.zeros when fill-zero loop raises#31320
seberg merged 2 commits into
numpy:mainfrom
MaartenBaert:main

Conversation

@MaartenBaert
Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

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...)

Comment thread numpy/_core/src/multiarray/arrayobject.c

fail:
NPY_traverse_info_xfree(&fill_zero_info);
Py_XDECREF(fa->mem_handler);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hah, "fun" that the old logic worked out here because the owndata flag wasn't set yet and mem_handler was never deallocated.

@charris charris added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported labels Apr 25, 2026
@seberg
Copy link
Copy Markdown
Member

seberg commented Apr 27, 2026

OK, let's put this in, thanks again.

@seberg seberg merged commit bab58ec into numpy:main Apr 27, 2026
95 of 97 checks passed
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Apr 28, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants