[Mlir-commits] [mlir] 28507ac - [MLIR] Fix thread safety of the deleter in PyDenseResourceElementsAttribute (#124832)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Jan 28 15:56:04 PST 2025


Author: Fabian Tschopp
Date: 2025-01-28T18:56:00-05:00
New Revision: 28507ac62928d79f647804de4df60409b3ebb364

URL: https://github.com/llvm/llvm-project/commit/28507ac62928d79f647804de4df60409b3ebb364
DIFF: https://github.com/llvm/llvm-project/commit/28507ac62928d79f647804de4df60409b3ebb364.diff

LOG: [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.

Added: 
    

Modified: 
    mlir/lib/Bindings/Python/IRAttributes.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 7bc21a31c3c84c..142b6eca11c438 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -1468,7 +1468,10 @@ class PyDenseResourceElementsAttribute
     // 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;
     };


        


More information about the Mlir-commits mailing list