[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.
This commit is contained in:
Fabian Tschopp 2025-01-29 00:56:00 +01:00 committed by GitHub
parent b8cdc5ea27
commit 28507ac629
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1468,7 +1468,10 @@ public:
// The userData is a Py_buffer* that the deleter owns.
auto deleter = [](void *userData, const void *data, size_t size,
size_t align) {
if (!Py_IsInitialized())
Py_Initialize();
Py_buffer *ownedView = static_cast<Py_buffer *>(userData);
nb::gil_scoped_acquire gil;
PyBuffer_Release(ownedView);
delete ownedView;
};