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

[Web] Stream.listen survives full restart of flutter on web #93793

Closed
pcrady opened this issue Nov 17, 2021 · 2 comments
Closed

[Web] Stream.listen survives full restart of flutter on web #93793

pcrady opened this issue Nov 17, 2021 · 2 comments
Labels
r: invalid Issue is closed as not valid

Comments

@pcrady
Copy link

pcrady commented Nov 17, 2021

main.dart

import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'dart:convert';
import 'dart:math';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MaterialApp(home: MyHomePage());
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var test = 0;
  late int randomInt;
  var random = Random();
  var uri = Uri.parse('wss://ws-feed-public.sandbox.pro.coinbase.com');
  late WebSocketChannel channel;
  String request = json.encode({
    "type": "subscribe",
    "product_ids": ["BTC-USD"],
    "channels": ["heartbeat"],
  });

  @override
  void initState() {
    randomInt = random.nextInt(1000);
    channel = WebSocketChannel.connect(uri);
    channel.sink.add(request);
    channel.stream.listen((event) {
      print('$randomInt: $test');
      test = test + 1;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Container();
}

pubspec.yaml

name: echowebsocket
description: A new Flutter project.

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  web_socket_channel: ^2.1.0

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

When I run the above code in flutter web and I do a hot restart (the green play button in android studio) the stream .listen() function survives the restart leaving two listening functions running.
An example output after running from cold (Chrome closed):

This application is not configured to build on the web.
To add web support to a project, run `flutter create .`.
Launching lib/main.dart on Chrome in debug mode...
Waiting for connection from debug service on Chrome...
This app is linked to the debug service: ws://127.0.0.1:56738/yLBcQjxMep4=/ws
Debug service listening on ws://127.0.0.1:56738/yLBcQjxMep4=/ws

💪 Running with sound null safety 💪
Debug service listening on ws://127.0.0.1:56738/yLBcQjxMep4=/ws
582: 0
582: 1
582: 2
582: 3
582: 4
582: 5
582: 6
582: 7
582: 8

Then after a hot restart:

Performing hot restart...
Waiting for connection from debug service on Chrome...
582: 30
Restarted application in 155ms.
876: 0
876: 1
582: 31
876: 2
582: 32
876: 3
582: 33
582: 34
876: 4
582: 35
876: 5
582: 36
876: 6
582: 37
876: 7
876: 8

As you can see there are now two listen functions being executed. I was under the impression that no state is supposed to survive a hot restart. I have sometimes been able to recreate this problem on emulators but it seems to work every time on web. It seems like its some kind of memory leak but dispose() doesn't get called when I do a hot restart so it doesn't matter what i do there. Here is the output of flutter doctor:

[✓] Flutter (Channel stable, 2.5.2, on macOS 11.6 20G165 darwin-x64, locale en)
    • Flutter version 2.5.2 at /Users/pcrady/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 3595343e20 (7 weeks ago), 2021-09-30 12:58:18 -0700
    • Engine revision 6ac856380f
    • Dart version 2.14.3

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/pcrady/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 13.1, Build version 13A1030d
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 96.0.4664.45

! Doctor found issues in 1 category.
@pcrady pcrady added the from: performance template Issues created via a performance issue template label Nov 17, 2021
@mamuseferha mamuseferha added in triage Presently being triaged by the triage team and removed from: performance template Issues created via a performance issue template labels Nov 18, 2021
@mamuseferha
Copy link

HI @pcrady
From what I can see, the issue is related to a 3rd party plugin rather than to Flutter itself. Please open the issue in the dedicated repository.
Closing, as this isn't an issue with Flutter itself.
If you disagree, please write in the comments,
providing your flutter doctor -v, your flutter run -v, your pubspec.yaml, a minimal reproducible code sample that does not use 3rd party plugins, your complete flutter run -v logs running the minimal code sample
and I will reopen it.
Thank you

@mamuseferha mamuseferha added r: invalid Issue is closed as not valid and removed in triage Presently being triaged by the triage team labels Nov 18, 2021
@github-actions
Copy link

github-actions bot commented Dec 2, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
r: invalid Issue is closed as not valid
Projects
None yet
Development

No branches or pull requests

2 participants