Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amplify Fails to Configure - WorkerBeeExceptionImpl #5190

Closed
2 of 14 tasks
CarlosEspinozaMaruri opened this issue Jul 22, 2024 · 11 comments
Closed
2 of 14 tasks

Amplify Fails to Configure - WorkerBeeExceptionImpl #5190

CarlosEspinozaMaruri opened this issue Jul 22, 2024 · 11 comments
Assignees
Labels
bug Something is not working; the issue has reproducible steps and has been reproduced pending-release Issues that have been addressed in main but have not been released Secure Storage Issues related to the secure_storage package windows

Comments

@CarlosEspinozaMaruri
Copy link

CarlosEspinozaMaruri commented Jul 22, 2024

Description

This issue is a duplicate of this that was closed due to lack of information. I have managed to obtain the requested JSON files and attach them below.


When attempting to configure Amplify using Amplify.configure(), the following exception is being thrown:

Exception:

 WorkerBeeExceptionImpl {
  error=FormatException: Unexpected character (at character 27907)
...33,74,41,5,53,129,103,142,83,81,166,215,198,74,136,96,91,72,189,196]},196]}
                                                                        ^
,
  stackTrace=#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1380)
#1      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:882)
#2      _parseJson (dart:convert-patch/convert_patch.dart:35)
#3      JsonDecoder.convert (dart:convert/json.dart:610)
#4      JsonCodec.decode (dart:convert/json.dart:216)
#5      FileKeyValueStore.readAll (package:amplify_secure_storage_dart/src/utils/file_key_value_store.dart:96)
<asynchronous suspension>
#6      FileKeyValueStore.readKey (package:amplify_secure_storage_dart/src/utils/file_key_value_store.dart:70)
<asynchronous suspension>
#7      AmplifySecureStorageWindows.read (package:amplify_secure_storage_dart/src/platforms/amplify_secure_storage_windows.dart:45)
<asynchronous suspension>
#8      SecureStorageWorker.run (package:amplify_secure_storage_dart/src/worker/secure_storage_worker.dart:53)
<asynchronous suspension>
#9      _run (package:amplify_secure_storage_dart/src/worker/secure_storage_worker.worker.vm.dart:14)
<asynchronous suspension>

Stacktrace:

dart:convert-patch/convert_patch.dart 1383                                                 _ChunkedJsonParser.fail
dart:convert-patch/convert_patch.dart 913                                                  _ChunkedJsonParser.parse
dart:convert-patch/convert_patch.dart 35                                                   _parseJson
dart:convert/json.dart 610                                                                 JsonDecoder.convert
dart:convert/json.dart 216                                                                 JsonCodec.decode
package:amplify_secure_storage_dart/src/utils/file_key_value_store.dart 96                 FileKeyValueStore.readAll
package:amplify_secure_storage_dart/src/utils/file_key_value_store.dart 70                 FileKeyValueStore.readKey
package:amplify_secure_storage_dart/src/platforms/amplify_secure_storage_windows.dart 45   AmplifySecureStorageWindows.read
package:amplify_secure_storage_dart/src/worker/secure_storage_worker.dart 56               SecureStorageWorker.run
package:amplify_secure_storage_dart/src/worker/secure_storage_worker.worker.vm.dart 14     _run
package:amplify_core/src/state_machine/event.dart 43                                       new EventCompleter
package:amplify_core/src/state_machine/state_machine.dart 179                              StateMachineManager.dispatch
package:amplify_core/src/state_machine/state_machine.dart 32                               Dispatcher.dispatchAndComplete
package:amplify_core/src/state_machine/state_machine.dart 190                              StateMachineManager.dispatchAndComplete
package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart 73                  CognitoAuthStateMachine.loadCredentials
package:amplify_auth_cognito_dart/src/state/machines/configuration_state_machine.dart 108  ConfigurationStateMachine.onConfigure
package:amplify_auth_cognito_dart/src/state/machines/configuration_state_machine.dart 50   ConfigurationStateMachine.resolve
package:amplify_core/src/state_machine/state_machine.dart 255                              StateMachine._listenForEvents.<fn>.<fn>
dart:async/future.dart 427                                                                 new Future.delayed.<fn>
dart:async/zone.dart 1391                                                                  _rootRun
dart:async/zone.dart 1301                                                                  _CustomZone.run
dart:async/zone.dart 1209                                                                  _CustomZone.runGuarded
dart:async/zone.dart 1249                                                                  _CustomZone.bindCallbackGuarded.<fn>
dart:async/zone.dart 1399                                                                  _rootRun
dart:async/zone.dart 1301                                                                  _CustomZone.run
dart:async/zone.dart 1233                                                                  _CustomZone.bindCallback.<fn>
dart:async-patch/timer_patch.dart 18                                                       Timer._createTimer.<fn>
dart:isolate-patch/timer_impl.dart 398                                                     _Timer._runTimers
dart:isolate-patch/timer_impl.dart 429                                                     _Timer._handleMessage
dart:isolate-patch/isolate_patch.dart 189                                                  _RawReceivePort._handleMessage
===== asynchronous gap ===========================
package:amplify_core/src/state_machine/event.dart 43                                       new EventCompleter
package:amplify_core/src/state_machine/state_machine.dart 150                              StateMachineManager.accept
package:amplify_core/src/state_machine/state_machine.dart 162                              StateMachineManager.acceptAndComplete
package:amplify_auth_cognito_dart/src/auth_plugin_impl.dart 236                            AmplifyAuthCognitoDart.configure
package:amplify_auth_cognito/src/auth_plugin_impl.dart 84                                  AmplifyAuthCognito.configure
dart:async/future.dart 525                                                                 Future.wait.<fn>
package:amplify_core/src/amplify_class_impl.dart 77                                        AmplifyClassImpl.configurePlatform
package:amplify_core/src/amplify_class.dart 113                                            AmplifyClass.configure
package:app/di/locator.dart 119                                                      configureAmplify
package:app/di/locator.dart 34                                                       initializeDI
package:app/main.dart 18                                                             main.<fn>

Corrupted json files:

{
"region:uid.accessKeyId":[56,193,24]}21,66],
"region:uid.sessionToken":[90,143,237],
"region:uid.expiration":[173,25,117]}5,101]
}
{
  "version": [1, 2, 3],
  "app_cliend_id.asfDeviceId": [1, 2, 3],
  "app_cliend_id.accessToken": [1, 2, 3],
  "app_cliend_id.refreshToken": [1, 2, 3],
  "app_cliend_id.idToken": [1, 2, 3],
  "app_cliend_id.username": [1, 0, 0],
  "app_cliend_id.authFlowType": [1, 2, 3],
  "region:uid:cognito_identity_pool_id.identityId": [1, 2, 3],
  "region:uid:cognito_identity_pool_id.accessKeyId": [1, 2, 4],
  "region:uid:cognito_identity_pool_id.secretAccessKey": [1, 2, 3],
  "region:uid:cognito_identity_pool_id.sessionToken": [1, 2, 3],
  "region:uid:cognito_identity_pool_id.expiration": [1, 2, 3]
},196]}

This seems to only happen on Windows devices. I have successfully replicated the error in production on 4 devices belonging to different clients. Despite efforts, I have been unable to reproduce the error in our development environment. However, it has come to my attention that this error occurs in installations approximately 2 or more days old. Furthermore, it is noteworthy that reinstalling the application from the Windows Store resolves the issue, suggesting potential file corruption.

Notes:

  • The application retrieves Amplify configuration settings through the amplifyconfiguration.dart file, utilizing methods such as
    String.fromEnvironment('variable_name') to access private keys in environment variables.
  • Devices are in the correct time zone.
  • In Android and iOS works as expected.
  • There are Windows devices (v. 10 and 11) in which the error does not occur.

Categories

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

Steps to Reproduce

configureAmplify() async {
  await amplify_flutter.Amplify.addPlugins(
    [
      pinpoint.AmplifyAnalyticsPinpoint(),
      auth_cognito.AmplifyAuthCognito(),
    ],
  );
  try {
    await amplify_flutter.Amplify.configure(amplifyconfig);
    final session = await amplify_flutter.Amplify.Auth
        .getPlugin(auth_cognito.AmplifyAuthCognito.pluginKey)
        .fetchAuthSession();
  } on amplify_flutter.AmplifyException catch (e, stackTrace) {
    logging.exception(
      e.message,
      exception: e,
      stackTrace: stackTrace,
    );
  } on Exception catch (e, stackTrace) {
    logging.exception(
      e.toString(),
      exception: e,
      stackTrace: stackTrace,
    );
  }
}

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.22.2

