This is an automated email from the ASF dual-hosted git repository.

alsuliman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/asterixdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 0f54b575bb [ASTERIXDB-3342][RT] Avoid NPE in close() of 
NestedLoopJoinOperatorDescriptor
0f54b575bb is described below

commit 0f54b575bb785c20cac3d10a0698c6f274c8ae05
Author: Ali Alsuliman <[email protected]>
AuthorDate: Thu Jan 18 14:33:02 2024 -0800

    [ASTERIXDB-3342][RT] Avoid NPE in close() of 
NestedLoopJoinOperatorDescriptor
    
    - user model changes: no
    - storage format changes: no
    - interface changes: no
    
    Details:
    NPE can happen in the close() of NestedLoopJoinOperatorDescriptor
    due to the state object being null.
    The state object can be null if the open() fails
    (due to various reasons like open() of downstream fails).
    
    Change-Id: I565642db8c7610c65f21791aca066fab395a850f
    Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18123
    Reviewed-by: Ali Alsuliman <[email protected]>
    Reviewed-by: Wail Alkowaileet <[email protected]>
    Integration-Tests: Jenkins <[email protected]>
    Tested-by: Jenkins <[email protected]>
---
 .../std/join/NestedLoopJoinOperatorDescriptor.java | 38 +++++++++++++++++-----
 1 file changed, 30 insertions(+), 8 deletions(-)

diff --git 
a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/NestedLoopJoinOperatorDescriptor.java
 
b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/NestedLoopJoinOperatorDescriptor.java
index 24e1b45a7a..52dc24161b 100644
--- 
a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/NestedLoopJoinOperatorDescriptor.java
+++ 
b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/NestedLoopJoinOperatorDescriptor.java
@@ -133,8 +133,11 @@ public class NestedLoopJoinOperatorDescriptor extends 
AbstractOperatorDescriptor
 
                 @Override
                 public void close() throws HyracksDataException {
-                    state.joiner.closeCache();
-                    ctx.setStateObject(state);
+                    // state and state.joiner can be null if open() fails
+                    if (state != null && state.joiner != null) {
+                        state.joiner.closeCache();
+                        ctx.setStateObject(state);
+                    }
                 }
 
                 @Override
@@ -161,10 +164,10 @@ public class NestedLoopJoinOperatorDescriptor extends 
AbstractOperatorDescriptor
 
                 @Override
                 public void open() throws HyracksDataException {
-                    writer.open();
                     state = (JoinCacheTaskState) ctx.getStateObject(
                             new TaskId(new ActivityId(getOperatorId(), 
JOIN_CACHE_ACTIVITY_ID), partition));
                     
state.joiner.setComparator(comparatorFactory.createTuplePairComparator(ctx));
+                    writer.open();
                 }
 
                 @Override
@@ -175,11 +178,7 @@ public class NestedLoopJoinOperatorDescriptor extends 
AbstractOperatorDescriptor
                 @Override
                 public void close() throws HyracksDataException {
                     if (failed) {
-                        try {
-                            state.joiner.closeCache();
-                        } finally {
-                            writer.close();
-                        }
+                        closeOnFail();
                         return;
                     }
                     try {
@@ -202,6 +201,29 @@ public class NestedLoopJoinOperatorDescriptor extends 
AbstractOperatorDescriptor
                     failed = true;
                     writer.fail();
                 }
+
+                private void closeOnFail() {
+                    try {
+                        // state can be null if open() fails
+                        JoinCacheTaskState stateObject = state;
+                        if (state == null) {
+                            // make sure if the state object is actually still 
in the ctx, then close the resources
+                            stateObject = (JoinCacheTaskState) 
ctx.getStateObject(
+                                    new TaskId(new ActivityId(getOperatorId(), 
JOIN_CACHE_ACTIVITY_ID), partition));
+                        }
+                        if (stateObject != null) {
+                            state.joiner.closeCache();
+                        }
+                    } catch (Exception e) {
+                        // ignore
+                    } finally {
+                        try {
+                            writer.close();
+                        } catch (Exception e) {
+                            // ignore
+                        }
+                    }
+                }
             };
         }
     }

Reply via email to