[Mlir-commits] [mlir] [MLIR] Fix thread safety of the deleter in PyDenseResourceElementsAtt… (PR #124832)

Fabian Tschopp llvmlistbot at llvm.org
Tue Jan 28 12:29:58 PST 2025


https://github.com/naibaf7 created https://github.com/llvm/llvm-project/pull/124832

…ribute.

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.

>From e3d795ac8eb4f0ab0d270a098336c2f04267cf12 Mon Sep 17 00:00:00 2001
From: Fabian Tschopp <fabian.tschopp at modular.com>
Date: Tue, 28 Jan 2025 20:25:15 +0000
Subject: [PATCH] [MLIR] Fix thread safety of the deleter in
 PyDenseResourceElementsAttribute.

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.
---
 mlir/lib/Bindings/Python/IRAttributes.cpp | 3 +++
 1 file changed, 3 insertions(+)

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