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.32k stars 246 forks source link

Subscription item event failed with error #2614

Closed kmvignesh closed 1 year ago

kmvignesh commented 1 year ago

Description

We are getting these errors while listening the stream's onError callback when user has un-stable internet.

PlatformException(ApiException, 15ebc0d8-c4a5-4739-a54c-cc768a5569c3, {recoverySuggestion: , 
underlyingException: The operation couldn’t be completed. ( error 4.), 
message: Subscription item event failed with error}, null)

and

Error thrown PlatformException(ApiException, 25ff04f0-a986-4a52-91b3-0dd48e5a4fe8, {underlyingException: 
The operation couldn’t be completed. (AppSyncRealTimeClient.ConnectionProviderError error 3.),
 recoverySuggestion: , message: Subscription item event failed with error}, null).

We where not able to find/re-produce below error. But when we get this exception with error 5 app-sync re-subscription also not working. Only killing and relaunching the app is working.

ApiException(message: Subscription item event failed with error: Unauthorized, recoverySuggestion: , 
underlyingException: The operation couldn’t be completed. (AppSyncRealTimeClient.ConnectionProviderError error 5.))

Can anyone explain what does it mean by error 5,4 and error 3.

Categories

Steps to Reproduce

  1. Start AppSync Subscription
  2. Immediately go Offline
  3. Then come to online ( to emulate un-stable internet)

Screenshots

No response

Platforms

Android Device/Emulator API Level

No response

Environment

<Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.7, on macOS 12.6.3 21G419 darwin-arm, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.71.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

Dependencies

dependencies:
- amplify_api 0.6.10 [amplify_api_android amplify_api_ios amplify_core amplify_flutter aws_common collection flutter meta plugin_platform_interface]
- amplify_auth_cognito 0.6.10 [amplify_auth_cognito_android amplify_auth_cognito_ios amplify_core aws_common collection flutter meta plugin_platform_interface]
- amplify_flutter 0.6.10 [amplify_core amplify_datastore_plugin_interface amplify_flutter_android amplify_flutter_ios aws_common collection flutter meta plugin_platform_interface]
- async 2.10.0 [collection meta]
- auto_size_text 3.0.0 [flutter]
- camera 0.10.0+4 [camera_android camera_avfoundation camera_platform_interface camera_web flutter flutter_plugin_android_lifecycle quiver]
- characters 1.2.1
- charts_flutter 0.12.0 [charts_common collection flutter intl logging meta]
- collection 1.17.0
- connectivity_plus 3.0.2 [flutter flutter_web_plugins connectivity_plus_platform_interface js meta nm]
- cupertino_icons 1.0.5
- device_info_plus 8.0.0 [device_info_plus_platform_interface ffi file flutter flutter_web_plugins meta win32]
- dotted_border 2.0.0+3 [flutter path_drawing]
- easy_mask 2.0.1 [flutter]
- email_validator 2.1.17
- f_logs 2.0.1 [flutter sembast path_provider intl xxtea meta path stack_trace]
- fdg_formula_engine_test 0.0.0 [path intl expression_language]
- firebase_core 2.4.1 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_crashlytics 3.0.11 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace]
- firebase_messaging 14.2.1 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- flat_buffers 2.0.5
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_driver 0.0.0 [file flutter flutter_test fuchsia_remote_debug_protocol path meta vm_service webdriver archive async boolean_selector characters clock collection crypto matcher material_color_utilities platform process source_span stack_trace stream_channel string_scanner sync_http term_glyph test_api typed_data vector_math]
- flutter_image_compress 1.1.3 [flutter]
- flutter_inapp_notifications 0.0.3 [flutter]
- flutter_keyboard_visibility 5.4.0 [meta flutter_keyboard_visibility_platform_interface flutter_keyboard_visibility_linux flutter_keyboard_visibility_macos flutter_keyboard_visibility_web flutter_keyboard_visibility_windows flutter]
- flutter_local_notifications 12.0.3 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone]
- flutter_slidable 2.0.0 [flutter]
- flutter_svg 1.1.6 [flutter meta path_drawing vector_math xml]
- flutter_switch 0.3.2 [flutter]
- geolocator 9.0.2 [flutter geolocator_platform_interface geolocator_android geolocator_apple geolocator_web geolocator_windows]
- google_fonts 3.0.1 [flutter http path_provider crypto]
- google_mlkit_text_recognition 0.5.0 [flutter google_mlkit_commons]
- gps_connectivity 0.0.4 [flutter meta plugin_platform_interface]
- http 0.13.5 [async http_parser meta path]
- hypertrack_plugin 0.4.3 [flutter plugin_platform_interface]
- image 3.2.2 [archive meta xml]
- image_cropper 3.0.1 [flutter image_cropper_platform_interface image_cropper_for_web]
- image_picker 0.8.6 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_platform_interface]
- in_app_notification 1.1.2 [flutter]
- intl 0.17.0 [clock path]
- isolate_handler 1.0.2 [flutter flutter_isolate]
- json_annotation 4.7.0 [meta]
- jwt_decoder 2.0.1
- keyboard_actions 4.1.0 [flutter]
- linked_scroll_controller 0.2.0 [flutter]
- local_auth 2.1.2 [flutter intl local_auth_android local_auth_ios local_auth_platform_interface local_auth_windows]
- numeric_keyboard 1.1.0 [flutter]
- objectbox 1.6.2 [collection flat_buffers ffi meta path]
- objectbox_flutter_libs 1.6.2 [objectbox path_provider]
- package_info_plus 3.0.1 [ffi flutter flutter_web_plugins http meta path package_info_plus_platform_interface win32]
- path 1.8.2
- path_provider 2.0.11 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows]
- path_provider_android 2.0.21 [flutter path_provider_platform_interface]
- path_provider_ios 2.0.11 [flutter path_provider_platform_interface]
- permission_handler 10.2.0 [flutter meta permission_handler_android permission_handler_apple permission_handler_windows permission_handler_platform_interface]
- petitparser 5.1.0 [meta]
- rxdart 0.27.5
- scanbot_sdk 2.9.1 [logging json_annotation flutter]
- scrollable_positioned_list 0.3.5 [flutter collection]
- shared_preferences 2.0.15 [flutter shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows]
- sprintf 7.0.0
- string_validator 0.3.0
- syncfusion_flutter_datepicker 20.3.56 [flutter intl syncfusion_flutter_core]
- url_launcher 6.1.6 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows]
- uuid 3.0.6 [crypto]
- visibility_detector 0.3.3 [flutter]
- webview_flutter 3.0.4 [flutter webview_flutter_android webview_flutter_platform_interface webview_flutter_wkwebview]

