Aws-sdk-android: Sign in issue in AmazonKinesisVideoDemoApp

Created on 13 May 2019  路  10Comments  路  Source: aws-amplify/aws-sdk-android

There is an Exception and app crashes after leaving the app alone for about 1 hour.

Full details and steps for reproducing the issue is below. It is reproducible on multiple device types, recent tested device is OnePlus6+.

  1. Follow the https://github.com/awslabs/aws-sdk-android-samples/blob/master/AmazonKinesisVideoDemoApp/README.md to set up the Cognito.
  2. Sign in the app and close it.
  3. Wait for ~1 hour and open the app again. Exception pops up and app crashes.
  4. Exception shows as com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException: Failed to authenticate user, but when I put a debug point here, its cause is android.os.NetworkOnMainThreadException.

Stacktrace in the Console:

W/AWSMobileClient: signalTokensNotAvailable
W/AWSMobileClient: Tokens are invalid, please sign-in again.
                   java.lang.Exception: No cached session.
                       at com.amazonaws.mobile.client.AWSMobileClient$6$1.signalTokensNotAvailable(AWSMobileClient.java:1037)
                       at com.amazonaws.mobile.client.AWSMobileClient$6$1.onFailure(AWSMobileClient.java:1032)
                       at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getSession(CognitoUser.java:751)
                       at com.amazonaws.mobile.client.AWSMobileClient$6.run(AWSMobileClient.java:999)
                       at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)
                       at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:973)
                       at com.amazonaws.mobile.client.AWSMobileClient.getUserStateDetails(AWSMobileClient.java:655)
                       at com.amazonaws.mobile.client.AWSMobileClient.isSignedIn(AWSMobileClient.java:552)
                       at com.amazonaws.kinesisvideo.demoapp.activity.StartUpActivity.onCreate(StartUpActivity.java:23)
                       at android.app.Activity.performCreate(Activity.java:7149)
                       at android.app.Activity.performCreate(Activity.java:7140)
                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3017)
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172)
                       at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4920)
                       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4829)
                       at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
                       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
                       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
                       at android.os.Handler.dispatchMessage(Handler.java:106)
                       at android.os.Looper.loop(Looper.java:193)
                       at android.app.ActivityThread.main(ActivityThread.java:6863)
                       at java.lang.reflect.Method.invoke(Native Method)
                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
                    Caused by: com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoInternalErrorException: Failed to authenticate user
                       at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getCachedSession(CognitoUser.java:859)
                       at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getSession(CognitoUser.java:742)
                       at com.amazonaws.mobile.client.AWSMobileClient$6.run(AWSMobileClient.java:999)聽
                       at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)聽
                       at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:973)聽
                       at com.amazonaws.mobile.client.AWSMobileClient.getUserStateDetails(AWSMobileClient.java:655)聽
                       at com.amazonaws.mobile.client.AWSMobileClient.isSignedIn(AWSMobileClient.java:552)聽
                       at com.amazonaws.kinesisvideo.demoapp.activity.StartUpActivity.onCreate(StartUpActivity.java:23)聽
                       at android.app.Activity.performCreate(Activity.java:7149)聽
                       at android.app.Activity.performCreate(Activity.java:7140)聽
                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)聽
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3017)聽
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3172)聽
                       at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4920)聽
                       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4829)聽
                       at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)聽
                       at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)聽
                       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)聽
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)聽
                       at android.os.Handler.dispatchMessage(Handler.java:106)聽
                       at android.os.Looper.loop(Looper.java:193)聽
                       at android.app.ActivityThread.main(ActivityThread.java:6863)聽
                       at java.lang.reflect.Method.invoke(Native Method)聽
                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)聽
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)聽
                    Caused by: android.os.NetworkOnMainThreadException
                       at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
                       at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
                       at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
                       at java.net.InetAddress.getAllByName(InetAddress.java:1154)
                       at com.android.okhttp.Dns$1.lookup(Dns.java:39)
                       at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
                       at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
                       at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
                       at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
                       at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
                       at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
                       at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
                       at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
                       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
                       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
                       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
W/AWSMobileClient:     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
                       at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:26)
                       at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:162)
                       at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:75)
                       at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:371)
                       at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:212)
                       at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.invoke(AmazonCognitoIdentityProviderClient.java:5296)
                       at com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient.initiateAuth(AmazonCognitoIdentityProviderClient.java:3581)
                       at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.refreshSession(CognitoUser.java:2241)
                       at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getCachedSession(CognitoUser.java:852)
                        ... 24 more
