google / java-photoslibrary

Java client library for the Google Photos Library API
http://developers.google.com/photos
Apache License 2.0
107 stars 64 forks source link

Exception when creating client #23

Closed hurricup closed 3 years ago

hurricup commented 4 years ago

Working on desktop application working with google photos. Sometimes having an exception:

Oct 24, 2019 5:22:06 PM io.grpc.internal.ManagedChannelOrphanWrapper$ManagedChannelReference cleanQueue
SEVERE: *~*~*~ Channel ManagedChannelImpl{logId=17, target=photoslibrary.googleapis.com:443} 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:514)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:223)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:164)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:156)
    at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:157)
    at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:122)
    at com.google.photos.library.v1.upload.PhotosLibraryUploadStubImpl.<init>(PhotosLibraryUploadStubImpl.java:35)
    at com.google.photos.library.v1.upload.PhotosLibraryUploadStubImpl.createStub(PhotosLibraryUploadStubImpl.java:44)
    at com.google.photos.library.v1.PhotosLibraryClient.<init>(PhotosLibraryClient.java:103)
    at com.google.photos.library.v1.PhotosLibraryClient.initialize(PhotosLibraryClient.java:114)
    at org.hurricup.google.PhotosLibraryClientFactory.createClient(PhotosLibraryClientFactory.java:64)
katrielalex commented 4 years ago

Apparently this is an issue in gax-java https://github.com/googleapis/google-cloud-java/issues/3912 (fixed in release 1.36.0: https://github.com/googleapis/gax-java/compare/v1.35.1...v1.36.0), but this package uses 1.45.0 so I'm not sure why we're still seeing it.

ylexus commented 4 years ago

@katrielalex actually I think think this happens because PhotosLibraryClient, after creating PhotosLibraryUploadStub via PhotosLibraryUploadStubImpl.createStub(settings), never closes it. The PhotosLibraryStub does get closed via InternalPhotosLibraryClient#close(), but not the PhotosLibraryUploadStub.

mixin82 commented 3 years ago

That's correct @ylexus. The issue manifests itself after the GC collects ManagedChannelOrphanWrapper instances and we then want to create or close another connection. Here's a way to reproduce this issue:

        PhotosLibraryClient client1, client2;

        client1 = PhotosLibraryClientFactory.createClient(secret_file_path, REQUIRED_SCOPES);
        client1.close();

        // let the GC dispose of ManagedChannelOrphanWrapper instances
        client1 = null;
        System.gc();
        Thread.sleep(1000);

        // Issue #23
        client2 = PhotosLibraryClientFactory.createClient(secret_file_path, REQUIRED_SCOPES);

I think it's the same as issue #33. Here's a slightly different way to reproduce that:

        PhotosLibraryClient client1, client2;

        client1 = PhotosLibraryClientFactory.createClient(secret_file_path, REQUIRED_SCOPES);
        client1.close();
        client2 = PhotosLibraryClientFactory.createClient(secret_file_path, REQUIRED_SCOPES);

        // let the GC dispose of ManagedChannelOrphanWrapper instances
        client1 = null;
        System.gc();
        Thread.sleep(1000);

        // Issue #33
        client2.close();

I did a change to close the uploadStub when PhotosLibraryClient closes and that should resolve both issues.

jfschmakeit commented 3 years ago

Just following up in the pull request before we can merge, but thanks for the investigation and the pull request @mixin82 !