[Mlir-commits] [mlir] [MLIR:Python] Fix race on PyOperations. (PR #139721)

Maksim Levental llvmlistbot at llvm.org
Tue May 13 11:44:57 PDT 2025


================
@@ -725,19 +733,27 @@ class PyOperation : public PyOperationBase, public BaseContextObject {
   /// parent context's live operations map, and sets the valid bit false.
   void erase();
 
-  /// Invalidate the operation.
-  void setInvalid() { valid = false; }
-
   /// Clones this operation.
   nanobind::object clone(const nanobind::object &ip);
 
+  /// Invalidate the operation.
+  void setInvalid() {
+    nanobind::ft_lock_guard lock(getContext()->liveOperationsMutex);
+    setInvalidLocked();
+  }
+  /// Like setInvalid(), but requires the liveOperations mutex to be held.
+  void setInvalidLocked() { valid = false; }
----------------
makslevental wrote:

dumb question: is there a way to put a runtime assert/check here that the mutex is actually held? so that there's some way for people that don't read the doc strings (...like me...) to save themselves via compiling with asserts. e.g. i'm wondering if [`nanobind::ft_mutex::lock()`](https://nanobind.readthedocs.io/en/latest/api_core.html#_CPPv4N8nanobind8ft_mutex4lockEv) is a no-op if the mutex is already held/locked by the thread?

https://github.com/llvm/llvm-project/pull/139721


More information about the Mlir-commits mailing list