Skip to content

Commit 28507ac

Browse files
authored
[MLIR] Fix thread safety of the deleter in PyDenseResourceElementsAttribute (#124832)
In general, `PyDenseResourceElementsAttribute` can get deleted at any time and any thread, where unlike the `getFromBuffer` call, the Python interpreter may not be initialized and the GIL may not be held. This PR fixes segfaults caused by `PyBuffer_Release` when the GIL is not being held by the thread calling the deleter.
1 parent b8cdc5e commit 28507ac

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

mlir/lib/Bindings/Python/IRAttributes.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,10 @@ class PyDenseResourceElementsAttribute
14681468
// The userData is a Py_buffer* that the deleter owns.
14691469
auto deleter = [](void *userData, const void *data, size_t size,
14701470
size_t align) {
1471+
if (!Py_IsInitialized())
1472+
Py_Initialize();
14711473
Py_buffer *ownedView = static_cast<Py_buffer *>(userData);
1474+
nb::gil_scoped_acquire gil;
14721475
PyBuffer_Release(ownedView);
14731476
delete ownedView;
14741477
};

0 commit comments

Comments
 (0)