aws-amplify / amplify-android

The fastest and easiest way to use AWS from your Android app.
https://docs.amplify.aws/lib/q/platform/android/
Apache License 2.0
237 stars 111 forks source link

Amplify configuration shared preferences are corrupted #2863

Open dnyaneshEuphotic opened 4 days ago

dnyaneshEuphotic commented 4 days ago

Before opening, please confirm:

Language and Async Model

Kotlin, Kotlin - Coroutines

Amplify Categories

Authentication

Gradle script dependencies

```groovy // Put output below this line ```

Environment information

``` # Put output below this line ```

Please include any relevant guides or documentation you're referencing

No response

Describe the bug

When our android device shutdown abruptly during authentication. It causing amplify credential shared preferences to get corrupted: Below are the logs: 2024-06-28 20:01:25.135 12564-12564 App com.euphotic.sbcApp I Usb initialized 2024-06-28 20:01:25.137 12564-12564 start com.euphotic.sbcApp I Device is not locked 2024-06-28 20:01:25.139 12564-12564 App com.euphotic.sbcApp I isregistrationchecked is false 2024-06-28 20:01:25.139 12564-12564 start com.euphotic.sbcApp D db file not found 2024-06-28 20:01:25.140 12564-12564 DB_deug com.euphotic.sbcApp D isDeviceRegistered: OA 2024-06-28 20:01:25.141 12564-12564 DB_debug com.euphotic.sbcApp D getDBInstance: DI 2024-06-28 20:01:25.141 12564-12564 DB_debug com.euphotic.sbcApp D buildNitriteDB: DI 2024-06-28 20:01:25.141 12564-12564 DB_debug com.euphotic.sbcApp D buildNitriteDB: try 2024-06-28 20:01:25.262 12564-12596 AndroidRuntime com.euphotic.sbcApp E FATAL EXCEPTION: DefaultDispatcher-worker-3 Process: com.euphotic.sbcApp, PID: 12564 java.lang.IllegalArgumentException: File com.amplify.credentialStore.+������ 5�cFL�C/�і���mn94�-�W��.xml contains a path separator at android.app.ContextImpl.makeFilename(ContextImpl.java:3263) at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:915) at android.app.ContextImpl.deleteSharedPreferences(ContextImpl.java:669) at android.content.ContextWrapper.deleteSharedPreferences(ContextWrapper.java:218) at com.amplifyframework.core.store.EncryptedKeyValueRepository.deleteSharedPreferences(EncryptedKeyValueRepository.kt:118) at com.amplifyframework.core.store.EncryptedKeyValueRepository.openKeystoreWithDefaultMasterKey(EncryptedKeyValueRepository.kt:98) at com.amplifyframework.core.store.EncryptedKeyValueRepository.getOrCreateSharedPreferences(EncryptedKeyValueRepository.kt:67) at com.amplifyframework.core.store.EncryptedKeyValueRepository.access$getOrCreateSharedPreferences(EncryptedKeyValueRepository.kt:32) at com.amplifyframework.core.store.EncryptedKeyValueRepository$sharedPreferences$2.invoke(EncryptedKeyValueRepository.kt:48) at com.amplifyframework.core.store.EncryptedKeyValueRepository$sharedPreferences$2.invoke(EncryptedKeyValueRepository.kt:48) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at com.amplifyframework.core.store.EncryptedKeyValueRepository.getSharedPreferences(EncryptedKeyValueRepository.kt:48) at com.amplifyframework.core.store.EncryptedKeyValueRepository.get(EncryptedKeyValueRepository.kt:51) at com.amplifyframework.auth.cognito.data.AWSCognitoAuthCredentialStore.retrieveCredential(AWSCognitoAuthCredentialStore.kt:62) at com.amplifyframework.auth.cognito.actions.CredentialStoreCognitoActions$loadCredentialStoreAction$$inlined$invoke$1.execute(Action.kt:70) at com.amplifyframework.statemachine.ConcurrentEffectExecutor$execute$1$1.invokeSuspend(ConcurrentEffectExecutor.kt:26) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:585) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:802) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:706) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:693) Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@296787f, Dispatchers.Default] 2024-06-28 20:01:25.346 450-1345 BufferQueueDebug surfaceflinger E Application Error: com.euphotic.sbcApp#0 id info cannot be read from 'Application Error: com.euphotic.sbcApp#0' 2024-06-28 20:01:25.941 12564-12564 DB_debug com.euphotic.sbcApp D buildNitriteDB: returning 2024-06-28 20:01:26.245 12564-12564 App com.euphotic.sbcApp I repo size is 0 2024-06-28 20:01:26.246 12564-12564 App com.euphotic.sbcApp I isregistered is false 2024-06-28 20:01:26.254 12564-12564 App com.euphotic.sbcApp I Showing first fragment 2024-06-28 20:01:26.267 12564-12564 App com.euphotic.sbcApp I On Welcome fragment 2024-06-28 20:01:26.267 12564-12564 App com.euphotic.sbcApp I Finished wait

