[BUG]: static numpy array_t leads to segfault on exit on Python 3.12

Offen
#5,270 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 4 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
cpp, numpy, python
Bereich
backend

Rechercherichtung

Beginne damit, den Fehler unter Python 3.12 mit dem bereitgestellten Modul test_static zu reproduzieren und ihn mit Python 3.11 zu vergleichen. Untersuche anschließend pybind11/numpy.h und die Lebensdauer des statischen Arrays Static::PYARRAY(). Überprüfe sowohl den grundlegenden Konstruktionsbefehl als auch die Sequenz aus Konstruktion und Löschung. Erledigt ist die Aufgabe, wenn keines der beiden Szenarien beim Beenden des Interpreters oder beim Löschen und erneuten Erstellen von Static-Instanzen einen Segmentation Fault verursacht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

triage
Required prerequisites
What version (or hash if on master) of pybind11 are you using?

2.13.1

Problem description

Observed behaviour: When exiting a Python script after constructing a bound class with an initialized static pybind11::array_t, there is a segfault calling decref on the array on Python 3.12.

Expected behaviour: No segfault, as is the case on Python < 3.12.

I have tested with several combinations of package version but can definitely confirm that on a Fedora 40 conda env with gxx=14.1.0, numpy=2.0.0 and pybind11=2.13.1 the segfault occurs with Python 3.12 but not 3.11, so this does not appear to be a regression in pybind11.

I compiled the module as follows:
g++ -Wall -shared -fPIC -std=c++17 $(python3 -m pybind11 --includes) test_image.cc -o test_static$(python3-config --extension-suffix)
and tested with:
python -c "from test_static import Static; Static()"

It is possible to defer the problem by adding a destructor like so:
~Static() { PYARRAY().release(); }
... however, this only fixes the trivial example and fails as soon as one instance is deleted, and the following example fails upon constructing a second instance:
python -c "from test_static import Static; x = Static(); del x; y = Static()"

Reproducible example code
#include <cassert>

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;
using namespace pybind11::literals;

struct Static {
public:
    Static() { assert(this->PYARRAY().ndim() == 2); }

    py::array_t<double>& PYARRAY() const {
        static py::array_t<double> array(py::array::ShapeContainer({1, 1}));
        return array;
    }
};

PYBIND11_MODULE(test_static, m) {
    py::class_<Static, std::shared_ptr<Static>>(m, "Static").def(py::init<>());
}
Is this a regression? Put the last known working version here if it is.

Not a regression in pybind11 but perhaps in CPython?

Vorherrschende Sprache
C++
Sterne
18k
Forks
2.3k
Ø Merge
5 T. 7 Std.
Gemergte PRs (30 T.)
53

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus pybind/pybind11

Alle Issues in pybind/pybind11

Ähnliche Issues

Weitere Issues zu C++

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.