Skip to content

BUG: Reference leak in PyUFunc_GenericReduction on CheckOverride error #31677

Closed

Description

@Fuyugithub

Bug Description

In numpy/_core/src/umath/ufunc_object.c, when PyUFunc_CheckOverride fails in PyUFunc_GenericReduction, the code returns NULL directly instead of jumping to the cleanup label, leaking full_args.in and full_args.out.

Location

numpy/_core/src/umath/ufunc_object.c:3777-3780

Code

int errval = PyUFunc_CheckOverride(ufunc, _reduce_type[operation],
        full_args.in, full_args.out, wheremask_obj, args, len_args, kwnames, &override);
if (errval) {
    return NULL;    // BUG: should be `goto fail` — leaks full_args.in/out
}

Impact

When reduce/accumulate/reduceat encounters a __array_ufunc__ override error, the full_args.in and full_args.out tuple references are leaked. This happens every time an override is attempted on an object that raises an exception in its __array_ufunc__ implementation.

Suggested Fix

if (errval) {
    goto fail;  // cleanup path that DECREFs full_args
}

Environment

  • NumPy version: current main
  • Affected: np.add.reduce(), np.accumulate() etc. with __array_ufunc__ overrides that raise

Activity

  1. Vatmuri2 commented on Jun 19, 2026

    @Vatmuri2

    Saw this is marked low priority. I was looking for a good first contribution and I'd love to take this one if it's still up for grabs!

    My plan: the one-line goto fail fix, plus a regression test that checks the operand's refcount stays flat when array_ufunc raises during a reduce. I'll get a PR up soon. Totally happy to adjust the plan, would love to learn from this, let me know if there's any other considerations!

  2. changed the title [-][BUG] Reference leak in PyUFunc_GenericReduction on CheckOverride error[/-] [+]BUG: Reference leak in PyUFunc_GenericReduction on CheckOverride error[/+] on Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority: lowValid, but not for immediate attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions