Repository: flutter
Issue: StateError from Multiple Stream Listeners in Debugger Attach
Labels: c: crash,tool,team-tool

Flutter version 3.24.3 on channel stable

When attempting to attach a debugger to a running Flutter application using the flutter attach command, an error occurs indicating that a stream has already been listened to, leading to an unexpected termination of the Flutter process. This issue surfaces when multiple instances of the Dart Development Service (DDS) attempt to listen on the same stream without proper handling for single-subscription streams, causing a StateError.

This was the solution:

packages/flutter_tools/lib/src/resident_runner.dart

From:

```
  Stream<Uri?>? vmServiceUris;
```

To:
```
  StreamController<Uri?>? _vmServiceUrisController;
  Stream<Uri?> get vmServiceUris => _vmServiceUrisController?.stream ?? const Stream<Uri?>.empty();
  set vmServiceUris(Stream<Uri?> value) => _vmServiceUrisController?.sink.addStream(value);
```

## command

flutter attach --no-version-check --suppress-analytics --debug --device-id emulator-5554 --target /integration_test/test_bundle.dart

## exception

StateError: Bad state: Stream has already been listened to.

```
#0      _StreamController._subscribe (dart:async/stream_controller.dart:686:7)
#1      _ControllerStream._createSubscription (dart:async/stream_controller.dart:837:19)
#2      _StreamImpl.listen (dart:async/stream_impl.dart:497:9)
#3      new _ForwardingStreamSubscription (dart:async/stream_pipe.dart:114:10)
#4      _ForwardingStream._createSubscription (dart:async/stream_pipe.dart:86:16)
#5      _ForwardingStream.listen (dart:async/stream_pipe.dart:81:12)
#6      FlutterDevice.connect (package:flutter_tools/src/resident_runner.dart:267:35)
#7      ResidentRunner.connectToServiceProtocol (package:flutter_tools/src/resident_runner.dart:1404:21)
#8      HotRunner.attach (package:flutter_tools/src/run_hot.dart:236:13)
#9      AttachCommand._attachToDevice (package:flutter_tools/src/commands/attach.dart:403:31)
<asynchronous suspension>
#10     AttachCommand.runCommand (package:flutter_tools/src/commands/attach.dart:265:5)
<asynchronous suspension>
#11     FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1408:27)
<asynchronous suspension>
#12     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:19)
<asynchronous suspension>
#13     CommandRunner.runCommand (package:args/command_runner.dart:212:13)
<asynchronous suspension>
#14     FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:420:9)
<asynchronous suspension>
#15     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:19)
<asynchronous suspension>
#16     FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:364:5)
<asynchronous suspension>
#17     run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:130:9)
<asynchronous suspension>
#18     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:19)
<asynchronous suspension>
#19     main (package:flutter_tools/executable.dart:93:3)
<asynchronous suspension>
```

## flutter doctor

```
[!] Flutter (Channel stable, 3.24.3, on macOS 13.6.4 22G513 darwin-x64, locale en-BR)
    • Flutter version 3.24.3 on channel stable at /Users/superuser/.asdf/installs/flutter/3.24.3-stable
    ! Upstream repository git@github.com:superuser/packages.git is not a standard remote.
      Set environment variable "FLUTTER_GIT_URL" to git@github.com:superuser/packages.git to dismiss this error.
    • Framework revision 2663184aa7 (6 days ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/superuser/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/superuser/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    ✗ CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it.
        This can usually be fixed by re-installing CocoaPods.
      For re-installation instructions, see https://guides.cocoapods.org/using/getting-started.html#installation

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • 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 17.0.9+0-17.0.9b1087.7-11185874)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.7)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • 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

[✓] VS Code (version 1.93.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.96.0

[✓] Connected device (4 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 14 (API 34) (emulator)
    • sdk gphone64 x86 64 (mobile) • emulator-5556 • android-x64    • Android 14 (API 34) (emulator)
    • macOS (desktop)              • macos         • darwin-x64     • macOS 13.6.4 22G513 darwin-x64
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 128.0.6613.138

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.
```