Grpc-java: Grpc-Java : make sure to call shutdown()/shutdownNow() and wait until awaitTermination

Created on 4 Feb 2019  路  7Comments  路  Source: grpc/grpc-java

Please answer these questions before submitting your issue.

What version of gRPC are you using?

grpc-core 1.13.1

image

What did you expect to see?

I saw lot ====> of .g.i.ManagedChannelOrphanWrapper : ~~~ Channel ManagedChannelImpl{logId=5, target=172.30.84.17:6565} was not shutdown properly!!! ~~~
Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.

java.lang.RuntimeException: ManagedChannel allocation site
at io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference.(ManagedChannelOrphanWrapper.java:103)
at io.grpc.internal.ManagedChannelOrphanWrapper.(ManagedChannelOrphanWrapper.java:53)

following each errors (StatusRuntimeException)

Client code :

ManagedChannel channel = ManagedChannelBuilder.forAddress(this.host, this.port).usePlaintext().intercept(interceptors).build();
    T t = runner.apply(stubSupplier.apply(channel).withDeadlineAfter(timeout, TimeUnit.MILLISECONDS));
    try {
      channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
      return t;
    } catch (InterruptedException e) {
      throw new IllegalStateException("Error happened during shutdown of validator gRPC channel", e);
    }

I start a new channel by request. Is-it correct ?

shutdown not done efficiently ?

question

Most helpful comment

Same here with the InProcessServerBuilder and gRPC 1.20.0

String name = UUID.randomUUID().toString();
Server server = InProcessServerBuilder.forName(name).addService(service).build();
server.start();
try {
    // do whatever....
} finally {
    server.shutdownNow().awaitTermination();
}
Channel ManagedChannelImpl{logId=44, target=directaddress:///8158b613-fec5-4656-a664-16c689b56f3b} was not shutdown properly!!! ~*~*~*
    Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
java.lang.RuntimeException: ManagedChannel allocation site
    at io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference.<init>(ManagedChannelOrphanWrapper.java:94)
    at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:52)
    at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:43)
    at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:527)

All 7 comments

This changed in recent versions of gRPC to only require shutdown(). Your code doesn't handle InterruptedException properly, and the RPC should be inside of the try block (if it throws, the channel wouldn't shutdown).

Which version ? So the rpc in the try and only Channel.shutdown(); in a finally block + interrupt flag to true if interruptedException ?? Is it right?

The PR was https://github.com/grpc/grpc-java/pull/5283 which will end up in the 1.19 release (1.18 happens next week).

So the rpc in the try and only Channel.shutdown(); in a finally block + interrupt flag to true if interruptedException ?? Is it right?

Correct.

So waiting for 1.19.. should I keep the code 'shutdown().awaitTermination(..)??

Hi.

I switch to grpc-netty and grpc-services 1.18.0.

image

But continue to have errors : java.lang.RuntimeException: ManagedChannel allocation site

I change my client code to :

 ManagedChannel channel = ManagedChannelBuilder.forAddress(this.host, this.port).usePlaintext().intercept(interceptors).build();
    try {
      return runner.apply(stubSupplier.apply(channel).withDeadlineAfter(timeout, TimeUnit.MILLISECONDS));
    } finally {
      try {
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
      } catch (InterruptedException e) {
        LOGGER.warn("Interrupted exception during gRPC channel close", e);
        Thread.currentThread().interrupt();
      }
    }

Same here with the InProcessServerBuilder and gRPC 1.20.0

String name = UUID.randomUUID().toString();
Server server = InProcessServerBuilder.forName(name).addService(service).build();
server.start();
try {
    // do whatever....
} finally {
    server.shutdownNow().awaitTermination();
}
Channel ManagedChannelImpl{logId=44, target=directaddress:///8158b613-fec5-4656-a664-16c689b56f3b} was not shutdown properly!!! ~*~*~*
    Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
java.lang.RuntimeException: ManagedChannel allocation site
    at io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference.<init>(ManagedChannelOrphanWrapper.java:94)
    at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:52)
    at io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:43)
    at io.grpc.internal.AbstractManagedChannelImplBuilder.build(AbstractManagedChannelImplBuilder.java:527)

Closing, since this should have long-ago been fixed.

@laymain, it seems your comment wasn't seen before. You're code snippet is for a Server but the exception is for a Channel, so you still may have a bug elsewhere in your code.

If anyone experiences this with gRPC v1.19 or later, then please open a new issue.

Was this page helpful?
0 / 5 - 0 ratings