Skip to content

Commit 8c624ea

Browse files
committed
Do not re-initialize interpreter during finalization. This causes all
kinds of issues including stolen GIL and segfaults.
1 parent 2cb1ecb commit 8c624ea

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

mlir/lib/Bindings/Python/IRAttributes.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,12 @@ class PyDenseIntElementsAttribute
14281428
}
14291429
};
14301430

1431+
// Check if the python version is less than 3.13. Py_IsFinalizing is a part
1432+
// of stable ABI since 3.13 and before it was available as _Py_IsFinalizing.
1433+
#if PY_VERSION_HEX < 0x030d0000
1434+
#define Py_IsFinalizing _Py_IsFinalizing
1435+
#endif
1436+
14311437
class PyDenseResourceElementsAttribute
14321438
: public PyConcreteAttribute<PyDenseResourceElementsAttribute> {
14331439
public:
@@ -1474,7 +1480,7 @@ class PyDenseResourceElementsAttribute
14741480
// The userData is a Py_buffer* that the deleter owns.
14751481
auto deleter = [](void *userData, const void *data, size_t size,
14761482
size_t align) {
1477-
if (!Py_IsInitialized())
1483+
if (!Py_IsFinalizing() && !Py_IsInitialized())
14781484
Py_Initialize();
14791485
Py_buffer *ownedView = static_cast<Py_buffer *>(userData);
14801486
nb::gil_scoped_acquire gil;

0 commit comments

Comments
 (0)