google-gemini/generative-ai-android

Failed to send message

Closed this issue · 8 comments

After a request fails once, subsequent requests will fail every time.

java.util.concurrent.CancellationException: Parent job is Canceled error reported

The log is as follows:

2024-02-26 21:43:10.633 32688-2795  System.err                  W  com.google.ai.client.generativeai.type.UnknownException: Something unexpected happened.
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.type.GoogleGenerativeAIException$Companion.from(Exceptions.kt:45)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.GenerativeModel.generateContent(GenerativeModel.kt:88)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.GenerativeModel$generateContent$1.invokeSuspend(Unknown Source:15)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at android.os.Handler.handleCallback(Handler.java:888)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at android.os.Handler.dispatchMessage(Handler.java:100)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at android.os.Looper.loop(Looper.java:213)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at android.app.ActivityThread.main(ActivityThread.java:8178)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at java.lang.reflect.Method.invoke(Native Method)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
2024-02-26 21:43:10.634 32688-2795  System.err                  W  Caused by: java.util.concurrent.CancellationException: Parent job is Cancelled
2024-02-26 21:43:10.634 32688-2795  System.err                  W  	at io.ktor.client.engine.UtilsKt$attachToUserJob$cleanupHandler$1.invoke(Utils.kt:97)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.engine.UtilsKt$attachToUserJob$cleanupHandler$1.invoke(Utils.kt:95)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at kotlinx.coroutines.JobSupport.invokeOnCompletion(JobSupport.kt:1529)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at kotlinx.coroutines.Job$DefaultImpls.invokeOnCompletion$default(Job.kt:357)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.engine.HttpClientEngineKt.createCallContext(HttpClientEngine.kt:166)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.engine.HttpClientEngine$DefaultImpls.executeWithinCallContext(HttpClientEngine.kt:91)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.engine.HttpClientEngine$DefaultImpls.access$executeWithinCallContext(HttpClientEngine.kt:24)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.engine.HttpClientEngine$install$1.invokeSuspend(HttpClientEngine.kt:70)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.engine.HttpClientEngine$install$1.invoke(Unknown Source:15)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.engine.HttpClientEngine$install$1.invoke(Unknown Source:4)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.execute$ktor_utils(SuspendFunctionGun.kt:98)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpSend$DefaultSender.execute(HttpSend.kt:138)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpTimeout$Plugin$install$1.invokeSuspend(HttpTimeout.kt:174)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpTimeout$Plugin$install$1.invoke(Unknown Source:15)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpTimeout$Plugin$install$1.invoke(Unknown Source:6)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpSend$InterceptedSender.execute(HttpSend.kt:116)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpRedirect$Plugin$install$1.invokeSuspend(HttpRedirect.kt:64)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpRedirect$Plugin$install$1.invoke(Unknown Source:15)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpRedirect$Plugin$install$1.invoke(Unknown Source:6)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpSend$InterceptedSender.execute(HttpSend.kt:116)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpCallValidator$Companion$install$3.invokeSuspend(HttpCallValidator.kt:151)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpCallValidator$Companion$install$3.invoke(Unknown Source:13)
2024-02-26 21:43:10.635 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpCallValidator$Companion$install$3.invoke(Unknown Source:6)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpSend$InterceptedSender.execute(HttpSend.kt:116)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpSend$Plugin$install$1.invokeSuspend(HttpSend.kt:104)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpSend$Plugin$install$1.invoke(Unknown Source:15)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpSend$Plugin$install$1.invoke(Unknown Source:4)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:88)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.DefaultTransformKt$defaultTransformers$1.invokeSuspend(DefaultTransform.kt:57)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.DefaultTransformKt$defaultTransformers$1.invoke(Unknown Source:11)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.DefaultTransformKt$defaultTransformers$1.invoke(Unknown Source:4)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:88)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install$1.invokeSuspend(ContentNegotiation.kt:252)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install$1.invoke(Unknown Source:11)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.contentnegotiation.ContentNegotiation$Plugin$install$1.invoke(Unknown Source:4)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:88)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:130)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(Unknown Source:13)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(Unknown Source:4)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invokeSuspend(HttpRequestLifecycle.kt:38)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(Unknown Source:11)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(Unknown Source:4)
2024-02-26 21:43:10.636 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at io.ktor.util.pipeline.SuspendFunctionGun.execute$ktor_utils(SuspendFunctionGun.kt:98)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at io.ktor.client.HttpClient.execute$ktor_client_core(HttpClient.kt:191)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:108)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:47)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at io.ktor.client.statement.HttpStatement.execute(HttpStatement.kt:62)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.internal.api.APIController.generateContent(APIController.kt:185)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.GenerativeModel.generateContent(GenerativeModel.kt:86)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.Chat.sendMessage(Chat.kt:60)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.java.ChatFutures$FuturesImpl$sendMessage$1.invokeSuspend(ChatFutures.kt:54)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.java.ChatFutures$FuturesImpl$sendMessage$1.invoke(Unknown Source:8)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.java.ChatFutures$FuturesImpl$sendMessage$1.invoke(Unknown Source:4)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at kotlinx.coroutines.intrinsics.UndispatchedKt.startCoroutineUndispatched(Undispatched.kt:44)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:112)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at kotlinx.coroutines.BuildersKt__Builders_commonKt.async(Builders.common.kt:91)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at kotlinx.coroutines.BuildersKt.async(Unknown Source:1)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at androidx.concurrent.futures.SuspendToFutureAdapter.launchFuture(SuspendToFutureAdapter.kt:86)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at androidx.concurrent.futures.SuspendToFutureAdapter.launchFuture$default(SuspendToFutureAdapter.kt:81)
2024-02-26 21:43:10.637 32688-2795  System.err                  W  	at com.google.ai.client.generativeai.java.ChatFutures$FuturesImpl.sendMessage(ChatFutures.kt:54)

