attilapiros commented on a change in pull request #24269: [SPARK-27341][CORE] 
fix a deadlock between TaskMemoryManager and UnsafeExternalSorter
URL: https://github.com/apache/spark/pull/24269#discussion_r271297526
 
 

 ##########
 File path: 
core/src/main/java/org/apache/spark/util/collection/unsafe/sort/UnsafeExternalSorter.java
 ##########
 @@ -573,19 +573,29 @@ public boolean hasNext() {
 
     @Override
     public void loadNext() throws IOException {
-      synchronized (this) {
-        loaded = true;
-        if (nextUpstream != null) {
-          // Just consumed the last record from in memory iterator
-          if (lastPage != null) {
-            freePage(lastPage);
-            lastPage = null;
+      MemoryBlock pageToFree = null;
+      try {
+        synchronized (this) {
+          loaded = true;
+          if (nextUpstream != null) {
+            // Just consumed the last record from in memory iterator
+            if (lastPage != null) {
+              // Do not free the page here, while we are locking 
`SpillableIterator`. The `freePage`
+              // method locks the `TaskMemoryManager`, and it's a bad idea to 
lock 2 objects in
+              // sequence. We may hit dead lock if another thread locks 
`TaskMemoryManager` and
+              // `SpillableIterator` in sequence, which may happen in
+              // `TaskMemoryManager.acquireExecutionMemory`.
+              pageToFree = lastPage;
+              lastPage = null;
+            }
+            upstream = nextUpstream;
+            nextUpstream = null;
           }
-          upstream = nextUpstream;
-          nextUpstream = null;
+          numRecords--;
+          upstream.loadNext();
         }
-        numRecords--;
-        upstream.loadNext();
+      } finally {
+        if (pageToFree != null) freePage(pageToFree);
 
 Review comment:
   Nit: add braces (according to Oracle java style: `if statements always use 
braces, {}`)

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

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

Reply via email to