AWSMobileClient Bug

All 10 comments

Hello @zhiyua-git

The NetworkOnMainThreadException would happen if you are calling the getTokens API on the main thread of your app? Is that the case?

I think the SDK could do better by surfacing the right exception. I will mark that part as a bug.

Thanks,
Rohan

Hi @rohandubal ,

I am not modifying any code in AmazonKinesisVideoDemoApp and I don't see any getTokens() called in AmazonKinesisVideoDemoApp project. Can you suggest where do you think that getTokens() is called from?

Thanks,
Zhiyuan

Any suggestion?

I have a similar stack trace on Sign out

Steps to reproduce:

  1. AWSMobileClient.signin(..)
  2. AppSync make subscription
  3. AppSync mutate call
  4. AWSMobileClient.signout(..)

Actual result:
App Crashed with stack trace above

Expected result:
Not so many crashes inside SDK...

Stack trace

2019-06-26 14:50:20.516 9021-10883/? D/RetryInterceptor: Retry Interceptor called
2019-06-26 14:50:20.516 9021-10883/? D/AppSyncSigV4SignerInterceptor: Signer Interceptor called
2019-06-26 14:50:20.543 9021-10883/? D/AWSMobileClient: Inspecting user state details
2019-06-26 14:50:20.543 9021-10800/? D/CognitoCachingCredentialsProvider: Clearing credentials from SharedPreferences
2019-06-26 14:50:20.544 9021-10800/? D/CognitoCachingCredentialsProvider: Identity id is changed
2019-06-26 14:50:20.544 9021-10800/? D/CognitoCachingCredentialsProvider: Saving identity id to SharedPreferences
2019-06-26 14:50:20.545 9021-10800/? D/CognitoCachingCredentialsProvider: Clearing credentials from SharedPreferences
2019-06-26 14:50:20.563 9021-10883/? W/AWSMobileClient: signalTokensNotAvailable
2019-06-26 14:50:20.565 9021-10800/? D/IdentityManager: Signing out...
2019-06-26 14:50:20.565 9021-10883/? W/AWSMobileClient: Tokens are invalid, please sign-in again.
    java.lang.Exception: No cached session.
        at com.amazonaws.mobile.client.AWSMobileClient$9$1.signalTokensNotAvailable(AWSMobileClient.java:1623)
        at com.amazonaws.mobile.client.AWSMobileClient$9$1.getAuthenticationDetails(AWSMobileClient.java:1603)
        at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getSession(CognitoUser.java:751)
        at com.amazonaws.mobile.client.AWSMobileClient$9.run(AWSMobileClient.java:1585)
        at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)
        at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:1551)
        at com.amazonaws.mobile.client.AWSMobileClient.getUserStateDetails(AWSMobileClient.java:1029)
        at com.amazonaws.mobile.client.AWSMobileClient.waitForSignIn(AWSMobileClient.java:907)
        at com.amazonaws.mobile.client.AWSMobileClient.getCredentials(AWSMobileClient.java:359)
        at com.amazonaws.mobileconnectors.appsync.sigv4.AppSyncSigV4SignerInterceptor.intercept(AppSyncSigV4SignerInterceptor.java:131)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at com.amazonaws.mobileconnectors.appsync.retry.RetryInterceptor.intercept(RetryInterceptor.java:40)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)
2019-06-26 14:50:20.565 9021-10883/? D/AWSMobileClient: waitForSignIn: userState:SIGNED_OUT_USER_POOLS_TOKENS_INVALID

@zhiyua-git Sorry for the delayed response. Your call to getTokens() is coming from isSignedIn(). Can you move that call to a background thread and wait until the operation is done to check it?

@anry200 The error you are experiencing is due to the fact that the user is signed out and the credentials are cleared on the device, however there are AppSync operations that are invoked after the user is signed out, so there are no valid credentials available to execute the AppSync operations.

You need to make sure all AppSync operations that require UserPools authorization are completed before the user is signed out.

same issues

fixed please chnage code from StartupActivity.java

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                if (auth.isSignedIn()) {
                    auth.getTokens(new Callback<Tokens>() {
                        @Override
                        public void onResult(Tokens result) {
                            ActivityUtils.startActivity(thisActivity, SimpleNavActivity.class);
                        }

                        @Override
                        public void onError(Exception e) {

                        }
                    });

                } else {
                    auth.showSignIn(thisActivity,
                            SignInUIOptions.builder()
                                    .nextActivity(SimpleNavActivity.class)
                                    .build(),
                            new Callback<UserStateDetails>() {
                                @Override
                                public void onResult(UserStateDetails result) {
                                    Log.d(TAG, "onResult: User signed-in " + result.getUserState());
                                }

                                @Override
                                public void onError(final Exception e) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Log.e(TAG, "onError: User sign-in error", e);
                                            Toast.makeText(StartUpActivity.this, "User sign-in error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }
                            });
                }
            }
        });

I have similar stacktrace as @anry200 had but without calling sign out:
Once some (not all) users update the App from Play Store and tries to open the it this issue happens.
It is important to mention that I don't call any getToken. The AppSync calls tries to get the token but it seems that they fail.

03-29 14:48:34.379   972  1379 V WindowManager: Checking 1 opening apps (frozen=false timeout=false)...
03-29 14:48:34.379   972  1379 V WindowManager: Check opening app=AppWindowToken{9d54053 token=Token{5a13842 ActivityRecord{7696e8d u0 com.aladdinmarket.aladdin.beta/com.aladdinmarket.aladdin.MainActivity t11}}}: allDrawn=false startingDisplayed=false startingMoved=false isRelaunching()=false
03-29 14:48:34.379 11112 11150 W AWSMobileClient: signalTokensNotAvailable
03-29 14:48:34.380 11112 11150 W AWSMobileClient: Tokens are invalid, please sign-in again.
03-29 14:48:34.380 11112 11150 W AWSMobileClient: java.lang.Exception: No cached session.
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobile.client.AWSMobileClient$11$1.signalTokensNotAvailable(AWSMobileClient.java:1756)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobile.client.AWSMobileClient$11$1.getAuthenticationDetails(AWSMobileClient.java:1736)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobileconnectors.cognitoidentityprovider.CognitoUser.getSession(CognitoUser.java:778)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobile.client.AWSMobileClient$11.run(AWSMobileClient.java:1718)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobile.client.internal.InternalCallback.await(InternalCallback.java:115)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobile.client.AWSMobileClient.getTokens(AWSMobileClient.java:1684)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobile.client.AWSMobileClient.getUserStateDetails(AWSMobileClient.java:1024)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobile.client.AWSMobileClient.waitForSignIn(AWSMobileClient.java:903)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobile.client.AWSMobileClient.getCredentials(AWSMobileClient.java:355)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobileconnectors.appsync.sigv4.AppSyncSigV4SignerInterceptor.intercept(AppSyncSigV4SignerInterceptor.java:131)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at com.amazonaws.mobileconnectors.appsync.retry.RetryInterceptor.intercept(RetryInterceptor.java:40)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
03-29 14:48:34.380 11112 11150 W AWSMobileClient:   at java.lang.Thread.run(Thread.java:784)
03-29 14:48:34.380 11112 11150 D AWSMobileClient: waitForSignIn: userState:SIGNED_OUT_USER_POOLS_TOKENS_INVALID

I use: com.amazonaws:aws-android-sdk-mobile-client:2.16.11

I was also working with AmazonKinesisVideoDemoApp and had similar issue of app crashing on relaunching it.

But this fixed the issue. Thanks @faisalvv.

fixed please chnage code from StartupActivity.java

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                if (auth.isSignedIn()) {
                    auth.getTokens(new Callback<Tokens>() {
                        @Override
                        public void onResult(Tokens result) {
                            ActivityUtils.startActivity(thisActivity, SimpleNavActivity.class);
                        }

                        @Override
                        public void onError(Exception e) {

                        }
                    });

                } else {
                    auth.showSignIn(thisActivity,
                            SignInUIOptions.builder()
                                    .nextActivity(SimpleNavActivity.class)
                                    .build(),
                            new Callback<UserStateDetails>() {
                                @Override
                                public void onResult(UserStateDetails result) {
                                    Log.d(TAG, "onResult: User signed-in " + result.getUserState());
                                }

                                @Override
                                public void onError(final Exception e) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Log.e(TAG, "onError: User sign-in error", e);
                                            Toast.makeText(StartUpActivity.this, "User sign-in error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }
                            });
                }
            }
        });
Was this page helpful?
0 / 5 - 0 ratings