Skip to content

Commit

Permalink
Merge pull request #790 from theigl/789-stackoverflow-message
Browse files Browse the repository at this point in the history
#789 Catch StackOverflowError and add hint for enabling references
  • Loading branch information
theigl authored Nov 16, 2020
2 parents ae777c6 + 7cedf69 commit 04e0e90
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/com/esotericsoftware/kryo/serializers/ReflectField.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public void write (Output output, Object object) {
} catch (KryoException ex) {
ex.addTrace(name + " (" + object.getClass().getName() + ")");
throw ex;
} catch (StackOverflowError ex) {
throw new KryoException(
"A StackOverflow occurred. The most likely cause is that your data has a circular reference resulting in " +
"infinite recursion. Try enabling references with Kryo.setReferences(true). If your data structure " +
"is really more than " + kryo.getDepth() + " levels deep then try increasing your Java stack size.",
ex);
} catch (Throwable t) {
KryoException ex = new KryoException(t);
ex.addTrace(name + " (" + object.getClass().getName() + ")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,22 @@ public void testDeep () {
roundTrip(1440, root);
}

@Test
public void testCircularReference () {
kryo.register(CircularReference.class);
kryo.register(CircularReference.Inner.class);

CircularReference instance = new CircularReference();
try {
roundTrip(1, instance);
} catch (KryoException ex) {
assertTrue(ex.getMessage().contains("A StackOverflow occurred."));
return;
}

fail("Exception was expected");
}

public static class DefaultTypes {
// Primitives.
public boolean booleanField;
Expand Down Expand Up @@ -1279,4 +1295,17 @@ public boolean equals (Object obj) {
return true;
}
}

static class CircularReference {
Inner b = new Inner(this);

static class Inner {
CircularReference a;

public Inner(CircularReference a) {
this.a = a;
}
}
}

}

0 comments on commit 04e0e90

Please sign in to comment.