Skip to content

BUG: Fix reference leakage for output arrays in reduction functions #29414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions numpy/_core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3727,6 +3727,8 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc,
if (ret == NULL) {
goto fail;
}

Py_XDECREF(out);

Py_DECREF(signature[0]);
Py_DECREF(signature[1]);
Expand All @@ -3753,6 +3755,8 @@ PyUFunc_GenericReduction(PyUFuncObject *ufunc,
return wrapped_result;

fail:
Py_XDECREF(out);

Py_XDECREF(signature[0]);
Py_XDECREF(signature[1]);
Py_XDECREF(signature[2]);
Expand Down
39 changes: 39 additions & 0 deletions numpy/_core/tests/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3004,6 +3004,45 @@ def test_reduce_casterrors(offset):
assert out[()] < value * offset


@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
def test_reduction_no_reference_leak():
# Test that the generic reduction does not leak references.
# gh-29358
arr = np.array([1, 2, 3], dtype=np.int32)
count = sys.getrefcount(arr)

np.add.reduce(arr, dtype=np.int32, initial=0)
assert count == sys.getrefcount(arr)

np.add.accumulate(arr, dtype=np.int32)
assert count == sys.getrefcount(arr)

np.add.reduceat(arr, [0, 1], dtype=np.int32)
assert count == sys.getrefcount(arr)

# with `out=` the reference count is not changed
out = np.empty((), dtype=np.int32)
out_count = sys.getrefcount(out)

np.add.reduce(arr, dtype=np.int32, out=out, initial=0)
assert count == sys.getrefcount(arr)
assert out_count == sys.getrefcount(out)

out = np.empty(arr.shape, dtype=np.int32)
out_count = sys.getrefcount(out)

np.add.accumulate(arr, dtype=np.int32, out=out)
assert count == sys.getrefcount(arr)
assert out_count == sys.getrefcount(out)

out = np.empty((2,), dtype=np.int32)
out_count = sys.getrefcount(out)

np.add.reduceat(arr, [0, 1], dtype=np.int32, out=out)
assert count == sys.getrefcount(arr)
assert out_count == sys.getrefcount(out)


def test_object_reduce_cleanup_on_failure():
# Test cleanup, including of the initial value (manually provided or not)
with pytest.raises(TypeError):
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy