You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ch...@apache.org on 2023/06/13 21:11:11 UTC

[flink] branch master updated: [FLINK-31919][core] Only run closure cleaner if serialization fails

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 50f069ce5b1 [FLINK-31919][core] Only run closure cleaner if serialization fails
50f069ce5b1 is described below

commit 50f069ce5b1203df8acd724d804afc33eda8a9d4
Author: Chesnay Schepler <ch...@apache.org>
AuthorDate: Sun Apr 23 20:45:33 2023 +0200

    [FLINK-31919][core] Only run closure cleaner if serialization fails
---
 .../java/org/apache/flink/api/java/ClosureCleaner.java | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/flink-core/src/main/java/org/apache/flink/api/java/ClosureCleaner.java b/flink-core/src/main/java/org/apache/flink/api/java/ClosureCleaner.java
index b0ee8900731..0c5a73071d6 100644
--- a/flink-core/src/main/java/org/apache/flink/api/java/ClosureCleaner.java
+++ b/flink-core/src/main/java/org/apache/flink/api/java/ClosureCleaner.java
@@ -92,6 +92,12 @@ public class ClosureCleaner {
             return;
         }
 
+        if (canBeSerialized(func)) {
+            return;
+        }
+
+        // serialization failed; try cleaning closure as a fallback
+
         // First find the field name of the "this$0" field, this can
         // be "this$x" depending on the nesting
         boolean closureAccessed = false;
@@ -169,7 +175,17 @@ public class ClosureCleaner {
     private static boolean needsRecursion(Field f, Object fo) {
         return (fo != null
                 && !Modifier.isStatic(f.getModifiers())
-                && !Modifier.isTransient(f.getModifiers()));
+                && !Modifier.isTransient(f.getModifiers())
+                && !canBeSerialized(fo));
+    }
+
+    private static boolean canBeSerialized(Object o) {
+        try {
+            InstantiationUtil.serializeObject(o);
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
     }
 
     private static boolean usesCustomSerialization(Class<?> cls) {