How to handle this exception. How can we clear the particular shared preference key.

Reproduction steps (if applicable)

  1. Create an android kotlin app with cognito auth
  2. while signing-in, power off the android device such that it shutsdown immediately.
  3. Happens after a couple of retries.

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line 2024-06-28 20:01:25.084 12564-12600 FirebaseCrashlytics com.euphotic.sbcApp W Unable to read App Quality Sessions session id. 2024-06-28 20:01:25.089 12564-12600 FirebaseCrashlytics com.euphotic.sbcApp W Could not synthesize final report file for /data/user/0/com.euphotic.sbcApp/files/.com.google.firebase.crashlytics.files.v2:com.euphotic.sbcApp/open-sessions/667EC93A01FE000131148AF91A0812DB/report java.io.FileNotFoundException: /data/user/0/com.euphotic.sbcApp/files/.com.google.firebase.crashlytics.files.v2:com.euphotic.sbcApp/open-sessions/667EC93A01FE000131148AF91A0812DB/report: open failed: ENOENT (No such file or directory) at libcore.io.IoBridge.open(IoBridge.java:575) at java.io.FileInputStream.(FileInputStream.java:160) at com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence.readTextFile(CrashlyticsReportPersistence.java:431) at com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence.synthesizeReportFile(CrashlyticsReportPersistence.java:356) at com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence.synthesizeReport(CrashlyticsReportPersistence.java:322) at com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence.finalizeReports(CrashlyticsReportPersistence.java:195) at com.google.firebase.crashlytics.internal.common.SessionReportingCoordinator.finalizeSessions(SessionReportingCoordinator.java:194) at com.google.firebase.crashlytics.internal.common.CrashlyticsController.doCloseSessions(CrashlyticsController.java:631) at com.google.firebase.crashlytics.internal.common.CrashlyticsController.doCloseSessions(CrashlyticsController.java:585) at com.google.firebase.crashlytics.internal.common.CrashlyticsController$2.call(CrashlyticsController.java:218) at com.google.firebase.crashlytics.internal.common.CrashlyticsController$2.call(CrashlyticsController.java:199) at com.google.firebase.crashlytics.internal.common.CrashlyticsBackgroundWorker$3.then(CrashlyticsBackgroundWorker.java:105) at com.google.android.gms.tasks.zze.run(com.google.android.gms:play-services-tasks@@18.1.0:1) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at com.google.firebase.crashlytics.internal.common.ExecutorUtils$1$1.onRun(ExecutorUtils.java:67) at com.google.firebase.crashlytics.internal.common.BackgroundPriorityRunnable.run(BackgroundPriorityRunnable.java:27) at java.lang.Thread.run(Thread.java:920) Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Linux.open(Native Method) at libcore.io.ForwardingOs.open(ForwardingOs.java:567) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:273) at libcore.io.ForwardingOs.open(ForwardingOs.java:567) at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7765) at libcore.io.IoBridge.open(IoBridge.java:561) at java.io.FileInputStream.(FileInputStream.java:160)  at com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence.readTextFile(CrashlyticsReportPersistence.java:431)  at com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence.synthesizeReportFile(CrashlyticsReportPersistence.java:356)  at com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence.synthesizeReport(CrashlyticsReportPersistence.java:322)  at com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence.finalizeReports(CrashlyticsReportPersistence.java:195)  at com.google.firebase.crashlytics.internal.common.SessionReportingCoordinator.finalizeSessions(SessionReportingCoordinator.java:194)  at com.google.firebase.crashlytics.internal.common.CrashlyticsController.doCloseSessions(CrashlyticsController.java:631)  at com.google.firebase.crashlytics.internal.common.CrashlyticsController.doCloseSessions(CrashlyticsController.java:585)  at com.google.firebase.crashlytics.internal.common.CrashlyticsController$2.call(CrashlyticsController.java:218)  at com.google.firebase.crashlytics.internal.common.CrashlyticsController$2.call(CrashlyticsController.java:199)  at com.google.firebase.crashlytics.internal.common.CrashlyticsBackgroundWorker$3.then(CrashlyticsBackgroundWorker.java:105)  at com.google.android.gms.tasks.zze.run(com.google.android.gms:play-services-tasks@@18.1.0:1)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)  at com.google.firebase.crashlytics.internal.common.ExecutorUtils$1$1.onRun(ExecutorUtils.java:67)  at com.google.firebase.crashlytics.internal.common.BackgroundPriorityRunnable.run(BackgroundPriorityRunnable.java:27)  at java.lang.Thread.run(Thread.java:920)  2024-06-28 20:01:25.114 12564-12590 FA com.euphotic.sbcApp I Tag Manager is not found and thus will not be used 2024-06-28 20:01:25.119 12564-12590 GooglePlayServicesUtil com.euphotic.sbcApp W Google Play services is missing. 2024-06-28 20:01:25.120 12564-12597 SessionLifecycleClient com.euphotic.sbcApp D Queued message 1. Queue size 1 2024-06-28 20:01:25.135 12564-12564 usbp com.euphotic.sbcApp D Device {pid=29987,did=1002} already has permission 2024-06-28 20:01:25.135 12564-12564 App com.euphotic.sbcApp I Usb initialized 2024-06-28 20:01:25.137 12564-12564 start com.euphotic.sbcApp I Device is not locked 2024-06-28 20:01:25.139 12564-12564 App com.euphotic.sbcApp I isregistrationchecked is false 2024-06-28 20:01:25.139 12564-12564 start com.euphotic.sbcApp D db file not found 2024-06-28 20:01:25.140 12564-12564 DB_deug com.euphotic.sbcApp D isDeviceRegistered: OA 2024-06-28 20:01:25.141 12564-12564 DB_debug com.euphotic.sbcApp D getDBInstance: DI 2024-06-28 20:01:25.141 12564-12564 DB_debug com.euphotic.sbcApp D buildNitriteDB: DI 2024-06-28 20:01:25.141 12564-12564 DB_debug com.euphotic.sbcApp D buildNitriteDB: try 2024-06-28 20:01:25.262 12564-12596 AndroidRuntime com.euphotic.sbcApp E FATAL EXCEPTION: DefaultDispatcher-worker-3 Process: com.euphotic.sbcApp, PID: 12564 java.lang.IllegalArgumentException: File com.amplify.credentialStore.+������ 5�cFL�C/�і���mn94�-�W��.xml contains a path separator at android.app.ContextImpl.makeFilename(ContextImpl.java:3263) at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:915) at android.app.ContextImpl.deleteSharedPreferences(ContextImpl.java:669) at android.content.ContextWrapper.deleteSharedPreferences(ContextWrapper.java:218) at com.amplifyframework.core.store.EncryptedKeyValueRepository.deleteSharedPreferences(EncryptedKeyValueRepository.kt:118) at com.amplifyframework.core.store.EncryptedKeyValueRepository.openKeystoreWithDefaultMasterKey(EncryptedKeyValueRepository.kt:98) at com.amplifyframework.core.store.EncryptedKeyValueRepository.getOrCreateSharedPreferences(EncryptedKeyValueRepository.kt:67) at com.amplifyframework.core.store.EncryptedKeyValueRepository.access$getOrCreateSharedPreferences(EncryptedKeyValueRepository.kt:32) at com.amplifyframework.core.store.EncryptedKeyValueRepository$sharedPreferences$2.invoke(EncryptedKeyValueRepository.kt:48) at com.amplifyframework.core.store.EncryptedKeyValueRepository$sharedPreferences$2.invoke(EncryptedKeyValueRepository.kt:48) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at com.amplifyframework.core.store.EncryptedKeyValueRepository.getSharedPreferences(EncryptedKeyValueRepository.kt:48) at com.amplifyframework.core.store.EncryptedKeyValueRepository.get(EncryptedKeyValueRepository.kt:51) at com.amplifyframework.auth.cognito.data.AWSCognitoAuthCredentialStore.retrieveCredential(AWSCognitoAuthCredentialStore.kt:62) at com.amplifyframework.auth.cognito.actions.CredentialStoreCognitoActions$loadCredentialStoreAction$$inlined$invoke$1.execute(Action.kt:70) at com.amplifyframework.statemachine.ConcurrentEffectExecutor$execute$1$1.invokeSuspend(ConcurrentEffectExecutor.kt:26) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:585) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:802) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:706) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:693) Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@296787f, Dispatchers.Default] 2024-06-28 20:01:25.346 450-1345 BufferQueueDebug surfaceflinger E [Application Error: com.euphotic.sbcApp#0](this:0xb400007bec6e8f58,id:-1,api:0,p:-1,c:-1) id info cannot be read from 'Application Error: com.euphotic.sbcApp#0' 2024-06-28 20:01:25.941 12564-12564 DB_debug com.euphotic.sbcApp D buildNitriteDB: returning 2024-06-28 20:01:26.245 12564-12564 App com.euphotic.sbcApp I repo size is 0 2024-06-28 20:01:26.246 12564-12564 App com.euphotic.sbcApp I isregistered is false 2024-06-28 20:01:26.254 12564-12564 App com.euphotic.sbcApp I Showing first fragment 2024-06-28 20:01:26.267 12564 ``` ### amplifyconfiguration.json { "UserAgent": "aws-amplify-cli/2.0", "Version": "1.0", "api": { "plugins": { "awsAPIPlugin": { "nosh": { "endpointType": "REST", "endpoint": "https://**********************.amazonaws.com/dev", "region": "ap-south-1", "authorizationType": "NONE" }, "api": { "endpointType": "REST", "endpoint": "https://****************************amazonaws.com/dev", "region": "ap-south-1", "authorizationType": "AMAZON_COGNITO_USER_POOLS" }, "nosh_otp_dev": { "endpointType": "REST", "endpoint": "https://**********************.amazonaws.com/dev", "region": "ap-south-1", "authorizationType": "AMAZON_COGNITO_USER_POOLS" } } } }, "auth": { "plugins": { "awsCognitoAuthPlugin": { "UserAgent": "aws-amplify-cli/0.1.0", "Version": "0.1.0", "IdentityManager": { "Default": {} }, "CredentialsProvider": { "CognitoIdentity": { "Default": { "PoolId": "ap-south-1:5503a4f0-cfba-4a39-a9ca-06ef9c43b28f", "Region": "ap-south-1" } } }, "CognitoUserPool": { "Default": { "PoolId": "ap-south-1_jvyEjtkKf", "AppClientId": "*********************", "Region": "ap-south-1" } }, "Auth": { "Default": { "authenticationFlowType": "CUSTOM_AUTH" } } } } } } ### GraphQL Schema
```graphql // Put your schema below this line ```
### Additional information and screenshots _No response_
tylerjroach commented 4 days ago

Hi @dnyaneshEuphotic,

This is definitely unexpected to see.

Amplify creates an installation identifier on the device in the application's sandbox (data/data//noBackup/com.amplify.credentialStore.installationIdentifier

This file simply stores a randomly generated UIUD. This UUID is appended to the shared preference file com.amplify.credentialStore.<UUID>.

I'm unsure how this has happened but the noBackup/com.amplify.credentialStore.installationIdentifier file appears to be corrupted and does not contain a UUID as expected.

This will reset the Amplify Auth state on the device, but this should hopefully be resolvable by deleting the installationIdentifier file.

File(applicationContext.noBackupFilesDir, "com.amplify.credentialStore.installationIdentifier").apply { 
    delete()
}

This would need to be deleted before calling Amplify.configure.

I would love to hear more about how often you are seeing this issue. Has this been seen on a single device or is this something you are running to often? We have not received any other reports of this happening.

dnyaneshEuphotic commented 1 day ago

Hi @tylerjroach,

Thank you for your response.

Could you help us identify where we can catch this exception?

try { Amplify.addPlugin(AWSApiPlugin()) Amplify.addPlugin(AWSCognitoAuthPlugin()) Amplify.configure(applicationContext) } catch (e: Exception) { e.printStackTrace() }

If this exception occurs, it doesn't seem to be caught in the catch block.

tylerjroach commented 1 day ago

This appears to be an edge case that hasn't been accounted for and is not catchable at the moment.

Can you please provide more information on how many times you have seen this crash occur. I don't quite understand how the library is even getting into this point.

com.amplify.credentialStore.�+�������5�c�FL�C/��і���mn94�-�W��.xml contains a path separator is not appended with a valid UUID, so even in this case while signing-in, power off the android device such that it shutsdown immediately., I don't see how the device is ending up with a filename that was never used in the first place. Creating that identifier file is a 1-time effort that happens on configuration (not signing in). A device restart during sign in should have no impact on that file.

For your reproduction steps, can you replicate this issue on multiple device types or just a single device? How many times have you seen this issue happen? Are you seeing customers experience this issue in production and at what rate?