I have a stacktrace that from my logs that looks like:
Aug 10, 2022 10:10:10 PM io.grpc.internal.SerializingExecutor run
SEVERE: Exception while executing runnable io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$1MessagesAvailable@5703a7b7
java.lang.NullPointerException
at MyProtoFile$MyProtoClass.<init>(MyProtoFile.java:20)
at MyProtoFile$MyProtoClass.<init>(MyProtoFile.java:10)
at MyProtoFile$MyProtoClass$1.parsePartialFrom(MyProtoFile.java:50)
at MyProtoFile$MyProtoClass$1.parsePartialFrom(MyProtoFile.java:40)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:86)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:48)
at io.grpc.protobuf.lite.ProtoLiteUtils$MessageMarshaller.parseFrom(ProtoLiteUtils.java:223)
at io.grpc.protobuf.lite.ProtoLiteUtils$MessageMarshaller.parse(ProtoLiteUtils.java:215)
at io.grpc.protobuf.lite.ProtoLiteUtils$MessageMarshaller.parse(ProtoLiteUtils.java:118)
at io.grpc.MethodDescriptor.parseRequest(MethodDescriptor.java:307)
at io.grpc.internal.ServerCallImpl$ServerStreamListenerImpl.messagesAvailableInternal(ServerCallImpl.java:318)
at io.grpc.internal.ServerCallImpl$ServerStreamListenerImpl.messagesAvailable(ServerCallImpl.java:301)
at io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$1MessagesAvailable.runInContext(ServerImpl.java:834)
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
However, when you look at the corresponding generated code, MyProtoFile$MyProtoClass.<init>(MyProtoFile.java:10), MyProtoFile$MyProtoClass$1.parsePartialFrom(MyProtoFile.java:40), and com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:48) point to the line that begins that class definition with public [static] class ClassName, for the first time the stacktrace enters that class.
I am not able to provide the source code generated by protobuf for MyProtoFile, but the rest of the stack trace is publicly available on github. For example, Abstract parser is available here: https://github.com/protocolbuffers/protobuf/blob/v3.20.0/java/core/src/main/java/com/google/protobuf/AbstractParser.java. You can see that line 48 is the beginning of the class definition for AbstractParser.
If you try running with Vanilla java 11:
public class ConstructorStackTrace {
public static class InnerClass {
public InnerClass() {
throw new NullPointerException("hello");
}
}
public static void main(String []args){
System.err.println("OneConstructor");
try {
OneConstructor constructorStackTrace = new OneConstructor();
} catch (Exception e) {
e.printStackTrace();
}
System.err.println("NestedConstructor");
try {
NestedConstructor constructorStackTrace = new NestedConstructor();
} catch (Exception e) {
e.printStackTrace();
}
System.err.println("InnerClass");
try {
InnerClass constructorStackTrace = new InnerClass();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class OneConstructor {
public OneConstructor() {
throw new NullPointerException("hello");
}
}
class NestedConstructor {
public NestedConstructor() {
this("Hello");
}
public NestedConstructor(String hello) {
throw new NullPointerException("hello");
}
}
You get the below which do not have StackTraceElements for the first line of the class definition:
OneConstructor
java.lang.NullPointerException: hello
at OneConstructor.<init>(ConstructorStackTrace.java:33)
at ConstructorStackTrace.main(ConstructorStackTrace.java:12)
NestedConstructor
java.lang.NullPointerException: hello
at NestedConstructor.<init>(ConstructorStackTrace.java:43)
at NestedConstructor.<init>(ConstructorStackTrace.java:39)
at ConstructorStackTrace.main(ConstructorStackTrace.java:18)
InnerClass
java.lang.NullPointerException: hello
at ConstructorStackTrace$InnerClass.<init>(ConstructorStackTrace.java:5)
at ConstructorStackTrace.main(ConstructorStackTrace.java:24)
Where did these extra StackTraceElements come from? Is this something inserted by grpc? by grpc's logging (their logging has a different format tha mine)? The rest of my logs don't have these extra StackTraceElements. At first I thought I had the wrong version of the code, but three classes (MyProtoClass, MyProtoClass$1, AbstractParser) all follow this pattern, making it to much of a coincidence.