Repository navigation
Bad state: Future already completed #195
Description
Activity
I reported this issue a while ago and opened a pull request to fix this issue. flutter/flutter#38060
Reacted by Marius Höfler and Daniel Vestergaard@earyzhe Is it possible that due to state changes in Flutter the code you sent gets executed twice shortly after each other? Or you call
RemoteConfig.instancesomewhere else at the same time?This is what happened to me.
@kroikie @collinjackson There is a test to check for this use case: doubleInstance
What I've done:
- Clone
flutterfire - Checkout
firebase_remote_config-v0.3.0+1 - Run
cd packages/firebase_remote_config - Run
flutter pub get - Run test
flutter test --plain-name doubleInstance test/firebase_remote_config_test.dart - See test fail with error:
Test result
> flutter test --plain-name doubleInstance test/firebase_remote_config_test.dart 00:03 +0 -1: RemoteConfig doubleInstance [E] Bad state: Future already completed dart:async _AsyncCompleter.complete package:firebase_remote_config/src/remote_config.dart 36:26 RemoteConfig.instance ===== asynchronous gap =========================== dart:async Future.wait test/firebase_remote_config_test.dart 62:14 main.<fn>.<fn> dart:async _AsyncAwaitCompleter.start test/firebase_remote_config_test.dart 57:28 main.<fn>.<fn> 00:03 +0 -1: Some tests failed.
Interestingly, if I run all tests using:
flutter test test/firebase_remote_config_test.darteverything works as expected.From my understanding of the code, this can be explained like this:
- Test instance starts to run to checks if the instance can be created.
1.1. Test callsRemoteConfig.instance.
1.2. By calling [1.1] the_instanceCompleteris now complete
1.3. Because the_instanceCompleterproperty is static the state is preserved across tests and still is completed when... - Test doubleInstance starts
2.1. WhenRemoteConfig.instancegets called twice the both won't go into this line which causes the error as the_instanceCompleteris already completed from the previous test.
2.2. None of the calls throw theStateErrorwhich occurs if the first two calls toRemoteConfig.instancehappen at the same time.
From that, I went to make the tests independent from each other:
First I made_instanceCompletervisible for testing like this:@visibleForTesting static Completer<RemoteConfig> instanceCompleter = Completer<RemoteConfig>();
And added a teardown call to reset the
instanceCompleterto the default value:tearDown(() { RemoteConfig.instanceCompleter = Completer<RemoteConfig>(); });
Now the
doubleInstancetest fails as expected.To fix the error itself, I added used try/catch around the
instanceCompleter.complete()call and only ignoreStateErrors with the messageFuture already completed.I hope this is an acceptable solution to this problem.
I opened up a pull request to fix this bug and make the test behave as expected. #2061
Reacted by Marius Höfler, Mayur Dhurpate and Daniel Vestergaard- Clone
- addedimpact: customerA bug with low impact (e.g. affecting only a few customers or has a workaround). (P3)A bug with low impact (e.g. affecting only a few customers or has a workaround). (P3)
on Apr 20, 2020 I'm having this issue outside test environment.
await RemoteConfig.instanceis throwingBad State: Future already completedalways.
My code snippet:remoteConfig = await RemoteConfig.instance; await remoteConfig.fetch(expiration: const Duration(hours: 0)); await remoteConfig.activateFetched();My workaround was just to add try+catch and keep the flow going when the error is BadState: Future already completed
flutter doctor -v:
[✓] Flutter (Channel stable, v1.17.5, on Linux, locale pt_BR.UTF-8)
• Flutter version 1.17.5 at /home/leonardo/Documentos/devTools/flutter
• Framework revision 8af6b2f038 (5 weeks ago), 2020-06-30 12:53:55 -0700
• Engine revision ee76268252
• Dart version 2.8.4[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at /home/leonardo/Android/Sdk
• Platform android-29, build-tools 29.0.3
• Java binary at: /opt/android-studio/jre/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.[✓] Android Studio (version 4.0)
• Android Studio at /opt/android-studio
• Flutter plugin version 47.1.2
• Dart plugin version 193.7361
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)[✓] Connected device (1 available)
• moto g 7 play • 0056621193 • android-arm • Android 10 (API 29)Reacted by Dheeraj AvvariThis issue seems to being fixed soon, due to a fundamental rework of the Remote Config plugin (#4186) by @kroikie 😍
PR is now ready for review 👍Will test if it actually fixes the issue, but as the underlying code responsible for this bug was replaced with a completely rewritten synchronous (rather than asynchronous) implementation it would be unlogical if it didn't 😂
@earyzhe there is now a pre-release version available for Remote Config which should solve this issue:
https://pub.dev/packages/firebase_remote_config/versions#prereleaseSo I think this issue could be closed now :)
- locked and limited conversation to collaborators
on Apr 22, 2021
When initilising remote config this error is thrown.
The code
The Error
Bad state: Future already completed
Output Flutter -v: