StackOverFlow error / hanging when trying to serialize classes than extend java.lang.Exception

1,297 views
Skip to first unread message

Chris Gibbs

unread,
Apr 12, 2012, 6:32:12 AM4/12/12
to kryo-...@googlegroups.com
Has anyone had any experience with trying to get classes that extend java.lang.Exception to work with Kryo?

I'm registering the classes using the plain register( ) method, e.g.

kryo.register(MyClassThatExtendsException.class);

I'm also doing the following:

kryo.setReferences(false);

... which results in a StackOverFlowError during the serializing/writing stage:

java.lang.StackOverflowError
at java.lang.reflect.Field.doSecurityCheck(Field.java:947)
at java.lang.reflect.Field.getFieldAccessor(Field.java:896)
at java.lang.reflect.Field.get(Field.java:358)
at com.esotericsoftware.kryo.serializers.FieldSerializer$CachedField.get(FieldSerializer.java:302)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:156)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:488)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:167)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:488)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:167)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:488)
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:167)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:488)
...
(this alternating goes on indefinitely, well until it has caused a stack overflow anyway).


Also, I've tried doing setReferences(true) and that seems to just result in it hanging (but no StackOverflowError thrown). I need to properly debug and trace this to figure out what is causing a probable infinite loop somewhere, I was just wondering if anyone else had had this before and if there was a simple solution?


Nate

unread,
Apr 12, 2012, 1:59:46 PM4/12/12
to kryo-...@googlegroups.com
Exception has an "Exception cause" field, and sometimes the cause is set to "this". If this happens and references is false, you'll get stack overflow. If references is true it should work, but v2 had a nasty bug requiring some refactoring today. See the API change message just posted to the list.

-Nate




--
You received this message because you are subscribed to the "kryo-users" group.
http://groups.google.com/group/kryo-users

Chris Gibbs

unread,
Apr 16, 2012, 5:08:39 AM4/16/12
to kryo-...@googlegroups.com
Sweet, works fine once I set the cause to null, thanks! (I can now leave in setReferences(false), saving some additonal data :)

If anyone is wondering how to set the cause to null - it can only be done once, either in the constructor or immediately after the Exception is created. In the constructor for all of my classes that extend Exception I now have:

    public MyClassThatExtendsException(String message)
    {
        super(message);
        initCause(null);

Nate

unread,
Apr 16, 2012, 5:18:56 AM4/16/12
to kryo-...@googlegroups.com
Glad you got it sorted. Note that setReferences(true) (the default) has less of an impact than it used to due to some recent optimizations. In some cases it can have zero impact on serialized size.

Unrelated, I rewrote Input#require, again. The optional parameter is gone now, as I don't think it made sense once we made the change to avoid reading from the stream if possible. Hopefully it still works for your more complex test case... I mean application. ;)

-Nate


Reply all
Reply to author
Forward
0 new messages