Skip to content

BUG: Fix a reference count leak in npy_find_descr_for_scalar. - #27666

Merged
seberg merged 1 commit into
numpy:mainfrom
hawkinsp:refleak
Oct 30, 2024
Merged

seberg merged 1 commit into
numpy:mainfrom
hawkinsp:refleak

Conversation

@hawkinsp

@hawkinsp hawkinsp commented Oct 29, 2024 •

Copy link
Copy Markdown
Contributor

The reference count for common->singleton is incremented twice, when it should only be incremented once.

This leak was found when running Google's tests with NumPy 2.1.2, and appears to be a new leak as of NumPy 2.1, probably introduced in 1cb4044.

In particular, this test:
https://github.com/protocolbuffers/protobuf/blob/6cb71402940c6645e49959dfc915f16f4d2e6c20/python/google/protobuf/internal/numpy/numpy_test.py runs in Py_DEBUG mode and verifies that the total reference count before and after various test cases is unchanged. The same test case has found other NumPy reference count leaks in the past and it may be sensible to add something similar to NumPy's own test suite.

@seberg

The reference count for common->singleton is incremented twice, when it
should only be incremented once.

This leak was found when running Google's tests with NumPy 2.1.2, and
appears to be a new leak as of NumPy 2.1, probably introduced in
numpy@1cb4044.

In particular, this test:
https://github.com/protocolbuffers/protobuf/blob/6cb71402940c6645e49959dfc915f16f4d2e6c20/python/google/protobuf/internal/numpy/numpy_test.py#L67
runs in Py_DEBUG mode and verifies that the total reference count before
and after various test cases is unchanged. The same test case has found
other NumPy reference count leaks in the past and it may be sensible to
add something similar to NumPy's own test suite.
@seberg
seberg merged commit 6d38a75 into numpy:main Oct 30, 2024
@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Oct 30, 2024
@seberg

seberg commented Oct 30, 2024

Copy link
Copy Markdown
Member

Thanks @hawkinsp. I don't mind adding tests, but basically running full leak checking is becoming more and more cumbersome unless pytest-leaks is updated or a similar machinery is added.
(And it is always ridiculously slow.)

I am sure our test suite covers all of this, but pytest requires magic to reliably run a test multiple times to see if it leaks.
In the past, I have often run full pytest-leaks, but I don't always get to it (2.1 was very quick after 2.0 for one). But mostly, pytest-leaks is outdated, and honestly, I don't even know if there is a fully working combination anymore (old pytest that pytest-leaks is compatible with).

So, adding test seems a bit "meh" to me. Making sure that running pytest-leaks or similar is quick and easy would be great (well not quick, it'll probably always run for at least a day... in the past it was never much faster than valgrind even).

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