What happened to cause the original request to fail, and how was it handled?

There are cases when a context may stop existing or get canceled if an exception is thrown (and not caught) in that context. That may be whats happening here, but its difficult to tell without more information.

Can you create a minimal reproduction case?

case 1:

  1. disconnect network, then sendMessage, report javax.net.ssl.SSLHandshakeException: Connection closed by peer error
  2. connect network, then sendMessage, report java.util.concurrent.CancellationException: Parent job is Cancelled error

case 2:

  1. Switch to a network that does not support the region, then sendMessage, report ServerException: User location is not supported for the API use error
  2. Switch to a network that support the region, then sendMessage, report java.util.concurrent.CancellationException: Parent job is Cancelled error

Tips: The application cannot be restarted and will be restored after restarting.

In addition, I am using the Java language. After I changed it to the Kotlin language, there was no problem.

Here is the code

 private void sendMessage(String message, List<Content> history) {
        GenerativeModel gm = new GenerativeModel("gemini-pro", "xxxxxxxx");
        GenerativeModelFutures model = GenerativeModelFutures.from(gm);

        ChatFutures chat = model.startChat(history);

        Content.Builder userMessage = new Content.Builder();
        userMessage.setRole("user");
        userMessage.addText(message);
        ListenableFuture<GenerateContentResponse> response = chat.sendMessage(userMessage.build());
        Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
            @Override
            public void onSuccess(GenerateContentResponse result) {
                String resultText = result.getText();
                .....
            }

            @Override
            public void onFailure(@NonNull Throwable t) {
                t.printStackTrace();
            }
        }, Executor..);
    }

I was unable to reproduce this from the details provided. Can you provide a more complete example?

Specifically, I think what kind of executor you are using here, and how, is very relevant.

It sounds like you have an android app you are using to generate this failure, can you post a link to the app repo? Or create a minimal example of this failure?

Actually, after a bit more effort, I am able to reproduce this issue. Investigating further.

Edit: A workaround for now may be to use the kotlin API, or sendMessageStream in the java API.

gemini
For my code also , i am facing the same issue. sometimes it is telling no result continually. If i restart the app then only it start working. log history look like this : com.google.ai.client.generativeai.type.UnknownException: Something unexpected happened.
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at com.google.ai.client.generativeai.type.GoogleGenerativeAIException$Companion.from(Exceptions.kt:42)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at com.google.ai.client.generativeai.GenerativeModel.generateContent(GenerativeModel.kt:77)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at com.google.ai.client.generativeai.GenerativeModel$generateContent$1.invokeSuspend(Unknown Source:15)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at android.os.Handler.handleCallback(Handler.java:938)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at android.os.Handler.dispatchMessage(Handler.java:99)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at android.os.Looper.loop(Looper.java:263)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at android.app.ActivityThread.main(ActivityThread.java:8276)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at java.lang.reflect.Method.invoke(Native Method)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
2024-01-13 13:47:12.879 31218-12347 System.err in.shgtechnologies.svgpro W Caused by: java.util.concurrent.CancellationException: Parent job is Cancelled

Hi, our next release will fix this issue, however, for now if you're experiencing it, you should be able to resolve it in just your own project by manually adding a dependency on the recently released

implementation("androidx.concurrent:concurrent-futures-ktx:1.2.0-alpha03")