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

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


The following commit(s) were added to refs/heads/master by this push:
     new a9781112a1 [flink] Fix NPE in close() of operators that failed before 
initialization (#11099)
a9781112a1 is described below

commit a9781112a11a9a7d7d4147644add9af16f2359bf
Author: p-eye <[email protected]>
AuthorDate: Thu Nov 12 14:12:56 2026 +0900

    [flink] Fix NPE in close() of operators that failed before initialization 
(#11099)
---
 .../sink/MultiTablesStoreCompactOperator.java      |  6 +-
 .../paimon/flink/sink/StoreCompactOperator.java    |  4 +-
 .../source/AppendBypassCoordinateOperator.java     |  4 +-
 .../sink/MultiTablesStoreCompactOperatorTest.java  | 56 +++++++++++++++++
 .../flink/sink/StoreCompactOperatorTest.java       | 23 +++++++
 .../source/AppendBypassCoordinateOperatorTest.java | 73 ++++++++++++++++++++++
 6 files changed, 162 insertions(+), 4 deletions(-)

diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/MultiTablesStoreCompactOperator.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/MultiTablesStoreCompactOperator.java
index 7d7ba29760..ea254d0088 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/MultiTablesStoreCompactOperator.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/MultiTablesStoreCompactOperator.java
@@ -211,8 +211,10 @@ public class MultiTablesStoreCompactOperator
     @Override
     public void close() throws Exception {
         super.close();
-        for (StoreSinkWrite write : writes.values()) {
-            write.close();
+        if (writes != null) {
+            for (StoreSinkWrite write : writes.values()) {
+                write.close();
+            }
         }
         if (catalog != null) {
             catalog.close();
diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/StoreCompactOperator.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/StoreCompactOperator.java
index d1cbfd3c64..1914eb1ef8 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/StoreCompactOperator.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/StoreCompactOperator.java
@@ -203,7 +203,9 @@ public class StoreCompactOperator extends 
PrepareCommitOperator<RowData, Committ
     @Override
     public void close() throws Exception {
         super.close();
-        write.close();
+        if (write != null) {
+            write.close();
+        }
     }
 
     @VisibleForTesting
diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/AppendBypassCoordinateOperator.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/AppendBypassCoordinateOperator.java
index 3fd3e305de..20c56bc32f 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/AppendBypassCoordinateOperator.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/source/AppendBypassCoordinateOperator.java
@@ -124,7 +124,9 @@ public class AppendBypassCoordinateOperator<CommitT>
 
     @Override
     public void close() throws Exception {
-        ExecutorUtils.gracefulShutdown(1, TimeUnit.MINUTES, executorService);
+        if (executorService != null) {
+            ExecutorUtils.gracefulShutdown(1, TimeUnit.MINUTES, 
executorService);
+        }
         super.close();
     }
 }
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/MultiTablesStoreCompactOperatorTest.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/MultiTablesStoreCompactOperatorTest.java
new file mode 100644
index 0000000000..53c22d1eba
--- /dev/null
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/MultiTablesStoreCompactOperatorTest.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.flink.sink;
+
+import org.apache.paimon.options.Options;
+
+import org.apache.flink.streaming.api.environment.CheckpointConfig;
+import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness;
+import org.apache.flink.table.data.RowData;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link MultiTablesStoreCompactOperator}. */
+public class MultiTablesStoreCompactOperatorTest {
+
+    @Test
+    public void testCloseAfterInitializeStateFailure() throws Exception {
+        MultiTablesStoreCompactOperator.Factory operatorFactory =
+                new MultiTablesStoreCompactOperator.Factory(
+                        () -> {
+                            throw new RuntimeException("Failed to load 
catalog");
+                        },
+                        "10086",
+                        new CheckpointConfig(),
+                        true,
+                        false,
+                        false,
+                        new Options());
+        try (OneInputStreamOperatorTestHarness<RowData, MultiTableCommittable> 
harness =
+                new OneInputStreamOperatorTestHarness<>(operatorFactory)) {
+            harness.setup();
+            assertThatThrownBy(harness::initializeEmptyState)
+                    .hasMessageContaining("Failed to load catalog");
+
+            assertThatCode(() -> 
harness.getOperator().close()).doesNotThrowAnyException();
+        }
+    }
+}
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/StoreCompactOperatorTest.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/StoreCompactOperatorTest.java
index cf78c43192..75fa55e079 100644
--- 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/StoreCompactOperatorTest.java
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/StoreCompactOperatorTest.java
@@ -63,6 +63,7 @@ import java.util.Map;
 import java.util.UUID;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 /** Test for {@link StoreCompactOperator}. */
@@ -172,6 +173,28 @@ public class StoreCompactOperatorTest extends 
TableTestBase {
         assertThat(compactRememberStoreWrite.compactTime).isEqualTo(3);
     }
 
+    @Test
+    public void testCloseAfterInitializeStateFailure() throws Exception {
+        createTableDefault();
+
+        StoreCompactOperator.Factory operatorFactory =
+                new StoreCompactOperator.Factory(
+                        getTableDefault(),
+                        (table, commitUser, state, ioManager, 
memoryPoolFactory, metricGroup) -> {
+                            throw new RuntimeException("Failed to create 
write");
+                        },
+                        "10086",
+                        false);
+        try (OneInputStreamOperatorTestHarness<RowData, Committable> harness =
+                new OneInputStreamOperatorTestHarness<>(operatorFactory)) {
+            harness.setup(new CommittableTypeInfo().createSerializer(new 
ExecutionConfig()));
+            assertThatThrownBy(harness::initializeEmptyState)
+                    .hasMessageContaining("Failed to create write");
+
+            assertThatCode(() -> 
harness.getOperator().close()).doesNotThrowAnyException();
+        }
+    }
+
     @Test
     public void testStreamingCompactConflictWithOverwrite() throws Exception {
         Schema schema =
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/AppendBypassCoordinateOperatorTest.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/AppendBypassCoordinateOperatorTest.java
new file mode 100644
index 0000000000..bfc1b99e52
--- /dev/null
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/source/AppendBypassCoordinateOperatorTest.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.flink.source;
+
+import org.apache.paimon.CoreOptions;
+import org.apache.paimon.append.AppendCompactTask;
+import org.apache.paimon.flink.sink.Committable;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.fs.local.LocalFileIO;
+import org.apache.paimon.schema.FileSystemSchemaManager;
+import org.apache.paimon.schema.Schema;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.table.FileStoreTableFactory;
+import org.apache.paimon.types.IntType;
+import org.apache.paimon.types.RowType;
+
+import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness;
+import org.apache.flink.types.Either;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import java.util.Collections;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+
+/** Tests for {@link AppendBypassCoordinateOperator}. */
+public class AppendBypassCoordinateOperatorTest {
+
+    @TempDir java.nio.file.Path temp;
+
+    @Test
+    public void testCloseWithoutOpen() throws Exception {
+        try (OneInputStreamOperatorTestHarness<Committable, 
Either<Committable, AppendCompactTask>>
+                harness =
+                        new OneInputStreamOperatorTestHarness<>(
+                                new 
AppendBypassCoordinateOperatorFactory<>(createTable()))) {
+            harness.setup();
+
+            // Flink closes operators that were never opened when a task is 
cancelled during
+            // restore.
+            assertThatCode(() -> 
harness.getOperator().close()).doesNotThrowAnyException();
+        }
+    }
+
+    private FileStoreTable createTable() throws Exception {
+        Path tablePath = new Path(temp.toUri().toString());
+        new FileSystemSchemaManager(LocalFileIO.create(), tablePath)
+                .createTable(
+                        new Schema(
+                                RowType.of(new IntType()).getFields(),
+                                Collections.emptyList(),
+                                Collections.emptyList(),
+                                
Collections.singletonMap(CoreOptions.BUCKET.key(), "-1"),
+                                ""));
+        return FileStoreTableFactory.create(LocalFileIO.create(), tablePath);
+    }
+}

Reply via email to