Skip to content

Calling endOfStream on a MockStreamHandlerEventSink causes widget tests to hang #190807

Closed

Description

@SamJakob

Steps to reproduce

(See the code example below).

  1. Set up a mock stream handler:
      const kTestEventChannelName = 'binary_messenger_test';

      TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
          .setMockStreamHandler(
            EventChannel(kTestEventChannelName),
            MockStreamHandler.inline(onListen: _onListen),
          );
  1. In the onListen for the stream (or whilst the stream is being listened to), call endOfStream:
      void _onListen(
        final Object? rawArguments,
        final MockStreamHandlerEventSink events,
      ) {
        events.endOfStream();
      }
  1. Listen to the stream:
      EventChannel(
        kTestEventChannelName,
      ).receiveBroadcastStream({'close': true}).listen((_) {});
  1. Observe that the test hangs.

Expected results

The test should not hang.

Actual results

The test hangs. (Reproducible on stable 3.44.9 and the latest beta build 3.47.0-0.4.pre)

Code sample

Code sample
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('closure of MockStreamHandlerEventSink does not hang', () {
    setUpAll(TestWidgetsFlutterBinding.ensureInitialized);

    const kTestEventChannelName = 'binary_messenger_test';

    setUp(() {
      void _onListen(
        final Object? rawArguments,
        final MockStreamHandlerEventSink events,
      ) {
        final arguments = rawArguments as Map<dynamic, dynamic>? ?? {};
        final close = arguments['close'] as bool? ?? false;

        if (close) {
          events.endOfStream();
        }
      }

      TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
          .setMockStreamHandler(
            EventChannel(kTestEventChannelName),
            MockStreamHandler.inline(onListen: _onListen),
          );
    });

    testWidgets('test finishes without hanging when sink is not closed', (
      final tester,
    ) async {
      EventChannel(
        kTestEventChannelName,
      ).receiveBroadcastStream({'close': false}).listen((_) {});
    });

    testWidgets('test finishes without hanging when sink is closed', (
      final tester,
    ) async {
      EventChannel(
        kTestEventChannelName,
      ).receiveBroadcastStream({'close': true}).listen((_) {});
    }, timeout: const Timeout(Duration(seconds: 1)));
  });
}

Screenshots or Video

N/A

Logs

N/A

Flutter Doctor output

Doctor output
❯ flutter doctor -v
[✓] Flutter (Channel beta, 3.47.0-0.4.pre, on macOS 26.5.2 25F84 darwin-arm64, locale en-US) [984ms]
    • Flutter version 3.47.0-0.4.pre on channel beta at /Users/samjakob/sdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 034409942b (4 days ago), 2026-08-04 19:31:03 -0700
    • Engine revision f88005a259
    • Dart version 3.13.0 (build 3.13.0-282.4.beta)
    • 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

[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [1,551ms]
    • Android SDK at [REDACTED]
    • Emulator version 36.6.11.0 (build_id 15507667) (CL:N/A)
    • Platform android-36.1, build-tools 36.0.0
    • Java binary at: [REDACTED]
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.x.x [REDACTED])
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 26.6) [614ms]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 17F113
    ! CocoaPods not installed.
        CocoaPods is a package manager for iOS or macOS platform code.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/to/platform-plugins
      For installation instructions, see https://guides.cocoapods.org/using/getting-started.html#installation

[✗] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome) [5ms]
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Connected device (1 available) [5.9s]
    • macOS (desktop) • macos • darwin-arm64 • macOS 26.x.x [REDACTED] darwin-arm64

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

! Doctor found issues in 2 categories.

Activity

  1. saurabh-mirajkar commented on Aug 10, 2026

    @saurabh-mirajkar
    Contributor

    @SamJakob Thanks for the report. I was able to reproduce with provided test sample on both latest stable and current master. Routing this to further investigation.

    stable
    Flutter 3.44.9 • channel stable • https://github.com/flutter/flutter.git
    Framework • revision 6b182d2c75 (5 days ago) • 2026-08-05 10:04:07 -0700
    Engine • hash b9499e4c25212536ba3a4eec4f5c1905fb3214fe (revision 5a2a6a42cc) (9 days ago) • 2026-07-31 18:31:59.000Z
    Tools • Dart 3.12.2 • DevTools 2.57.0
    
    master
    Flutter 3.47.0-1.0.pre-468 • channel master • https://github.com/saurabh-mirajkar/flutter.git
    Framework • revision 1cd7c6b603 (68 minutes ago) • 2026-08-10 02:36:27 -0400
    Engine • hash 765e05252ba7a57f5e0d6645751bfb22bd893162 (revision 1cd7c6b603) (60 minutes ago) • 2026-08-10
    06:36:27.000Z
    Tools • Dart 3.14.0 (build 3.14.0-110.0.dev) • DevTools 2.61.0-dev.0
    
    log
    00:01 +1 -1: closure of MockStreamHandlerEventSink does not hang hangs when sink is closed [E]                                                    
      TimeoutException after 0:00:01.000000: Test timed out after 1 seconds.
      dart:isolate  _RawReceivePort._handleMessage
      
    
    To run this test again: /Users/admin/develop/flutter/bin/cache/dart-sdk/bin/dart test /Users/admin/develop/projects/flutter_application_2/test/widget_test.dart -p vm --plain-name 'closure of MockStreamHandlerEventSink does not hang hangs when sink is closed'
    00:01 +1 -1: Some tests failed.
    
  2. added
    a: tests"flutter test", flutter_test, or one of our tests
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on Aug 10, 2026
  3. added a commit that references this issue on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our testshas reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework teamwaiting for PR to land (fixed)A fix is in flight

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions