Copilot commented on code in PR #12714:
URL: https://github.com/apache/gluten/pull/12714#discussion_r3772129050


##########
gluten-arrow/src/main/scala/org/apache/gluten/memory/NativeMemoryManager.scala:
##########
@@ -76,7 +76,15 @@ object NativeMemoryManager {
     private val released: AtomicBoolean = new AtomicBoolean(false)
 
     override def addSpiller(spiller: Spiller): Unit = spillers.append(spiller)
-    override def hold(): Unit = NativeMemoryManagerJniWrapper.hold(handle)
+    override def hold(): Unit = {
+      // hold() must run before release(). Reaching here after release means a 
broken teardown
+      // ordering, so surface it instead of dereferencing a freed native 
handle silently.
+      if (released.get()) {
+        throw new GlutenException(
+          s"Cannot hold memory manager instance that has already been 
released: $handle")
+      }
+      NativeMemoryManagerJniWrapper.hold(handle)
+    }

Review Comment:
   The released.get() guard is still racy: release() can start after the check 
but before the JNI call, so hold(handle) can still interleave with 
release(handle) and hit the same use-after-free crash. Guard the JNI call with 
the same monitor that release() uses so native hold/release cannot run 
concurrently.
   
   This issue also appears on line 86 of the same file.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to