Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nicreve opened this issue Apr 16, 2021 · 3 comments · Fixed by #822
Closed
Assignees
Labels

Comments

@nicreve
Copy link

nicreve commented Apr 16, 2021

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:

  • OS: Ubuntu 20.04
  • JDK Version: 8
  • Kryo Version: 5.0.4

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

@theigl
Copy link
Collaborator

theigl commented Apr 16, 2021

@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 added a commit that referenced this issue Apr 17, 2021
@theigl
Copy link
Collaborator

theigl commented Apr 17, 2021

Merged. Please verify the fix against the latest SNAPSHOT.

@nicreve
Copy link
Author

nicreve commented Apr 17, 2021

Merged. Please verify the fix against the latest SNAPSHOT.

Thanks, I will verify the fix next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants