Skip to content

BUG: DType reference leak in _convert_from_type (descriptor.c) #31676

Closed

Description

@Fuyugithub

Bug Description

In numpy/_core/src/multiarray/descriptor.c, PyArray_DiscoverDTypeFromScalarType returns a new reference, but PyArray_GetDefaultDescr does NOT steal it. The DType reference is leaked unconditionally.

Location

numpy/_core/src/multiarray/descriptor.c:1599-1601

Code

PyObject *DType = PyArray_DiscoverDTypeFromScalarType(typ);  // NEW REF
if (DType != NULL) {
    return PyArray_GetDefaultDescr((PyArray_DTypeMeta *)DType);  // DType LEAKED!
}

Impact

PyArray_GetDefaultDescr returns a new reference (via Py_INCREF or NPY_DT_CALL_default_descr). It does NOT steal the DType reference. Therefore DType is leaked on every successful call to _convert_from_type with a scalar type. This triggers on every custom scalar type → dtype conversion.

Suggested Fix

PyObject *DType = PyArray_DiscoverDTypeFromScalarType(typ);
if (DType != NULL) {
    PyArray_Descr *ret = PyArray_GetDefaultDescr((PyArray_DTypeMeta *)DType);
    Py_DECREF(DType);
    return ret;
}

Environment

  • NumPy version: current main
  • Affected: numpy.dtype() conversions from custom scalar types

Activity

  1. seberg commented on Jun 18, 2026

    @seberg
    Member

    All of these seem like real but very minor issues (besides one theoretically they they have zero real world impact).
    But as a note, fully automated results should try to be brief and more importantly make a note of that.

  2. changed the title [-][BUG] DType reference leak in _convert_from_type (descriptor.c)[/-] [+]BUG: DType reference leak in _convert_from_type (descriptor.c)[/+] 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