Amplify Flutter Version

2.1.0

Deployment Method

Amplify CLI

Schema

No response

@Jordan-Nelson Jordan-Nelson added pending-triage This issue is in the backlog of issues to triage Secure Storage Issues related to the secure_storage package windows labels Jul 22, 2024
@ftauro082799
Copy link

ftauro082799 commented Jul 22, 2024

@CarlosEspinozaMaruri I found this thread from another package

grpc/grpc-dart#697

The solution was described:

_[ Service ] Include drive letter in path when launching DDS snapshot

The previous logic for building the path to dds.dart.snapshot would result
in the Windows drive letter being dropped from the path:

\path\to\dart-sdk\bin\dds.dart.snapshot

This works most of the time since a leading slash is treated as a reference
to the current drive, which often contains the Dart SDK. However, if the SDK
is on a different drive than the current (e.g., in a container with two drives),
the VM will fail to find the snapshot.

This change uses the File(...) APIs from dart:io to build the path rather than
trying to use the Uri class to manually hack together a path.

TEST=N/A, not reproducible without a second Windows drive_

@Jordan-Nelson
Copy link
Member

@CarlosEspinozaMaruri - Thanks for providing the additional info. The json file appears to be mostly valid, but there are additional characters at the end of the file. The additional characters look like they are json fragments. It isn't clear to me how this would occur. One thought I had was that multiple simultaneous writes may somehow result in a fragment being written the the file, although if that were the case I would think that would be a bug in the Dart File APIs. I am going to try to see if I can reproduce this on a windows device though.

@ftauro082799 - I don't think the issue you linked is related. It happens to be the same exception (FormatException: Unexpected character), but this could occur for many reasons. Let me know if I am missing something. If you are also experiencing this issue, sharing the exception/stacktrace/logs along with any reproduction in you have would be helpful.

@ftauro082799
Copy link

@Jordan-Nelson I figured, but it was one of the few other threads I found with this issue so figured I would link if it was helpful.

I am experiencing this issue as well. Here is a timeline of what I found:

  1. On the original Windows PC (windows 11, x64) Amplify 2.0.0 was used
  2. Moved to new PC and setup environment for Flutter development
  3. Cloned repo, began developing, experienced BSOD (Friday, related to crowdstrike issue but may be coincidence with this issue)
  4. Shut down and restarted PC
  5. Upon restart, the following error occurs in my AuthService class after successfully building:

