nanobind: leaked ... when a DuckDB type is referenced in overload signature and numpy is imported

Abierto
#732 5 comentarios 3 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
numpy, python
Área
databases

Línea de trabajo

Start with the reproduction script in the issue: it needs only typing.overload referencing duckdb.DuckDBPyConnection plus import numpy, and the leak appears at interpreter exit. Run it with uv run --script to confirm the nanobind warnings. The cause is a reference-counting problem in the nanobind binding code, so the work is tracing which type or function objects are not released, which needs familiarity with nanobind. Done means the script exits without the leak warnings.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

needs triage
What happens?

When the interpreter exits, duckdb 2.0.0 prints nanobind leak warnings if both of these are true:

  • a DuckDB type (e.g. DuckDBPyConnection) is referenced from a typing.overload signature, which typing keeps in its module-level overload registry until shutdown
  • numpy is imported (torch 2.6.0 triggers it the same way)
nanobind: leaked 27 instances!
nanobind: leaked 6 types!
 - leaked type "_duckdb._sqltypes.DuckDBPyType"
 - leaked type "_duckdb.DuckDBPyConnection"
 - leaked type "FunctionNullHandling"
 - leaked type "PythonUDFType"
 - leaked type "ExplainType"
 - leaked type "PythonExceptionHandling"
nanobind: leaked 84 functions!
 - leaked function "fetchone"
 - leaked function "fetch_df"
 - leaked function "__exit__"
 - leaked function "_result_as_arrow"
 - leaked function ""
 - leaked function "abort"
 - leaked function "append"
 - leaked function "interrupt"
 - leaked function "dtype"
 - leaked function "_register_values"
 - ... skipped remainder
nanobind: this is likely caused by a reference counting issue in the binding code.
See https://nanobind.readthedocs.io/en/latest/refleaks.html%
To Reproduce

with duckdb_nanobind_leak.py as follows

# /// script
# requires-python = ">=3.13"
# dependencies = ["duckdb==2.0.0", "numpy==2.5.2"]
# ///
"""duckdb 2.0.0 prints "nanobind: leaked ..." at interpreter exit.

Run: uv run --script duckdb_nanobind_leak.py

Triggered when a DuckDB type is referenced from a `typing.overload` signature (typing keeps overloads in a
module-level registry until shutdown) and numpy is imported. Without the overload, or without numpy, exit is clean.
torch (2.6.0) triggers it the same way as numpy.
"""

from typing import overload

import duckdb
import numpy


@overload
def connect(database: str) -> duckdb.DuckDBPyConnection: ...
@overload
def connect(database: None) -> None: ...
def connect(database):
    return None if database is None else duckdb.connect(database)

run uv run --script duckdb_nanobind_leak.py

OS:

x86_64

DuckDB Version:

2.0.0

DuckDB Client:

Python

Hardware:

No response

Full Name:

Bartosz Marcinkowski

Affiliation:

MIM Fertility

Did you include all relevant configuration (e.g., CPU architecture, Linux distribution) to reproduce the issue?
  • Yes, I have
Did you include all code required to reproduce the issue?
  • Yes, I have
Did you include all relevant data sets for reproducing the issue?

Not applicable - the reproduction does not require a data set

Lenguaje dominante
Python
Estrellas
203
Forks
124
Merge medio
13 h 36 min
PR fusionados (30 d)
61

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de duckdb/duckdb-python

Todos los issues de duckdb/duckdb-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.