flutter / flutter/flutter

"Stream already subscribed" in Flutter Widget preview

Open
#192,424 3 comments 0 reactions 0 assignees View on GitHub
team-tool tool
Dominant language
Dart
Stars
179k
Forks
31.1k
PR merge metrics
PR metrics pending

Description

### Steps to reproduce

I don't have great steps to reproduce, it's somewhat inconsistent and I can only repo on GH Actions and in a local container (with a wonky setup).

I'm running Dart-Code's E2E Playwright tests that try to verify the widget preview, and sometimes it crashes.

Logs look approximately like:

```console
[Info] Spawning flutter with args ["widget-preview","start","--machine","--web-server",...]
[Info] <== widget_preview.initializing
[Info] "Launching analysis server..."
[warning] "Lost connection to the Dart Tooling Daemon (DTD). Live preview updates are paused.
Details: JSON-RPC error 103: Stream already subscribed"
[Info] "Launching the Widget Preview Scaffold on Web Server..."
[Info] "Launching lib/main.dart on Web Server in debug mode..."
[Info] "Triggering reload..." x3 (all in the same second)
[error] "Flutter failed to read file at /tmp/flutter_tools.XXX/web_entrypoint.dart.
PathNotFoundException ... errno 2"
[Error] Process flutter/bin/flutter terminated! 1
[Error] Widget Preview server process terminated
```

Looking in [dtd_services](https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/widget_preview/dtd_services.dart) my suspicion is a race condition in calls to `_waitForLspService()` being called twice:

1. First by `getFlutterWidgetPreviews()` as the preview starts loading
2. Next by `getFlutterWidgetPreviewsForFile()` as a file is opened

`_waitForLspService` bails out if `_lspServiceAvailable` is already set. However it's set after ` await _dtd!.streamListen(kServiceStream);`. This means if two calls to this function occur at the same time, both could call `streamListen()`.

@bkonyi WDYT, does this sound plausible? I haven't fully debugged this yet, because I can only repro it in containers on remote machines, but I'll try to hack in some logging to verify if that's what's happening.

### Expected results

No crash :)

### Actual results

Process crashes.

### Code sample

N/A

### Screenshots or Video

N/A

### Logs

Logs

```console
[Info] Spawning flutter with args ["widget-preview","start","--machine","--web-server",...]
[Info] <== widget_preview.initializing
[Info] "Launching analysis server..."
[warning] "Lost connection to the Dart Tooling Daemon (DTD). Live preview updates are paused.
Details: JSON-RPC error 103: Stream already subscribed"
[Info] "Launching the Widget Preview Scaffold on Web Server..."
[Info] "Launching lib/main.dart on Web Server in debug mode..."
[Info] "Triggering reload..." x3 (all in the same second)
[error] "Flutter failed to read file at /tmp/flutter_tools.XXX/web_entrypoint.dart.
PathNotFoundException ... errno 2"
[Error] Process flutter/bin/flutter terminated! 1
[Error] Widget Preview server process terminated
```

### Flutter Doctor output

Doctor output

```console
└ [!] Flutter (Channel stable, 3.47.2, on Ubuntu 26.04 LTS 7.0.0-30-generic, locale en_US) [25ms]
• Flutter version 3.47.2 on channel stable at /home/ubuntu/flutter
! Warning: `dart` on your path resolves to /usr/lib/dart/bin/dart, which is not inside your current Flutter SDK checkout at /home/ubuntu/flutter. Consider adding /home/ubuntu/flutter/bin to the front of your path.
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision d3b14c8769 (13 days ago), 2026-08-26 16:07:51 -0700
• Engine revision a804b26164
• Dart version 3.13.2
• DevTools version 2.60.0
• Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-native-assets, enable-record-use, enable-swift-package-manager, omit-legacy-version-file, enable-lldb-debugging, enable-uiscene-migration
• 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 [20ms]
✗ Unable to locate Android SDK.
Install Android Studio from: https://developer.android.com/studio/index.html
On first launch it will assist you in installing the Android SDK components.
(or visit https://flutter.dev/to/linux-android-setup for detailed instructions).
If the Android SDK has been installed to a custom location, please use
`flutter config --android-sdk` to update to that location.

[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome) [8ms]
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✗] Linux toolchain - develop for Linux desktop [24ms]
✗ clang++ is required for Linux development.
It is likely available from your distribution (e.g.: apt install clang), or can be downloaded from https://releases.llvm.org/
✗ CMake is required for Linux development.
It is likely available from your distribution (e.g.: apt install cmake), or can be downloaded from https://cmake.org/download/
✗ ninja is required for Linux development.
It is likely available from your distribution (e.g.: apt install ninja-build), or can be downloaded from https://github.com/ninja-build/ninja/releases
✗ pkg-config is required for Linux development.
It is likely available from your distribution (e.g.: apt install pkg-config), or can be downloaded from https://www.freedesktop.org/wiki/Software/pkg-config/

[✓] Connected device (1 available) [25ms]
• Linux (desktop) • linux • linux-x64 • Ubuntu 26.04 LTS 7.0.0-30-generic

[✓] Network resources [208ms]
• All expected network resources are available.

! Doctor found issues in 4 categories.
```

Contributor guide

Open the contributing guide

Research direction

Start in packages/flutter_tools/lib/src/widget_preview/dtd_services.dart, focusing on _waitForLspService(), getFlutterWidgetPreviews(), and getFlutterWidgetPreviewsForFile(). Run the Dart-Code E2E Playwright widget-preview tests in a setup where the failure can be reproduced, and verify whether concurrent calls cause the duplicate stream subscription. Done means the preview no longer crashes with “Stream already subscribed” or the resulting missing web_entrypoint.dart error.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter, playwright
Domain
testing, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.