flutter: [I] AuthService, configureCognitoPlugin, amplify plugins added
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: WorkerBeeExceptionImpl {
error=FormatException: Unexpected character (at character 1)
,
stackTrace=#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1380:5)
#1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1247:9)
#2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:912:22)
#3 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#4 JsonDecoder.convert (dart:convert/json.dart:610:36)
#5 JsonCodec.decode (dart:convert/json.dart:216:41)
#6 FileKeyValueStore.readAll (package:amplify_secure_storage_dart/src/utils/file_key_value_store.dart:96:35)

#7 FileKeyValueStore.readKey (package:amplify_secure_storage_dart/src/utils/file_key_value_store.dart:70:18)

#8 AmplifySecureStorageWindows.read (package:amplify_secure_storage_dart/src/platforms/amplify_secure_storage_windows.dart:45:21)

#9 SecureStorageWorker.run (package:amplify_secure_storage_dart/src/worker/secure_storage_worker.dart:53:25)

#10 _run (package:amplify_secure_storage_dart/src/worker/secure_storage_worker.worker.vm.dart:14:18)

,
}
dart:convert-patch/convert_patch.dart 1380:5 _ChunkedJsonParser.fail
dart:convert-patch/convert_patch.dart 1247:9 _ChunkedJsonParser.parseNumber
dart:convert-patch/convert_patch.dart 912:22 _ChunkedJsonParser.parse
dart:convert-patch/convert_patch.dart 35:10 _parseJson
dart:convert/json.dart 610:36 JsonDecoder.convert
dart:convert/json.dart 216:41 JsonCodec.decode
package:amplify_secure_storage_dart/src/utils/file_key_value_store.dart 96:35 FileKeyValueStore.readAll
===== asynchronous gap ===========================
package:amplify_secure_storage_dart/src/utils/file_key_value_store.dart 70:18 FileKeyValueStore.readKey
===== asynchronous gap ===========================
package:amplify_secure_storage_dart/src/platforms/amplify_secure_storage_windows.dart 45:21 AmplifySecureStorageWindows.read
===== asynchronous gap ===========================
package:amplify_secure_storage_dart/src/worker/secure_storage_worker.dart 53:25 SecureStorageWorker.run
===== asynchronous gap ===========================
package:amplify_secure_storage_dart/src/worker/secure_storage_worker.worker.vm.dart 14:18 _run
===== asynchronous gap ===========================
package:amplify_core/src/state_machine/event.dart 39:47 new EventCompleter
package:amplify_core/src/state_machine/state_machine.dart 186:19 StateMachineManager.dispatch
package:amplify_core/src/state_machine/state_machine.dart 32:23 Dispatcher.dispatchAndComplete
package:amplify_core/src/state_machine/state_machine.dart 197:13 StateMachineManager.dispatchAndComplete
package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart 77:36 CognitoAuthStateMachine.loadCredentials
package:amplify_auth_cognito_dart/src/state/machines/configuration_state_machine.dart 108:25 ConfigurationStateMachine.onConfigure
package:amplify_auth_cognito_dart/src/state/machines/configuration_state_machine.dart 50:15 ConfigurationStateMachine.resolve
package:amplify_core/src/state_machine/state_machine.dart 262:59 StateMachine._listenForEvents..
dart:async/future.dart 423:39 new Future.delayed.
dart:async-patch/timer_patch.dart 18:15 Timer._createTimer.
dart:isolate-patch/timer_impl.dart 398:19 _Timer._runTimers
dart:isolate-patch/timer_impl.dart 429:5 _Timer._handleMessage
dart:isolate-patch/isolate_patch.dart 184:12 _RawReceivePort._handleMessage
===== asynchronous gap ===========================
package:amplify_core/src/state_machine/event.dart 39:47 new EventCompleter
package:amplify_core/src/state_machine/state_machine.dart 157:23 StateMachineManager.accept
package:amplify_core/src/state_machine/state_machine.dart 169:23 StateMachineManager.acceptAndComplete
package:amplify_auth_cognito_dart/src/auth_plugin_impl.dart 244:25 AmplifyAuthCognitoDart.configure
package:amplify_auth_cognito/src/auth_plugin_impl.dart 88:5 AmplifyAuthCognito.configure
dart:async/future.dart 524:21 Future.wait.
package:amplify_core/src/amplify_class.dart 199:7 AmplifyClass._configurePlugins

***Note: Even when building my app on the original PC, which works fine, I cannot run the compiled app on my new PC where this error is occurring

This error occurred upon calling this line. The json encode is NOT the part causing issues

  await Amplify.configure(json.encode(amplifyConfig));

This is the amplify config being encoded and sent in:

{
"UserAgent": "aws-amplify-cli/2.0",
"Version": "1.0",
"auth": {
"plugins": {
"awsCognitoAuthPlugin": {
"UserAgent": "aws-amplify-cli/0.1.0",
"Version": "0.1.0",
"IdentityManager": {"Default": {}},
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {"PoolId": "", "Region": ""}
}
},
"CognitoUserPool": {
"Default": {"PoolId": "", "AppClientId": "", "Region": ""}
},
"Auth": {
"Default": {"authenticationFlowType": ""}
}
}
}
}
}

Within the configure function of the AmplifyClass, I found that the error occurs in the _configurePlugins function:

  await _configurePlugins(amplifyOutputs);

Specifically, where the plugin.configure is called during the mapping:

  for (final category in sortedCategories) {
      final plugins = categories[category]!.plugins;
      await Future.wait(
        eagerError: true,
        plugins.map(
          (plugin) => plugin.configure(
            config: config,
            authProviderRepo: authProviderRepo,
          ),
        ),
      );
    }

