aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.31k stars 247 forks source link

issue with "User-Agent was already configured successfully" #5244

Open mobiletoly opened 2 months ago

mobiletoly commented 2 months ago

Description

This issue is related to amplify_datastore.dart file.

in method:

@override
  Future<void> configure({
    AmplifyOutputs? config,
    required AmplifyAuthProviderRepository authProviderRepo,
  }) async {
...
}

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),
        );
    ...

so this line Map<String, String>.from(e.details as Map) throws error (type 'Null' is not a subtype of type 'Map<dynamic, dynamic>' in type cast) because e.details can be null.

in my case PlatformException contains this information

PlatformException(
  AmplifyException, 
  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.},
  null,
  null
)

(as you can see e.details field is null there)

I would suggest to ignore this "User-Agent was already configured successfully" error, because for example in my case I'm initializing Amplify from the main UI thread, but there is also a possibility that some separate isolate is launched and it tries to initialize Amplify as well (have to deal with background services). Since there is no way to detect from one isolate that Amplify was already configured in another isolate - I end up having this issue.

Categories

Steps to Reproduce

  1. Register Amplify with DataStore plugin in main isolate
  2. Register Amplify with DataStore plugin in different isolate

Screenshots

No response

Platforms

Flutter Version

3.24.0-0.2.pre

Amplify Flutter Version

3.2.0

Deployment Method

Amplify CLI

Schema

No response

tyllark commented 2 months ago

Hi @mobiletoly, thanks for taking the time to submit this issue. We will investigate this isolate configuration behavior and update you as we can.

tyllark commented 2 months ago

Hi @mobiletoly, I wanted to note that we do not officially support Amplify in multiple isolates, but I was able to reproduce the issue you mentioned. We should not be throwing the type 'Null' is not a subtype of type 'Map<dynamic, dynamic>' in type cast exception. There is already logic in place to silently log AmplifyAlreadyConfiguredExceptions, but it is not being hit due to additional exception handling downstream returning an unexpected exception that we do not handle properly. I'm marking this as a bug and we will get back to you when we have a fix.