PubSub: grpc Uncaught exception in the SynchronizationContext. Panic! - Could not find LoadBalancer pick_first
- Specify the API: PubSub
- OS type and version: Docker image gradle:5.2-jdk11-slim (also happens with jdk8 and other images)
- Java version: jdk11 and jdk8
- google-cloud-java version(s): 1.62.0 and above (tested with 1.65.0, too)
Steps to reproduce
- Use google-cloud-pubsub library with version 1.62.0 or above as dependency
- Create a subscription in own code (see code example below)
Code example
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
subscriptionAdminClient.getSubscription(subscriptionName); // Exception is thrown in this line
}
...
Stack trace
Mar 19, 2019 6:20:52 PM io.grpc.internal.ManagedChannelImpl$1 uncaughtException
SEVERE: [io.grpc.internal.ManagedChannelImpl-1] Uncaught exception in the SynchronizationContext. Panic!
java.lang.IllegalStateException: Could not find LoadBalancer pick_first. The build probably threw away META-INF/services/io.grpc.LoadBalancerProvider
at io.grpc.internal.AutoConfiguredLoadBalancerFactory$AutoConfiguredLoadBalancer.<init>(AutoConfiguredLoadBalancerFactory.java:74)
at io.grpc.internal.AutoConfiguredLoadBalancerFactory.newLoadBalancer(AutoConfiguredLoadBalancerFactory.java:45)
at io.grpc.internal.ManagedChannelImpl.exitIdleMode(ManagedChannelImpl.java:351)
at io.grpc.internal.ManagedChannelImpl$ChannelTransportProvider$1ExitIdleModeForTransport.run(ManagedChannelImpl.java:447)
at io.grpc.SynchronizationContext.drain(SynchronizationContext.java:101)
at io.grpc.SynchronizationContext.execute(SynchronizationContext.java:130)
at io.grpc.internal.ManagedChannelImpl$ChannelTransportProvider.get(ManagedChannelImpl.java:451)
at io.grpc.internal.ClientCallImpl.start(ClientCallImpl.java:241)
at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1.start(CensusTracingModule.java:392)
at io.grpc.internal.CensusStatsModule$StatsClientInterceptor$1.start(CensusStatsModule.java:689)
at io.grpc.ForwardingClientCall.start(ForwardingClientCall.java:32)
at com.google.api.gax.grpc.GrpcHeaderInterceptor$1.start(GrpcHeaderInterceptor.java:95)
at io.grpc.stub.ClientCalls.startCall(ClientCalls.java:308)
at io.grpc.stub.ClientCalls.asyncUnaryRequestCall(ClientCalls.java:280)
at io.grpc.stub.ClientCalls.futureUnaryCall(ClientCalls.java:189)
at com.google.api.gax.grpc.GrpcDirectCallable.futureCall(GrpcDirectCallable.java:58)
at com.google.api.gax.grpc.GrpcExceptionCallable.futureCall(GrpcExceptionCallable.java:64)
at com.google.api.gax.rpc.AttemptCallable.call(AttemptCallable.java:86)
at com.google.api.gax.rpc.RetryingCallable.futureCall(RetryingCallable.java:63)
at com.google.api.gax.rpc.RetryingCallable.futureCall(RetryingCallable.java:41)
at com.google.api.gax.tracing.TracedUnaryCallable.futureCall(TracedUnaryCallable.java:75)
at com.google.api.gax.rpc.UnaryCallable$1.futureCall(UnaryCallable.java:126)
at com.google.api.gax.rpc.UnaryCallable.futureCall(UnaryCallable.java:87)
at com.google.api.gax.rpc.UnaryCallable.call(UnaryCallable.java:112)
at com.google.cloud.pubsub.v1.SubscriptionAdminClient.getSubscription(SubscriptionAdminClient.java:467)
at com.google.cloud.pubsub.v1.SubscriptionAdminClient.getSubscription(SubscriptionAdminClient.java:420)
at my.package.MyClass.start(MyClass.java:37)
at my.package.App$1.run(App.java:31)
at java.base/java.lang.Thread.run(Thread.java:834)
External references such as API reference guides used
- None
Any additional information below
The error only occurs since google-cloud-pubsub 1.62.0 . When using google-cloud-pubsub 1.61.0 or below, the error does not occur with the same user code.
Thank you for the report!
@sduskis, @andreamlin, Pub/Sub version 1.62 included an upgrade in gax - any idea if there were changes that could have caused this?
@kolea2, can you tell if there was a grpc upgrade? That might be one possible reason.
Yep - looks like grpc went from 1.17.1 to 1.18.0: https://github.com/googleapis/google-cloud-java/pull/4496/
@sceee can you please share your docker file with full repro? I tried the steps you mentioned and couldn't reproduce.
@sceee I can reproduce this when I build a 'thick' jar using the maven shade plugin and add the Google Cloud client libraries as dependencies in the jar. Are you doing something like that?
If so, the files io.grpc.LoadBalancerProvider and io.grpc.NameResolverProvider from grpc-core/META-INF/services are probably not included in you final jar and this exception will occur. A work-around is to add these files to your own jar by adding them either manually or by including them in your build process. The files should be in the META-INF/services/ folder of your jar.
If you add the files to your own project, you can include them in your final jar by using a maven-shade-plugin resource transformer like this:
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>META-INF/services</resource>
<file>io.grpc.LoadBalancerProvider</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>META-INF/services</resource>
<file>io.grpc.NameResolverProvider</file>
</transformer>
Thank you for your response guys. @olavloite indeed I am shading using gradle with com.github.jengelman.gradle.plugins:shadow:5.0.0
Is this an issue that will be fixed in the future in grpc or the google-java library or do I permanently need this workaround from now on?
And with the workaround, do you mean including these files in my own project and including in them in the final jar?
- https://github.com/grpc/grpc-java/blob/master/core/src/bazel-internal/resources/META-INF/services/io.grpc.LoadBalancerProvider
- https://github.com/grpc/grpc-java/blob/master/core/src/bazel-internal/resources/META-INF/services/io.grpc.NameResolverProvider
@sceee Yes, you should include those files in the META-INF/services directory of your jar, which means that you need to add them to your own project (at least for now as a workaround). I'll have to discuss with someone else on what could be a possible long-term solution for this. @sduskis Could you point me in a direction of someone who might be able to help with that?
@olavloite alright, thank you for your help!
This problem is caused if you build a fat/über-jar that contains all your dependencies. In those cases, you also need to include all service definitions of the jars that you are packaging. The way to achieve this depends on your choice of build system.
For Maven you should use the ServicesResourceTransformer. For Gradle you can use the ServiceFileTransformer.
These plugins will collect all the service definitions of the included jars and bundle these into the META-INF/services directory of your fat/über-jar.
Hello there. I have the same error and I've applied the discussed workaround but it doesn't seem to work. Is there something else I should check? I am running the puller from a daemon thread. Do you think that can be the reason?
@mikessoldier Can you confirm that your fat jar really contains the directory structure META-INF/services at top level, and that the directory contains the files io.grpc.LoadBalancerProvider and io.grpc.NameResolverProvider?
Maybe this will help. I did a search from the list of contents of the .jar to see if those files were added.
This is the result:
$ jar tf target/MyApp.jar | grep io.grpc.LoadBalancerProvider
META-INF/services/io.grpc.LoadBalancerProvider
io/grpc/LoadBalancerProvider.class
$ jar tf target/MyApp.jar | grep io.grpc.NameResolverProvider
io/grpc/NameResolverProvider$NameResolverFactory.class
io/grpc/NameResolverProvider$NameResolverPriorityAccessor.class
io/grpc/NameResolverProvider.class
META-INF/services/io.grpc.NameResolverProvider
I would have expected that the following command:
$ jar tf target/MyApp.jar | grep io.grpc.NameResolverProvider
Would have yielded at least also the following result:
META-INF/services/io.grpc.NameResolverProvider
In other words: It looks like your build does include the io.grpc.LoadBalancerProvider in the services folder, but that the io.grpc.NameResolverProvider is not included in the services folder.
Could you copy the stack trace of the error message you are getting? I would expect your stack trace to be similar but slightly different from the one at the top of this thread.
@mikessoldier I noticed that you just added META-INF/services/io.grpc.NameResolverProvider to the results of the second search, so that shouldn't be the problem then. Could you copy the stack trace that you get?
Yes, I am sorry, I did a wrong copy of the terminal output, but the file is there. I hoped I edited the comment on time, but, It seems I was late. the output is this:
$ jar tf target/MyApp.jar | grep io.grpc.LoadBalancerProvider
META-INF/services/io.grpc.LoadBalancerProvider
io/grpc/LoadBalancerProvider.class
$ jar tf target/MyApp.jar | grep io.grpc.NameResolverProvider
io/grpc/NameResolverProvider$NameResolverFactory.class
io/grpc/NameResolverProvider$NameResolverPriorityAccessor.class
io/grpc/NameResolverProvider.class
META-INF/services/io.grpc.NameResolverProvider
And about the error I am getting, it should be this:
{
"timeStamp":"2019-03-28T09:48:21.613850Z",
"threadID":24,
"logName":"io.grpc.internal.ManagedChannelImpl",
"sourceMethodName":"uncaughtException",
"logLevel":"SEVERE",
"message":"[io.grpc.internal.ManagedChannelImpl-1] Uncaught exception in the SynchronizationContext. Panic!"
}
I cannot give you a stacktrace right now, sorry.
I made some changes to narrow the source of the error... after doing this:
try {
eventProcessorExecutor.submit(() -> {
logger.info("start MyApp !");
Subscriber subscriber = null;
try {
subscriber = myService.createSubscriber(receiver);
subscriber.startAsync();
subscriber.awaitTerminated(5000, TimeUnit.MILLISECONDS);
logger.info("started async subscriber !!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (subscriber != null) {
subscriber.stopAsync().awaitTerminated();
logger.info("stopped async subscriber !!");
}
}
});
} catch(Exception e) {
e.printStackTrace();
}
I got this:
{"timeStamp":"2019-03-28T10:45:15.842541Z","threadID":22,"logName":"com.myClassPath.MyApp","sourceMethodName":null,"logLevel":"INFO","message":"start MyApp !"}
{"timeStamp":"2019-03-28T10:45:16.349366Z","threadID":24,"logName":"io.grpc.internal.ManagedChannelImpl","sourceMethodName":"uncaughtException","logLevel":"SEVERE","message":"[io.grpc.internal.ManagedChannelImpl-1] Uncaught exception in the SynchronizationContext. Panic!"}
java.util.concurrent.TimeoutException: Timed out waiting for InnerService [RUNNING] to reach a terminal state. Current state: RUNNING
at com.google.common.util.concurrent.AbstractService.awaitTerminated(AbstractService.java:331)
at com.google.api.core.AbstractApiService.awaitTerminated(AbstractApiService.java:108)
at com.myClassPath.MyApp.lambda$start$0(MyApp.java:46)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
{"timeStamp":"2019-03-28T10:45:21.335513Z","threadID":22,"logName":"com.myClassPath.MyApp","sourceMethodName":null,"logLevel":"INFO","message":"stopped async subscriber !!"}
This error seems to happen
- at
subscriber.startAsync().awaitTerminated();whensubscriber.startAsync().awaitTerminated();happens and... - at
subscriber.stopAsync().awaitTerminated();whensubscriber.startAsync();no exception thrown in any case.
The following code:
subscriber.startAsync();
subscriber.awaitTerminated(5000, TimeUnit.MILLISECONDS);
will always end in a TimeoutException. The method subscriber.awaitTerminated(...) waits until the subscriber has been terminated (stopped), and not until it is started. I think your intention is to run this:
subscriber.startAsync();
subscriber.awaitRunning(5000, TimeUnit.MILLISECONDS);
Using .awaitRunning() stops it right away, and throws the same error.
Just to be clear, as I said before, I changed the code to try and find the source of the problem.
But the expected behavior is this:
try {
eventProcessorExecutor.submit(() -> {
logger.info("start MyApp !");
Subscriber subscriber = null;
try {
subscriber = myService.createSubscriber(receiver);
subscriber.startAsync().awaitTerminated();
logger.info("started async subscriber !!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (subscriber != null) {
subscriber.stopAsync().awaitTerminated();
logger.info("stopped async subscriber !!");
}
}
});
} catch(Exception e) {
e.printStackTrace();
}
In this example logger.info("started async subscriber !!"); never gets executed, and there is no exception to be printed or thrown.
I think that the line subscriber.startAsync().awaitTerminated(); in the example above will wait indefinitely. That's why the logging statement will never be executed, and no exception will be thrown, so you really need to change that line into something else. I'm curious what the stacktrace or the result is if you change that line into subscriber.startAsync().awaitRunning();
When I used subscriber.startAsync().awaitRunning();
I got the following results:
{"timeStamp":"2019-03-28T13:34:41.890145Z","threadID":22,"logName":"com.myClassPath.MyApp","sourceMethodName":null,"logLevel":"INFO","message":"start pubSubWorker verticle !"}
{"timeStamp":"2019-03-28T13:34:42.364381Z","threadID":24,"logName":"io.grpc.internal.ManagedChannelImpl","sourceMethodName":"uncaughtException","logLevel":"SEVERE","message":"[io.grpc.internal.ManagedChannelImpl-1] Uncaught exception in the SynchronizationContext. Panic!"}
{"timeStamp":"2019-03-28T13:34:42.375532Z","threadID":22,"logName":"com.myClassPath.MyApp","sourceMethodName":null,"logLevel":"INFO","message":"started async subscriber !!"}
{"timeStamp":"2019-03-28T13:34:42.377339Z","threadID":22,"logName":"com.myClassPath.MyApp","sourceMethodName":null,"logLevel":"INFO","message":"stopped async subscriber !!"}
As you said, my intention is that the subscriber will wait indefinitely, and it makes sense that the logging statement never gets executed, you are right.
It seemed to me that it should execute since the Panic! might have broken the waiting (but of course I don't really know that).