My configuration which is being sent into the for loop above is shown below (with sensitive info being replaced with x's, of course):

Config is AmplifyOutputs {
"version": "x",
"auth": {
"aws_region": "xxxx",
"user_pool_id": "xxxxxx",
"user_pool_client_id": "xxxxxxx",
"identity_pool_id": "xxxxx",
"unauthenticated_identities_enabled": true
}
}

I only have one plugin which is being configured, and it fails during the first loop as plugin.configure is called. After fully reinstalling both the dart and flutter sdk, and verifying that everything else on the new PC is set up properly, the error continues to occur.

@Jordan-Nelson Jordan-Nelson added bug Something is not working; the issue has reproducible steps and has been reproduced and removed pending-triage This issue is in the backlog of issues to triage labels Jul 23, 2024
@Jordan-Nelson
Copy link
Member

@CarlosEspinozaMaruri I believe I was able to reproduce the issue your are experiencing. Based on the discussion on dart-lang/sdk#36087 it seems that parallel unawaited calls to File APIs in not safe (at least on Windows).

I have raised a PR to address this. I will let you know when it is released.

@pieterbergmans
Copy link

@Jordan-Nelson - could you explain why this happens on some Windows PCs on not others?

@Jordan-Nelson
Copy link
Member

Jordan-Nelson commented Jul 24, 2024

@pieterbergmans I do not know that this is only reproducible on some Windows machines. It may be reproducible on all Windows machines. Have you been able to consistently reproduce this on some machines and not others? If so, can you share the reproduction steps you are following?

To reproduce this consistently I had to write a test that performs 1,000 parallel operations. With 100 parallel operations it was only reproducible some of the time. Secure Storage will only perform a much smaller number of parallel operations (maybe 10-20). I think this issue would rarely occur with that number of parallel ops. This would explain why this issue was not easily reproducible under normal use.

It is possible that the file system APIs behave differently on different Windows machines. Since this is a race condition, it is also possible this issue is more likely to occur on machines that process the operations either faster or slower.

@pieterbergmans
Copy link

@Jordan-Nelson - I work with @ftauro082799 so I’ve been watching all this go down. His new PC (base speed: 2.40GHz; cores: 14; L1, L2, L3 cache: 1.2MB, 11.5MB, 24.0MB) doesn’t work at all. His old PC (base speed: 2.30GHz; cores: 14; L1, L2, L3 cache: 1.2MB, 11.5MB, 24.0MB) never had an issue. I tested on a third PC (base speed: 1.10GHz; cores: 10; L1, L2, L3 cache: 928KB, 6.5MB, 12.0MB) and it worked just fine. To your point about it being a race condition and that the speed of the PC could impact it - as you can see, Fred’s new PC is the fastest and it doesn’t work. But, it doesn’t seem that much faster than his old. We’re looking forward to your PR. Thanks so much for helping us out. We’ll provide feedback after we try out your changes.

@Jordan-Nelson
Copy link
Member

I see. Thanks for that context. It is possible that you are facing a different issue, but I am not sure. You can test this PR by adding the following to your pubspec.yaml and then re-installing packages (pub get / pub upgrade)

# add after dependencies/dev_dependencies
dependency_overrides:
  amplify_secure_storage_dart:
    git:
      url: https://github.com/aws-amplify/amplify-flutter
      ref: fix/secure_storage/process-events
      path: packages/secure_storage/amplify_secure_storage_dart

@ftauro082799
Copy link

@Jordan-Nelson I just tested the PR and it seemed to have fixed my issue. I also now do not need to have the override enabled after running it once and it clearing the supposedly corrupt file. It will be interesting if it happens again. Thank you!

@Jordan-Nelson
Copy link
Member

@ftauro082799 thanks for letting me know

@Jordan-Nelson Jordan-Nelson added the pending-release Issues that have been addressed in main but have not been released label Aug 7, 2024
@NikaHsn
Copy link
Member

NikaHsn commented Aug 21, 2024

I'm going to close this issue as the fix has been released in 2.4.0 version. If you see otherwise or have additional questions, please open a new issue.

@NikaHsn NikaHsn closed this as completed Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working; the issue has reproducible steps and has been reproduced pending-release Issues that have been addressed in main but have not been released Secure Storage Issues related to the secure_storage package windows
Projects
None yet
Development

No branches or pull requests

5 participants