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

Peter Hawkins llvmlistbot at llvm.org
Wed May 14 08:01:02 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; }
----------------
hawkinsp wrote:

I should add: there's no particular reason we have to use a `nb::ft_mutex` here, they aren't particularly special. We could use another mutex that does have this API. However, `nb::ft_mutex` has the nice property of being present if and only if we are in GIL-disabled mode.

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


More information about the Mlir-commits mailing list