EsotericSoftware / kryo

Java binary serialization and cloning: fast, efficient, automatic
BSD 3-Clause "New" or "Revised" License
6.19k stars 823 forks source link

Lambda field (functional interface) deserialize failed with CompatiableFieldSerializer #821

Closed nicreve closed 3 years ago

nicreve commented 3 years ago

Describe the bug We have a class with a field to store lambda, the field's class is a functional interface, something like this:

public class TestClass implements Serializable {
    private static final long serialVersionUID = 4061358520297707007L;

    @FunctionalInterface
    public interface Callback extends Serializable {
        void execute();
    }

    private Callback callback;
}

When serialize then deserialize this class with CompatiableFieldSerializer, we encountered this exception:

Caused by: com.esotericsoftware.kryo.KryoException: Read type is incompatible with the field type: com.esotericsoftware.kryo.serializers.ClosureSerializer$Closure -> com.esotericsoftware.kryo.serializers.ClosureSerializer$Closure (com.xxx.TestClass#callback)

I believe it's because CompatiableFieldSerializer cached the field's value class (set to TestClass#callback) during serialization, but the actual output content is ClosureSerializer$Closure. Then during deserialization, it's mismatched. To Reproduce Use CompatiableFieldSerializer to serialize a class with lambda field(s), then deserialize it.

Environment:

Additional context I switch to FieldSerializer it works fine, but I didn't test TaggedFieldSerializer and VersionFieldSerializer.

theigl commented 3 years ago

@nicreve: Good catch!

The type validation in CompatibleFieldSerializer currently fails for lambdas because they are serialized using a marker type ClosureSerializer$Closure. I created a PR that adds an additional check for this marker interface. It works, but it is not safe, because we do not know if the serialized lambda really is assignable to the given type. I'll think about it some more, but I'll apply the PR if I can't come up with a better solution.

theigl commented 3 years ago

Merged. Please verify the fix against the latest SNAPSHOT.

nicreve commented 3 years ago

Merged. Please verify the fix against the latest SNAPSHOT.

Thanks, I will verify the fix next week.