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

`Concurrent modification during iteration` issue in Amplify Core

Open Fleximex opened this issue 1 year ago • 3 comments

Description

In Amplify Core (state_machine/dependency_manager.dart)) the following code modifies the list that is being looped over:

void close() {
    for (final instance in _instances.values) {
        _closeIfPossible(instance);
    }
    _instances.clear();
}

This results in an error: Concurrent modification during iteration: _Map len:28

Modifying the code fixed the issue for me:

void close() {
    final instancesCopy = List.of(_instances.values);
    for (final instance in instancesCopy) {
        _closeIfPossible(instance);
    }
    _instances.clear();
}

Categories

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

Steps to Reproduce

After configuring Amplify for Cognito (Auth), call the Amplify.reset() method.

Screenshots

No response

Platforms

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

Flutter Version

3.22.2

Amplify Flutter Version

2.2.0

Deployment Method

Amplify CLI

Schema

No response

Fleximex avatar Jul 01 '24 08:07 Fleximex

Hello @Fleximex - Amplify.reset() is an internal API. It is not intended to be used outside of Amplify packages. What is your use case for calling this API?

Jordan-Nelson avatar Jul 01 '24 14:07 Jordan-Nelson

We are aware Amplify.reset() is not battle tested for production, and thus not intended for production. In our case, however, we have a multi tenant solution where, on runtime, Amplify needs to be reconfigured for a different identity and user pool (won't open a separate issue/discussion for that since amplify-flutter, amplify-swift and amplify-android all have multiple open already).

This issue though, doesn't exclusively relate to Amplify.reset(). Whether or not Amplify.reset() is used to execute the close() method, in the mentioned file. It modifies the list it is iterating over. Which should not happen regardless.

Fleximex avatar Jul 01 '24 14:07 Fleximex

The exception indicates that either addInstance or create is called at the same time as or immediately after close. I am not sure we want to allow that. It might hide other issues.

Are you calling reset() immediately after addPlugin() or configure()? Are you awaiting the calls to addPlugin() and configure()?

Jordan-Nelson avatar Jul 01 '24 15:07 Jordan-Nelson

@Fleximex Please let me know if you are awaiting the calls to addPlugin and configure. It would help if you can share the code snippet that results in this. We use Amplify.reset() in many of our integration tests I do not see this occur.

Jordan-Nelson avatar Jul 09 '24 16:07 Jordan-Nelson

@Fleximex I am going to close this out since we have not heard back. If you are still facing this issue please share the info requested above. Thanks.

Jordan-Nelson avatar Jul 24 '24 18:07 Jordan-Nelson