Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash in Android BillingClient with coroutines

I'm getting notified that my Billing solution is crashing in a weird way. I'm unable to reproduce it or find a fix/bypass the problem. Maybe you could help.

Fatal Exception: java.lang.IllegalStateException: Already resumed at kotlin.coroutines.SafeContinuation.resumeWith + 45(SafeContinuation.java:45) at com.android.billingclient.api.BillingClientKotlinKt$querySkuDetails$2$1.onSkuDetailsResponse + 2(BillingClientKotlinKt.java:2) at com.android.billingclient.api.zzj.run + 8(zzj.java:8) at android.os.Handler.handleCallback + 907(Handler.java:907) at android.os.Handler.dispatchMessage + 105(Handler.java:105) at android.os.Looper.loop + 216(Looper.java:216) at android.app.ActivityThread.main + 7625(ActivityThread.java:7625) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 524(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main + 987(ZygoteInit.java:987)

//billing
implementation 'com.android.billingclient:billing:2.2.0'
implementation 'com.android.billingclient:billing-ktx:2.1.0'
like image 354
Cativail Avatar asked Oct 15 '22 04:10

Cativail


1 Answers

Since last week we have this code running to prevent the exception:

suspend fun BillingClient.querySkuDetailsFixed(params: SkuDetailsParams) = suspendCancellableCoroutine<SkuDetailsResult> { continuation ->
querySkuDetailsAsync(params) { billingResult, skuDetails: List<SkuDetails>? ->
    if (continuation.isActive) {
        // doing some logging
        continuation.resumeWith(Result.success(SkuDetailsResult(billingResult, skuDetails)))
    } else {
      // Already resumed, doing some logging
    }
}
}

In the logs we see that we get 2 calls from the library:

The first response has always the BillingResponseCode -3 SERVICE_TIMEOUT.

The second response often has either 6 ERROR or 2 SERVICE_UNAVAILABLE.

In our case this is happening when the app is awaken in the background for a PeriodicWorkRequest.

like image 68
3dmg Avatar answered Oct 27 '22 08:10

3dmg