transitive dependencies:
- _flutterfire_internals 1.0.12 [collection firebase_core firebase_core_platform_interface flutter meta]
- amplify_api_android 0.6.10 [flutter]
- amplify_api_ios 0.6.10 [amplify_core flutter]
- amplify_auth_cognito_android 0.6.10 [flutter]
- amplify_auth_cognito_ios 0.6.10 [amplify_core flutter]
- amplify_core 0.6.10 [aws_common collection flutter intl json_annotation meta plugin_platform_interface uuid]
- amplify_datastore_plugin_interface 0.6.10 [amplify_core collection flutter meta]
- amplify_flutter_android 0.6.10 [flutter]
- amplify_flutter_ios 0.6.10 [amplify_core flutter]
- archive 3.3.2 [crypto path]
- args 2.3.1
- aws_common 0.1.1 [async collection http meta stream_transform uuid]
- boolean_selector 2.1.1 [source_span string_scanner]
- camera_android 0.10.0+4 [camera_platform_interface flutter flutter_plugin_android_lifecycle stream_transform]
- camera_avfoundation 0.9.8+6 [camera_platform_interface flutter stream_transform]
- camera_platform_interface 2.3.0 [cross_file flutter plugin_platform_interface stream_transform]
- camera_web 0.3.0+1 [camera_platform_interface flutter flutter_web_plugins stream_transform]
- charts_common 0.12.0 [collection intl logging meta vector_math]
- clock 1.1.1
- connectivity_plus_platform_interface 1.2.3 [flutter meta plugin_platform_interface]
- cross_file 0.3.3+2 [js meta]
- crypto 3.0.2 [typed_data]
- dbus 0.7.8 [args ffi meta xml]
- device_info_plus_platform_interface 7.0.0 [flutter meta plugin_platform_interface]
- expression_language 1.0.0 [petitparser rational]
- fake_async 1.3.1 [clock collection]
- ffi 2.0.1
- file 6.1.4 [meta path]
- firebase_core_platform_interface 4.5.2 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.1.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_crashlytics_platform_interface 3.3.11 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_platform_interface 4.2.10 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.2.11 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- flutter_isolate 2.0.3 [flutter uuid]
- flutter_keyboard_visibility_linux 1.0.0 [flutter_keyboard_visibility_platform_interface flutter]
- flutter_keyboard_visibility_macos 1.0.0 [flutter_keyboard_visibility_platform_interface flutter]
- flutter_keyboard_visibility_platform_interface 2.0.0 [flutter meta plugin_platform_interface]
- flutter_keyboard_visibility_web 2.0.0 [flutter_keyboard_visibility_platform_interface flutter_web_plugins flutter]
- flutter_keyboard_visibility_windows 1.0.0 [flutter_keyboard_visibility_platform_interface flutter]
- flutter_local_notifications_linux 2.0.0 [flutter flutter_local_notifications_platform_interface dbus path xdg_directories]
- flutter_local_notifications_platform_interface 6.0.0 [flutter plugin_platform_interface]
- flutter_plugin_android_lifecycle 2.0.7 [flutter]
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters collection matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- fuchsia_remote_debug_protocol 0.0.0 [process vm_service file meta path platform]
- geolocator_android 4.1.4 [flutter geolocator_platform_interface]
- geolocator_apple 2.2.3 [flutter geolocator_platform_interface]
- geolocator_platform_interface 4.0.7 [flutter plugin_platform_interface vector_math meta]
- geolocator_web 2.1.6 [flutter flutter_web_plugins geolocator_platform_interface]
- geolocator_windows 0.1.1 [flutter geolocator_platform_interface]
- google_mlkit_commons 0.2.0 [flutter]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- image_cropper_for_web 1.0.3 [flutter flutter_web_plugins image_cropper_platform_interface js]
- image_cropper_platform_interface 3.0.3 [flutter plugin_platform_interface http]
- image_picker_android 0.8.5+3 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface]
- image_picker_for_web 2.1.10 [flutter flutter_web_plugins image_picker_platform_interface]
- image_picker_ios 0.8.6+1 [flutter image_picker_platform_interface]
- image_picker_platform_interface 2.6.2 [cross_file flutter http plugin_platform_interface]
- js 0.6.5 [meta]
- local_auth_android 1.0.15 [flutter flutter_plugin_android_lifecycle intl local_auth_platform_interface]
- local_auth_ios 1.0.10 [flutter intl local_auth_platform_interface]
- local_auth_platform_interface 1.0.5 [flutter intl plugin_platform_interface]
- local_auth_windows 1.0.4 [flutter local_auth_platform_interface]
- logging 1.1.0
- matcher 0.12.13 [meta stack_trace]
- material_color_utilities 0.1.5
- meta 1.8.0
- nm 0.5.0 [dbus]
- package_info_plus_platform_interface 2.0.1 [flutter meta plugin_platform_interface]
- path_drawing 1.0.1 [vector_math meta path_parsing flutter]
- path_parsing 1.0.1 [vector_math meta]
- path_provider_linux 2.1.7 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_macos 2.0.6 [flutter path_provider_platform_interface]
- path_provider_platform_interface 2.0.5 [flutter platform plugin_platform_interface]
- path_provider_windows 2.1.3 [ffi flutter path path_provider_platform_interface win32]
- permission_handler_android 10.2.0 [flutter permission_handler_platform_interface]
- permission_handler_apple 9.0.7 [flutter permission_handler_platform_interface]
- permission_handler_platform_interface 3.9.0 [flutter meta plugin_platform_interface]
- permission_handler_windows 0.1.2 [flutter permission_handler_platform_interface]
- platform 3.1.0
- plugin_platform_interface 2.1.3 [meta]
- process 4.2.4 [file path platform]
- quiver 3.1.0 [matcher]
- rational 1.2.1
- sembast 3.3.1 [meta path synchronized]
- shared_preferences_android 2.0.14 [flutter shared_preferences_platform_interface]
- shared_preferences_ios 2.1.1 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.1.1 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_macos 2.0.4 [flutter shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.1.0 [flutter plugin_platform_interface]
- shared_preferences_web 2.0.4 [flutter flutter_web_plugins shared_preferences_platform_interface]
- shared_preferences_windows 2.1.1 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- sky_engine 0.0.99
- source_span 1.9.1 [collection path term_glyph]
- stack_trace 1.11.0 [path]
- stream_channel 2.1.1 [async]
- stream_transform 2.1.0
- string_scanner 1.2.0 [source_span]
- sync_http 0.3.1
- syncfusion_flutter_core 20.3.56 [vector_math flutter]
- synchronized 3.0.0+3
- term_glyph 1.2.1
- test_api 0.4.16 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- timezone 0.9.0 [path]
- typed_data 1.3.1 [collection]
- url_launcher_android 6.0.21 [flutter url_launcher_platform_interface]
- url_launcher_ios 6.0.17 [flutter url_launcher_platform_interface]
- url_launcher_linux 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_macos 3.0.1 [flutter url_launcher_platform_interface]
- url_launcher_platform_interface 2.1.1 [flutter plugin_platform_interface]
- url_launcher_web 2.0.13 [flutter flutter_web_plugins url_launcher_platform_interface]
- url_launcher_windows 3.0.1 [flutter url_launcher_platform_interface]
- vector_math 2.1.4
- vm_service 9.4.0
- webdriver 3.0.1 [archive matcher path stack_trace sync_http]
- webview_flutter_android 2.10.4 [flutter webview_flutter_platform_interface]
- webview_flutter_platform_interface 1.9.5 [flutter meta plugin_platform_interface]
- webview_flutter_wkwebview 2.9.5 [flutter path webview_flutter_platform_interface]
- win32 3.1.1 [ffi]
- xdg_directories 0.2.0+2 [meta path process]
- xml 6.1.0 [collection meta petitparser]
- xxtea 2.1.0

Device

iPhone XR

OS

iOS 15.7.1 & iOS 16.2

Deployment Method

Amplify CLI

CLI Version

No response

Additional Context

No response

Amplify Config

{ "UserAgent": "aws-amplify-cli/2.0", "Version": "1.0", "auth": {..}, "api": { "plugins": { "awsAPIPlugin": { "app-name": { "endpointType": "GraphQL", "endpoint": "https://mydomain.amazonaws.com/graphql", "region": "region", "authorizationType": "OPENID_CONNECT" } } } } }

fjnoyp commented 1 year ago

Hi @kmvignesh can you please provide the code you use to setup the subscription.

Also, you mentioned you are unable to reproduce Appsync error 5, but that the issue happens when you start emulator go offline and then go online. What error or issue is consistently happening after?

Is this issue the same as your previous one? https://github.com/aws-amplify/amplify-flutter/issues/2581

fjnoyp commented 1 year ago

Wondering if this could somehow be related as well: https://github.com/aws-amplify/amplify-flutter/issues/2599

kmvignesh commented 1 year ago

Hi @kmvignesh can you please provide the code you use to setup the subscription.

Also, you mentioned you are unable to reproduce Appsync error 5, but that the issue happens when you start emulator go offline and then go online. What error or issue is consistently happening after?

Is this issue the same as your previous one? #2581

@fjnoyp yes. this issue is similar to https://github.com/aws-amplify/amplify-flutter/issues/2581 . But the main issue is, when user moves from background to fore ground we cancel the subscription like mentioned here. But when these exception is thrown, re-subscription not happening.

fjnoyp commented 1 year ago

Okay thanks for clarifying that. Can you please provide your code for subscribing and re subscribing then. After I can test on an iOS simulator to see if I can replicate.

kmvignesh commented 1 year ago

@fjnoyp Please find the code below.

try {
  _stream = Amplify.API.subscribe(
    GraphQLRequest<String>(
        apiName: api,
        document: getDocument(),
        variables: getVariables()),
    onEstablished: () {
      log('AppSync connection established');
    },
  );
} on ApiException catch (e, s) {
  crashlytic.log(exception: e, stackTrace: s)
}
_Subscription = _stream?.listen((event) {
  handleData(event, syncReading);
}, onDone: () {
  log('AppSync connection closed');
}, onError: (e, s) {
  crashlytic.log(exception: e, stackTrace: s)
});
fjnoyp commented 1 year ago

Hi @kmvignesh where is the resubscription logic triggered in your code?

Based on the exception you get on resubscribe: ApiException(message: Subscription item event failed with error: Unauthorized

It appears this might be an issue with your auth setup.

In your amplify config the authorization is set to OIDC. In this case you need to configure and manage the details of the token refreshes yourself - can you verify that you have done this properly?

Perhaps the app has been backgrounded long enough for the token to expire and your token is not being refreshed on app foreground before you attempt to resubscribe.

kmvignesh commented 1 year ago

When the app goes to background we cancel the subscription and re-subscribe it on resume using lifecycle handler.

class LifecycleHandler extends WidgetsBindingObserver {
  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
    switch (state) {
      case AppLifecycleState.resumed:
        AppSyncDal().subscribe();
        break;
      case AppLifecycleState.paused:
        AppSyncDal().cancel();
        break;
    }
  }
}
class AppSync {
  Stream<GraphQLResponse<String>>? _stream;
  StreamSubscription<GraphQLResponse<String>>? _subscription;

  void cancel() {
    if (_stream != null) {
      _subscription?.cancel().then((value) {
          log('AppSync cancelled');
      });
      _stream = null;
    }
  }

  void subscribe() {
    try {
      _stream = Amplify.API.subscribe(
        GraphQLRequest<String>(
            apiName: api,
            document: getDocument(),
            variables: getVariables()),
        onEstablished: () {
          log('AppSync connection established');
        },
      );
    } on ApiException catch (e, s) {
      crashlytic.log(exception: e, stackTrace: s)
    }
    _subscription = _stream?.listen((event) {
      handleData(event, syncReading);
    }, onDone: () {
      log('AppSync connection closed');
    }, onError: (e, s) {
      crashlytic.log(exception: e, stackTrace: s)
    });
  }
}

Below is the code for our auth setup

AmplifyAuthCognito authPlugin = AmplifyAuthCognito();
AmplifyAPI amplifyAPI = AmplifyAPI(
  authProviders: const [
    CustomOIDCProvider(),
  ],
);

await amplify.addPlugins([authPlugin, amplifyAPI]);
String config = '...';
await amplify.configure(config);
class CustomOIDCProvider extends OIDCAuthProvider {
  const CustomOIDCProvider();

  @override
  Future<String> getLatestAuthToken() async {
    String token = await getAuth().getToken();
    return token;
  }

}
ragingsquirrel3 commented 1 year ago

Thanks for the info, I was able to reproduce the errors you said you were able to reproduce (not the unauthorized errors). From what I understand of the underlying amplify-swift libraries these are expected when the connection becomes unstable and at times, you will have to resubscribe. You may also be able to use something like connectivity_plus to monitor the user's connection and cancel when they lose network, however, there will probably still be sometimes when you will have to resubscribe.

Regarding the unauthorized errors you mentioned, when you say resubscribe does not work, do you mean resub fails bc unauthorized or it's not possible to try? I know you said you can't repro this so maybe tough to know, but the most likely explanation would be that the auth token from CustomOIDCProvider isn't valid or isn't working. Is there any way to determine if these tokens are valid and getting pulled in correctly in these situations? If that's not working, then the unauthorized messages would be expected and it won't be possible to establish the connection.

Regarding error messages 5, 4, 3, also not sure what they mean, will update here if I get a definitive answer.

kmvignesh commented 1 year ago

@ragingsquirrel3 We are using connectivity_plus plugin as well as InternetAddress to make sure internet is available before making any subscription. Still we are getting these issues.

fjnoyp commented 1 year ago

Hi @kmvignesh thanks for you reply.

That's good that you check for internet availability before making any subscription.

However, it seems the re subscription is still not working.

Therefore, let's analyze if unauthorized errors are causing re subscription to not work.

A most likely explanation would be that the auth token from CustomOIDCProvider isn't valid or isn't working.

Can you determine if these tokens are valid and getting pulled in correctly when you re subscribe?

If you aren't pulling proper auth tokens, then the unauthorized messages would be expected and it won't be possible to establish re subscribe and establish that connection.

Jordan-Nelson commented 1 year ago

@kmvignesh - If you are still facing this issue can you please provide the info request above? Thanks.

fjnoyp commented 1 year ago

Hi @kmvignesh since we haven't heard back I'm going to close this issue. Please feel free to create another or respond here should you need any more help.