Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Channel ManagedChannelImpl was not shut down properly

If I run following these two tests I get the error.

1st test

@Rule
public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();

@Test
public void findAll() throws Exception {
    // Generate a unique in-process server name.
    String serverName = InProcessServerBuilder.generateName();

    // Create a server, add service, start, and register for automatic graceful shutdown.
    grpcCleanup.register(InProcessServerBuilder
            .forName(serverName)
            .directExecutor()
            .addService(new Data(mockMongoDatabase))
            .build()
            .start());

    // Create a client channel and register for automatic graceful shutdown.
    RoleServiceGrpc.RoleServiceBlockingStub stub = RoleServiceGrpc.newBlockingStub(
            grpcCleanup.register(InProcessChannelBuilder
                    .forName(serverName)
                    .directExecutor()
                    .build()));

    RoleOuter.Response response = stub.findAll(Empty.getDefaultInstance());
    assertNotNull(response);
}

2nd test

@Test
public void testFindAll() {
    ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8081)
            .usePlaintext()
            .build();

    RoleServiceGrpc.RoleServiceBlockingStub stub = RoleServiceGrpc.newBlockingStub(channel);
    RoleOuter.Response response = stub.findAll(Empty.newBuilder().build());
    assertNotNull(response);
}

io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference cleanQueue SEVERE: ~~~ Channel ManagedChannelImpl{logId=1, target=localhost:8081} 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:94)

If I comment out one of them, then no errors, unit tests pass though but the exception is thrown if both are ran together.

Edit

Based on the suggestion.

@Test
public void testFindAll() {
    ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8081)
            .usePlaintext()
            .build();

    RoleServiceGrpc.RoleServiceBlockingStub stub = RoleServiceGrpc.newBlockingStub(channel);
    RoleOuter.Response response = stub.findAll(Empty.newBuilder().build());
    assertNotNull(response);

    channel.shutdown();
}
like image 685
AppDeveloper Avatar asked Aug 13 '19 16:08

AppDeveloper


1 Answers

Hey I just faced similar issue using Dialogflow V2 Java SDK where I received the error

 Oct 19, 2019 4:12:23 PM io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference cleanQueue
SEVERE: *~*~*~ Channel ManagedChannelImpl{logId=41, target=dialogflow.googleapis.com:443} was not shutdown properly!!! ~*~*~*
    Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.

Also, Having a huge customer base we started running into out of memory unable to create native thread error.

After performing a lot of Debugging operations and Using Visual VM Thread Monitoring I finally figured out that the problem was because of SessionsClient not closing. So I used the attached code block to solve that issue. Post testing that block I was finally able to free up all the used threads and also the error mentioned earlier was resolved.

SessionsClient sessionsClient = null;
QueryResult queryResult = null;

try {
    SessionsSettings.Builder settingsBuilder = SessionsSettings.newBuilder();
    SessionsSettings sessionsSettings = settingsBuilder
            .setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();
    sessionsClient = SessionsClient.create(sessionsSettings);
    SessionName session = SessionName.of(projectId, senderId);
    com.google.cloud.dialogflow.v2.TextInput.Builder textInput = TextInput.newBuilder().setText(message)
            .setLanguageCode(languageCode);
    QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();

    DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput);

    queryResult = response.getQueryResult();
} catch (Exception e) {
    e.printStackTrace();
}
finally {
    sessionsClient.close();
}

The shorter values on the graph highlights the use of client.close(). Without that the threads were stuck in Parking State.

like image 189
Ayyub Kolsawala Avatar answered Oct 18 '22 01:10

Ayyub Kolsawala