amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

AmplifyException on Datastore re-initialization

Open veeramneni opened this issue 11 months ago • 5 comments

Description

I have Amplify setup for Auth, API and Datastore. They are configured without an issue on app launch. However, upon killing the app and relaunching, amplify configuration fails with below error.

AmplifyException{message=User-Agent was already configured successfully., cause=null, recoverySuggestion=User-Agent is configured internally during Amplify configuration. This method should not be called externally.}

I am not sure where it's picking User-agent from. I understand it is set automatically by Amplify. Amplify.isConfigured is indeed false before reconfiguration. So I know it is not configured before trying again.

Any ideas or thoughts are much appreciated

If it helps, the exception is being triggered in amplify_datastore.dart at this code

try { final nativeBridge = NativeAmplifyBridge(); await nativeBridge.configure( Amplify.version, jsonEncode(config.toJson()), ); } on PlatformException catch (e) { if (e.code == 'AmplifyException') {

    throw AmplifyException.fromMap(
      Map<String, String>.from(e.details as Map),
    );
    return;
  }

Categories

  • [ ] Analytics
  • [ ] API (REST)
  • [ ] API (GraphQL)
  • [ ] Auth
  • [ ] Authenticator
  • [X] DataStore
  • [ ] Notifications (Push)
  • [ ] Storage

Steps to Reproduce

  1. Create an Amplify flutter project with Auth, API and Datastore
  2. Configure it with Amplify.configure
  3. Kill and relaunch the app to re-configure Amplify
  4. Watch for configuration exception.

Screenshots

No response

Platforms

  • [ ] iOS
  • [X] Android
  • [ ] Web
  • [ ] macOS
  • [ ] Windows
  • [ ] Linux

Flutter Version

3.34.4

Amplify Flutter Version

2.5.0

Deployment Method

Amplify Gen 2

Schema

No response

veeramneni avatar Jan 06 '25 10:01 veeramneni

Hello @veeramneni, this sounds like a duplicate of this issue, please let me know if you agree. Internally we are not handling an exception properly, which is causing this exception to be thrown rather than logging and failing silently. We have a PR open with a fix, but we are waiting for a test to be fixed prior to merging.

tyllark avatar Jan 06 '25 23:01 tyllark

Thank's for your response. Yes, it is indeed the same issue and we can ignore this exception on our end. However, Datastore is not able to successfully start the sync after this. Even after stop, clear and start, I see hub event stuck in 'Outbox' status. I neither see any configuration error nor other event's. Am I missing something?(Auth and API are fine)

veeramneni avatar Jan 07 '25 13:01 veeramneni

DataStore's internal Auth configuration breaks with this exception. Any DataStore API calls afterwards will make the changes locally and they will remain your Outbox until you restart the app with a properly configured DataStore. If you want to verify these changes fully resolve your issue you can add a pubspec_overrides.yaml file to your project with the following content:

dependency_overrides:
  amplify_datastore:
    git:
      url: https://github.com/aws-amplify/amplify-flutter.git
      ref: fix/datastore_android_multi-configure
      path: packages/amplify_datastore
      fetch: fix/datastore_android_multi-configure

Just remember to delete these overrides afterwards!

tyllark avatar Jan 07 '25 22:01 tyllark

Thanks again! This does safely ignore exception but the exception itself is resulting when the app is restarted. So upon original launch plugin gets initialized like so:

I/amplify:flutter:datastore( 8139): Added Auth plugin I/amplify:flutter:datastore( 8139): Added API plugin I/amplify:flutter:datastore( 8139): Unhandled DataStoreHubEvent: SUCCEEDED I/amplify:flutter:datastore( 8139): com.amplifyframework.core.category.CategoryInitializationResult@417428a I/amplify:flutter:datastore( 8139): Established a new stream form flutter com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda1@a6de9d8

Once app is killed and restarted, it appears like native android is keeping part of amplify configuration alive(possibly because of background services?). So, the exception from your branch does safely ignore reconfiguration error, however Amplify Datastore is not properly configured. Is there a way to force clear any amplify configuration lingering in the background so Amplify gets re-initialized properly?

This is how the relaunch log looks like. As you can see, plugins are not initialized again and Datastore is not syncing as expected.

I/amplify:flutter:datastore( 8139): Initiated DataStore plugin I/amplify:flutter:datastore( 8139): Initiated DataStore plugin I/amplify:flutter:datastore( 8139): Established a new stream form flutter com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda1@7aa64ce

veeramneni avatar Jan 08 '25 14:01 veeramneni

For internal testing purposes we have an Amplify.reset() call, but this is not intended for customer use and I don't see any MessageChannels that would extend this reset to the Android native layer. I'm going to mark this issue as a bug since this use case was not accounted for in the provided PR. We will investigate and respond here when we have an update.

tyllark avatar Jan 08 '25 18:01 tyllark