googleapis / google-cloud-java

Google Cloud Client Library for Java
https://cloud.google.com/java/docs/reference
Apache License 2.0
1.86k stars 1.06k forks source link

KeyManagementServiceClient | managed channel garbage collection error in java application while using google cloud kms client library #10976

Closed sudhakar6343 closed 1 week ago

sudhakar6343 commented 1 week ago

Use Case

GCP KMS java client is used in our application to integrate with gcp kms for encryption/decryption operations.

Mvn dependency

google-cloud-kms: 2.48.0

Library usage

ksm client created using "KeyManagementServiceClient client = KeyManagementServiceClient.create()". Client is created only once and used through out the application lifetime.

Steps to reproduce

  1. Create a KeyManagementServiceClient on application startup without try-resource.
  2. On application startup, receiving managed channel garbage collection error.
  3. Issue is not reproduced when KeyManagementServiceClient is sorrounded with try-resoure, but our use case have lots of encrypt/decrypt operations and so we created client only once (without try resource) and using it throughout the application lifetime

Code example

KeyManagementServiceClient client = KeyManagementServiceClient.create()

Stack trace

{"mdc":{},"timestamp":"2024-06-17 05:57:59.526","level":"ERROR","logger":"io.grpc.internal.ManagedChannelOrphanWrapper","message":"*~*~*~ Previous channel ManagedChannelImpl{logId=1, target=cloudkms.googleapis.com:443} was garbage collected without being shut down! ~*~*~*\n    Make sure to call shutdown()/shutdownNow()","exception":"\njava.lang.RuntimeException: ManagedChannel allocation site\n\tat io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference.<init>(ManagedChannelOrphanWrapper.java:102)\n\tat io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:60)\n\tat io.grpc.internal.ManagedChannelOrphanWrapper.<init>(ManagedChannelOrphanWrapper.java:51)\n\tat io.grpc.internal.ManagedChannelImplBuilder.build(ManagedChannelImplBuilder.java:672)\n\tat io.grpc.ForwardingChannelBuilder2.build(ForwardingChannelBuilder2.java:260)\n\tat com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:442)\n\tat com.google.api.gax.grpc.ChannelPool.<init>(ChannelPool.java:107)\n\tat com.google.api.gax.grpc.ChannelPool.create(ChannelPool.java:85)\n\tat com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:243)\n\tat com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:237)\n\tat com.google.api.gax.rpc.ClientContext.create(ClientContext.java:226)\n\tat com.google.cloud.kms.v1.stub.GrpcKeyManagementServiceStub.create(GrpcKeyManagementServiceStub.java:482)\n\tat com.google.cloud.kms.v1.stub.KeyManagementServiceStubSettings.createStub(KeyManagementServiceStubSettings.java:687)\n\tat com.google.cloud.kms.v1.KeyManagementServiceClient.<init>(KeyManagementServiceClient.java:783)\n\tat com.google.cloud.kms.v1.KeyManagementServiceClient.create(KeyManagementServiceClient.java:765)\n\tat com.google.cloud.kms.v1.KeyManagementServiceClient.create(KeyManagementServiceClient.java:756)

External references such as API reference guides

suztomo commented 1 week ago

Thank you for reporting issue.

lots of encrypt/decrypt operations

For us to reproduce the problem, would you describe roughly how many method call (which methods?) did you run to get the error? Also how long (X minutes or X hours?) should one expect for the error to appear?

suztomo commented 1 week ago

The code example seems lacking method calls. Would you enrich these?

sudhakar6343 commented 1 week ago

Error is on application startup. Just the client is created during the application startup using"KeyManagementServiceClient client = KeyManagementServiceClient.create()" and none of the methods in the client were called.

Seeing the error only once on application startup, after that don't see the error again.

After application is up, encrypt method was called and worked fine.

suztomo commented 1 week ago

@sudhakar6343 I'm afraid that I couldn't reproduce the error. The code just worked fine.

Screenshot 2024-06-20 at 5 38 41 PM

Would you try running the code that only has KeyManagementServiceClient client = KeyManagementServiceClient.create(); and see whether you observe the error message in your environment? This will help you to identify what's special in your case.

    public static void main(String[] args) throws Exception {
        KeyManagementServiceClient client = KeyManagementServiceClient.create();

        Thread.sleep(10000);
        System.out.println("Hello World!");
    }
suztomo commented 1 week ago

I'll close this later this week.

sudhakar6343 commented 1 week ago

Thanks for the check. It seems like something special in my case. I will try to check and investigate further.