GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: fix leak in reductions when a ufunc override errors or is not found by ikrommyd · Pull Request #31731 · numpy/numpy

Conversation

@ikrommyd

@ikrommyd ikrommyd commented Jun 23, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

PR summary

Closes #31677

Just another thing I noticed while reading reduction code and realised there's an open issue for it already.
One can see this memory leak by doing something like the following and memory profiling that (with memray for example) and will notice the memory is just growing.

import numpy as np

class Array:
    data = np.array([1,2,3])
    def __array_ufunc__(ufunc, method, *inputs, **kwargs):
        return NotImplemented

arr = Array()

if __name__ == "__main__":
    for _ in range(1_000_000):
        try:
            np.add.reduce(arr)
        except Exception as e:
            pass

I think goto fail is probably nicer than

Py_XDECREF(full_args.in);
Py_XDECREF(full_args.out);

and I believe it's safe. Only the else if (override) branch right bellow cannot use goto fail as it needs to return the override and not NULL.

I don't think there is a way to have a test regarding the ref count in the python side because full_args is created internally in the function in C. But we could have a test that is just the above without the loop and a sanitizer would probably capture the leak. Let me know if you want that. cc @seberg because you wanted a similar test in #31699.

First time committer introduction

N/A

AI Disclosure

No AI

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ikrommyd added 2 commits June 23, 2026 16:13
@ikrommyd
BUG: fix leak of full_args.in/out in reductions when ufunc overrides …
e478bdf
…fail

Signed-off-by: Iason Krommydas <[email protected]>
@ikrommyd
MNT: switch to goto fail
81e65e5
Signed-off-by: Iason Krommydas <[email protected]>
@jorenham jorenham changed the title Fix leak reductions override BUG: Fix leak reductions override Jun 23, 2026
@jorenham jorenham added the 00 - Bug label Jun 23, 2026
@ikrommyd ikrommyd changed the title BUG: Fix leak reductions override BUG: fix leak in reductions when a ufunc override errors or is not found Jun 23, 2026
@ikrommyd

ikrommyd commented Jun 23, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Just pasting the results of profiling the snippet above with memray (with native traces too) on main:
image
image
With this PR, it becomes flat:
image

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg

seberg commented Jun 24, 2026

Copy link
Copy Markdown
Member

Yeah, just out of principle. Seems like a simple test to add and I expect leak sanitizer or so will find it even without a loop.

ikrommyd reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ikrommyd
TST: add test
fab93db
Signed-off-by: Iason Krommydas <[email protected]>
@ikrommyd

ikrommyd commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

Yeah, just out of principle. Seems like a simple test to add and I expect leak sanitizer or so will find it even without a loop.

Done

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ikrommyd
MNT: use issue number in test comment
cd91949
Signed-off-by: Iason Krommydas <[email protected]>
@ikrommyd

ikrommyd commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

The BLAS tests failure should be unrelated

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg
seberg approved these changes Jun 24, 2026
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks, for adding the test, good enough, let's put it in.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/tests/test_umath.py
a = A()
assert_raises(TypeError, np.add.reduce, a)
assert_raises(TypeError, np.add.accumulate, a)
assert_raises(TypeError, np.add.reduceat, a, [0])

@seberg seberg Jun 24, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

N.B. I generally prefer with pytest.raises() even if it a lot of old tests use assert_raises.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ikrommyd ikrommyd Jun 24, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ah thanks. Noted for the future. I intuitively tend to follow existing patterns and I literally just copied it from the tests right above it

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@seberg
seberg merged commit 4d85561 into numpy:main Jun 24, 2026
94 of 96 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Jun 24, 2026
@ikrommyd
ikrommyd deleted the fix-leak-reductions-override branch June 24, 2026 12:59
@ikrommyd ikrommyd mentioned this pull request Jun 24, 2026
BUG: fix out array leak in reduceat and accumulate when dtype resolution fails #31699
Merged
@charris charris mentioned this pull request Jul 2, 2026
BUG: fix leak in reductions when a ufunc override errors or is not found (#31731) #31842
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 2, 2026
charris pushed a commit that referenced this pull request Jul 3, 2026
@ikrommyd @charris
BUG: fix leak in reductions when a ufunc override errors or is not fo…
22978d2
…und (#31731)

Signed-off-by: Iason Krommydas <[email protected]>
charris added a commit that referenced this pull request Jul 3, 2026
@charris
Merge pull request #31842 from charris/backport-31731
4cdf5e0
BUG: fix leak in reductions when a ufunc override errors or is not found (#31731)
@ikrommyd ikrommyd mentioned this pull request Jul 12, 2026
BUG: fix refcount leak on overlapping copyto with where=False #31968
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@seberg seberg seberg approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

BUG: Reference leak in PyUFunc_GenericReduction on CheckOverride error

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@ikrommyd @seberg @charris @jorenham
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.