centrifugal / centrifuge-dart

Dart (Flutter) client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket
https://pub.dartlang.org/packages/centrifuge
MIT License
110 stars 34 forks source link

App crashes after recovering internet connection #7

Closed vanelizarov closed 5 years ago

vanelizarov commented 5 years ago

I've installed centrifuge lib in my flutter app. Not the version from pub, but directly from my fork of main repo. Suddenly I've experienced a bug: app crashes after recovering internet connection. There were no error messages in console, only 'Lost connection to device'.

Steps to reproduce (on iOS simulator):

  1. With active internet connection, connect to centrifuge on app start
  2. Subscribe to some arbitrary channel
  3. Disconnect from internet for about 30-40 seconds
  4. Enable internet again

After doing that, app crashes after a couple of sesonds. Tried launching app through Xcode, but when app crashes, there are also no error logs, only Message from debugger: Terminated due to signal 13.

flutter doctor:

[✓] Flutter (Channel dev, v1.4.4, on Mac OS X 10.14.5 18F96h, locale en-RU)
    • Flutter version 1.4.4 at /Users/vanelizarov/flutter
    • Framework revision 3b3f6c7a04 (2 days ago), 2019-03-27 13:56:23 -0700
    • Engine revision 0d83a2ecd1
    • Dart version 2.2.1 (build 2.2.1-dev.2.0 None)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/vanelizarov/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 10.2)
    • Xcode at /Applications/Xcode-beta.app/Contents/Developer
    • Xcode 10.2, Build version 10P107d
    • ios-deploy 2.0.0
    • CocoaPods version 1.5.3

[✓] Android Studio (version 3.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 33.4.1
    • Dart plugin version 182.5215
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[✓] VS Code (version 1.32.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 2.24.0

[✓] Connected device (2 available)
    • vanelizarov-iphone • c25ab95a7a881c4a4d782f7ebbff2c392d44bb69 • ios • iOS 12.3
    • iPhone Xʀ          • 96D9C935-0DD3-47BA-A9E3-F640322197D6     • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-2 (simulator)

• No issues found!
vanelizarov commented 5 years ago

The problem was in incorrect formatting in file lib/src/transport.dart Seems to be fixed in #8

FZambia commented 5 years ago

Hi, was this an error in your fork or the problem reproduces with code in this repo too?

vanelizarov commented 5 years ago

The problem reproduces with code in this repo, in my fork and in #8 I've fixed it Actually, it's in lines 10-11, when TransportBuilder typedef is defined

vanelizarov commented 5 years ago

Any updates?..

FZambia commented 5 years ago

Sorry, @mogol is a bit busy these days – as soon as he has time he will look at this

mogol commented 5 years ago

@vanelizarov I will go through the code, thanks.

But can you explain how it is possible that formatting changes behavior? Very curious

vanelizarov commented 5 years ago

@mogol I thought that the problem was in formatting, because as soon as I fixed it in transport.dart file, the problem stopped occuring. Maybe that was because I was running my app in flutter's debug mode, which is, as I know, kind of interpreter mode. So, transport.dart was not compiled when app started and when it came to it, app crashed. Unfortunately, this bug suddenly started appearing again, so this assumption was initially wrong.

mogol commented 5 years ago

I will check the issue over the weekends.

vanelizarov commented 5 years ago

@mogol Tried running on Android simulator - app didn't crash after recovering connection. It seems like this bug is reproducible only on iOS

mogol commented 5 years ago

@vanelizarov, I can't reproduce it. can you reproduce the issue

If yes, please share flutter doctor -v again

mogol commented 5 years ago

I use

[✓] Flutter (Channel unknown, v1.4.4, on Mac OS X 10.14.4 18E226, locale en-SG)
    • Flutter version 1.4.4 at /Users/german/Projects/github/flutter
    • Framework revision 3b3f6c7a04 (9 days ago), 2019-03-27 13:56:23 -0700
    • Engine revision 0d83a2ecd1
    • Dart version 2.2.1 (build 2.2.1-dev.2.0 None)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
 ...
[✓] iOS toolchain - develop for iOS devices (Xcode 10.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.2, Build version 10E125
    • ios-deploy 1.9.4
    • CocoaPods version 1.5.3

[!] Android Studio (version 3.3)
...
[✓] IntelliJ IDEA Community Edition (version 2018.3.5)
...
[✓] VS Code (version 1.32.3)
...
[✓] Connected device (1 available)
    • iPhone Xʀ • 2854D84D-6668-4D1D-9A36-7646CF88B7B8 • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-2 (simulator)
vanelizarov commented 5 years ago

@mogol yeah, this bug can be reproduced with toolchain you described

flutter doctor -v:

[✓] Flutter (Channel stable, v1.2.1, on Mac OS X 10.14.5 18F96h, locale en-RU)
    • Flutter version 1.2.1 at /Users/vanelizarov/flutter
    • Framework revision 8661d8aecd (7 weeks ago), 2019-02-14 19:19:53 -0800
    • Engine revision 3757390fa4
    • Dart version 2.1.2 (build 2.1.2-dev.0.0 0a7dcf17eb)

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/vanelizarov/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 10.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.2, Build version 10E125
    • ios-deploy 2.0.0
    • CocoaPods version 1.5.3

[✓] Android Studio (version 3.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 33.4.1
    • Dart plugin version 182.5215
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[✓] VS Code (version 1.32.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 2.24.0

[✓] Connected device (1 available)
    • vanelizarov-iphone • c25ab95a7a881c4a4d782f7ebbff2c392d44bb69 • ios • iOS 12.3

• No issues found!

To be honest, my api requires token to be supplied in centrifuge client to support any channel subscriptions. So, the only addition to demo app is http request to get such token in initState and then call to setToken method on _centrifuge prop:

import 'package:http/http.dart' as http;

...

@override
void initState() {
  final authUrl = 'http://10.78.206.237:8001/api/v1/pub/ws_pub_auth';
  http.post(authUrl).then((res) {
    final String token = json.decode(res.body)['token'];
    print(token);

    final centUrl = 'ws://10.78.206.237:8001/connection/websocket?format=protobuf';
    _centrifuge = centrifuge.createClient(centUrl);
    _centrifuge.setToken(token);
  });

  _controller = ScrollController();
  super.initState();
}

...

Here's the video demonstration: https://drive.google.com/open?id=1DGip5Rn-gG1a8KP-Q3yeJeWLRxhEgm0-

mogol commented 5 years ago

What is used as server? lib or server ? what version?

I test with centrifugo2.herokuapp.com and no crashes so far.

vanelizarov commented 5 years ago

@mogol we use latest Docker image centrifugo/centrifugo:v2.1

How can I connect to app on heroku? I have tried connecting via ws://centrifugo2.herokuapp.com/connection/websocker?format=protobuf with no luck, client stays permanently in connecting state

mogol commented 5 years ago

I use this

@override
  void initState() {
    final url =
        'wss://centrifugo2.herokuapp.com/connection/websocket?format=protobuf';
    Future<void>.delayed(Duration(seconds: 5)).then((_) {
      print('Set token');
      _centrifuge = centrifuge.createClient(url);
      _centrifuge.setToken(
          'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0c3VpdGVfand0In0.hPmHsVqvtY88PvK4EmJlcdwNuKFuy3BGaF7dMaKdPlw');
    });

    _controller = ScrollController();
    super.initState();
  }
vanelizarov commented 5 years ago

@mogol Cool, thanks, will try it now

vanelizarov commented 5 years ago

@mogol Hmmm, app is not crashing if I connect to heroku instance. Will try to check with my colleagues, maybe something is wrong in server config.

FZambia commented 5 years ago

Closing as can't reproduce with a permission from @vanelizarov

HathTech commented 3 years ago

@vanelizarov Hey, did you find the solution?