{"record":{"id":"2a09795daf913c77","repo":"apache/flink","slug":"object-obj-is-not-serializable","errorCode":null,"errorMessage":"Object {obj} is not serializable","messagePattern":"Object (.+?) is not serializable","errorType":"exception","errorClass":"InvalidProgramException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/java/ClosureCleaner.java","lineNumber":211,"sourceCode":"            cls.getDeclaredMethod(\"writeObject\", ObjectOutputStream.class);\n            return true;\n        } catch (NoSuchMethodException ignored) {\n        }\n\n        try {\n            cls.getDeclaredMethod(\"writeReplace\");\n            return true;\n        } catch (NoSuchMethodException ignored) {\n        }\n\n        return Externalizable.class.isAssignableFrom(cls);\n    }\n\n    public static void ensureSerializable(Object obj) {\n        try {\n            InstantiationUtil.serializeObject(obj);\n        } catch (Exception e) {\n            throw new InvalidProgramException(\"Object \" + obj + \" is not serializable\", e);\n        }\n    }\n\n    private static boolean cleanThis0(Object func, Class<?> cls, String this0Name) {\n\n        This0AccessFinder this0Finder = new This0AccessFinder(this0Name);\n        getClassReader(cls).accept(this0Finder, 0);\n\n        final boolean accessesClosure = this0Finder.isThis0Accessed();\n\n        if (LOG.isDebugEnabled()) {\n            LOG.debug(this0Name + \" is accessed: \" + accessesClosure);\n        }\n\n        if (!accessesClosure) {\n            Field this0;\n            try {\n                this0 = func.getClass().getDeclaredField(this0Name);","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/java/ClosureCleaner.java#L193-L229","documentation":"Thrown by ClosureCleaner.ensureSerializable() when InstantiationUtil.serializeObject(obj) fails for any reason. This is the generic, no-diagnostic variant: the object could not be Java-serialized. Unlike error 541 this message does not indicate a specific cause; the chained exception (NotSerializableException) names the offending class.","triggerScenarios":"Direct call to ClosureCleaner.ensureSerializable(obj), or the final check in clean() when closureAccessed is false but the object still fails serialization. Any function/object passed to a DataStream operation that contains or references a non-serializable field (e.g. a Thread, Socket, JDBC Connection, SparkContext, raw lambda capturing a non-serializable local).","commonSituations":"A function holds a non-transient field of a non-serializable type (logger with state, DB client, config object from a framework); a lambda captures a local variable of a non-serializable type; passing an object graph that transitively reaches a system resource.","solutions":["Read the chained NotSerializableException to find the offending class name.","Make the offending field transient and reconstruct it in the RichFunction.open() lifecycle hook.","Ensure all fields on the function are themselves Serializable or primitives.","Replace the offending object with a serializable descriptor (e.g. store connection params as Strings, build the connection in open())."],"exampleFix":"// before\npublic class BadMapper implements MapFunction<String,String> {\n  private Connection db; // java.sql.Connection is NOT serializable\n}\n\n// after\npublic class GoodMapper extends RichMapFunction<String,String> {\n  private transient Connection db;\n  private final String url; // serializable descriptor\n  public void open(Configuration c) throws Exception { db = DriverManager.getConnection(url); }\n}","handlingStrategy":"try-catch","validationCode":"// Proactive serialization check\nprivate static void ensureSerializable(Object o) {\n    try {\n        org.apache.flink.util.InstantiationUtil.serializeObject(o);\n    } catch (java.io.NotSerializableException e) {\n        throw new IllegalArgumentException(\"Non-serializable field on \" + o.getClass(), e);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    org.apache.flink.api.java.ClosureCleaner.ensureSerializable(func);\n} catch (org.apache.flink.api.common.InvalidProgramException e) {\n    Throwable cause = e.getCause(); // NotSerializableException names the offending class\n    throw new RuntimeException(\"Make offending fields transient or Serializable: \" + cause, e);\n}","preventionTips":["Mark non-serializable fields transient and rebuild them in open().","Keep only Serializable/primitive fields on functions.","Unit-test serialization of every UDF class."],"tags":["serialization","closure-cleaner","user-function"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}