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

[flutter web] violently crashes app on when the server isn't available #15

Open
lukepighetti opened this issue Mar 22, 2021 · 4 comments

Comments

@lukepighetti
Copy link

lukepighetti commented Mar 22, 2021

Description

If you try to connect to a websocket server that isn't available on flutter web the app violently crashes even if you try to catch the error.

flutter --version

Flutter 2.1.0-12.1.pre • channel dev • https://github.com/flutter/flutter.git
Framework • revision 8264cb3e8a (12 days ago) • 2021-03-10 12:37:57 -0800
Engine • revision 711ab3fda0
Tools • Dart 2.13.0 (build 2.13.0-116.0.dev)

Reproduction

  • run MCVE on Chrome (web environment) with no server available at ws://localhost:8000/foo

Expected Results

  • Websocket tries to connect, fails
  • The try/catch block successfully catches the error
  • App to proceeds to execute as expected

Actual Results

  • Websocket tries to connect, fails
  • The try/catch block fails to catch the error
  • App violently crashes

Error State

Screen Shot 2021-03-22 at 11 40 10 AM

MCVE

import 'package:flutter/material.dart';
import 'package:socketcluster_client/socketcluster_client.dart';

void main() async {
  try {
    await Socket.connect('ws://localhost:8000/foo', listener: MyListener());
  } catch (e) {
    print('caught $e');
  } finally {
    runApp(MyApp());
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Websocket Issue',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Center(
          child: Text('Websocket Issue'),
        ),
      ),
    );
  }
}

class MyListener extends BasicListener {
  @override
  void onAuthentication(Socket socket, bool status) {
    print('onAuthentication');
  }

  @override
  void onConnectError(Socket socket, e) {
    print('onConnectError');
  }

  @override
  void onConnected(Socket socket) {
    print('onConnected');
  }

  @override
  void onDisconnected(Socket socket) {
    print('onDisconnected');
  }

  @override
  void onSetAuthToken(String token, Socket socket) {
    print('onSetAuthToken');
  }
}
name: flutter_websocket_issue
description: A new Flutter project.
publish_to: "none"
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  socketcluster_client: ^0.3.0

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true
@lukepighetti lukepighetti changed the title Hard crash on flutter web if the server is not available [flutter web] violently crashes app on when the server isn't available Mar 22, 2021
@lukepighetti
Copy link
Author

This was cross posted from MichaelMarner/dart-redux-remote-devtools#35, which has been closed because the issue is upstream in socketcluster_client

@lukepighetti
Copy link
Author

lukepighetti commented Mar 22, 2021

I tried wrapping the web WebSocket creation with this, making sure to await the builder method, but I still can't get it to error cleanly. If I put this same exact code in my Flutter main() method, it will error cleanly. No idea what's going on inside this package to cause this difference.

final completer = Completer();
final ws = WebSocket(url);

ws.onError.listen((e) {
  print('onError $e');
  completer.complete();
});

ws.onOpen.listen((e) {
  print('onOpen $e');
  completer.complete();
});

await completer.future;

return ws;

@MichaelMarner
Copy link
Contributor

I don't use Flutter on web so don't have much experience here. But do you get a stacktrace where the error is occuring (ie what's in the exception in the devtools if you expand it)?

@lukepighetti
Copy link
Author

lukepighetti commented Mar 23, 2021

I get nothing useful out of any of the normal flutter tooling, unfortunately.

This package is upstream of redux_remote_dev_tools and the result is that on web if we try to connect to dev tools and the server is not up, the app hard crashes and won't launch. This problem doesn't exist on any other platform, where the failure to connect is easily caught by a try/catch block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants