Skip to content

[macOS] Crash in engineCallbackOnPlatformMessage when dispatching a nil handler #193097

Closed

Description

@vardhan

Bug Description

In the macOS embedder, passing nil to -[FlutterEngine setMessageHandlerOnChannel:binaryMessageHandler:] does not remove the channel entry from _messengerHandlers. As a result, when a platform message comes through on that channel, it calls this nil handler on MacOS and crashes.

However, FlutterBinaryMessenger.h says that assigning nil should unregister the handler:

"Replaces any existing handler. Use a nil handler for unregistering the existing handler."

Note that iOS doesn't have this issue; only macOS does.

There are bugs in two places:

  1. setMessageHandlerOnChannel:binaryMessageHandler: which retains nil handlers
  2. engineCallbackOnPlatformMessage: checks that handlerInfo is not null, but doesn't check handlerInfo.handler.

Proposed Fix

  1. In setMessageHandlerOnChannel:binaryMessageHandler:, remove the channel from _messengerHandlers if handler == nil.
  2. In engineCallbackOnPlatformMessage:, verify handlerInfo.handler is non-nil before invoking it.

Expected results

don't call null handlers, and maybe call binaryResponseHandler(nil) instead?

Actual results

The engine crashes with SEGV_ACCERR in -[FlutterEngine engineCallbackOnPlatformMessage:]; see stacktrace below

Repro code sample

You can repro this by unregistering a handler on the platform thread and then sending a message to that channel from Dart:

Native side:

id<FlutterBinaryMessenger> messenger = engine.binaryMessenger;

// Register a handler, then unregister it per the API contract:
[messenger setMessageHandlerOnChannel:@"repro/nil-handler"
                binaryMessageHandler:^(NSData* message, FlutterBinaryReply reply) {
  reply(nil);
}];

// Unregister:
[messenger setMessageHandlerOnChannel:@"repro/nil-handler"
                binaryMessageHandler:nil];

Dart side:

const BasicMessageChannel<String>(
  'repro/nil-handler',
  StringCodec(),
).send('ping');
Logs

Teardown Stack Trace (showing run loop pumping during view disposal)

Received signal 11 SEGV_ACCERR 000000000010
-[FlutterEngine engineCallbackOnPlatformMessage:] + 396
flutter::PlatformViewEmbedder::HandlePlatformMessage(...)
flutter::EmbedderTaskRunner::PostTask(...)
FlutterEngineRunTask
-[FlutterEngine postMainThreadTask:targetTimeInNanoseconds:] block
InternalFlutterSwift.FlutterRunLoop.performExpiredTasks
InternalFlutterSwift.FlutterRunLoop.pollFlutterMessagesOnce
-[FlutterEngine deregisterViewControllerForIdentifier:] + 168
-[FlutterEngine removeViewController:] + 84
-[FlutterViewController dispose] + 80
AppHostView::Detach()

Signal Handler Stack Trace

Received signal 11 SEGV_ACCERR 000000000010
3   libsystem_platform.dylib            0x000000019b6c4144 _sigtramp + 56
4   FlutterMacOS                        0x00000001040ebb98 -[FlutterEngine engineCallbackOnPlatformMessage:] + 396
5   FlutterMacOS                        0x0000000104a90910 _ZNSt4__fl10__function6__funcIZ23FlutterEngineInitializeE3$3NS_9allocatorIS2_EEFvNS_10unique_ptrIN7flutter15PlatformMessageENS_14default_deleteIS7_EEEEEEclEOSA + 128
6   FlutterMacOS                        0x0000000104aa48e4 flutter::PlatformViewEmbedder::HandlePlatformMessage(std::__fl::unique_ptr<flutter::PlatformMessage, std::__fl::default_delete<flutter::PlatformMessage>>) + 76
7   FlutterMacOS                        0x0000000104aa54d4 std::__fl::__function::__func<...>::operator()() + 84
8   FlutterMacOS                        0x0000000104aa166c flutter::EmbedderTaskRunner::PostTask(unsigned long long) + 576
9   FlutterMacOS                        0x0000000104a955dc flutter::EmbedderEngine::RunTask(FlutterTask const*) + 56
10  FlutterMacOS                        0x0000000104a87e20 FlutterEngineRunTask + 68
11  FlutterMacOS                        0x00000001040ef7fc __60-[FlutterEngine postMainThreadTask:targetTimeInNanoseconds:]block_invoke + 72
Flutter Doctor output
[!] Flutter (Channel [user-branch], 3.47.4, on macOS 27.0 26A428 darwin-arm64, locale en-CA) [204ms]
    ! Flutter version 3.47.4 on channel [user-branch] at /Users/vardhan/development/flutter
      Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
      If that doesn't fix the issue, try deleting the 'bin/cache/flutter.version.json' file in your Flutter SDK directory and then reinstall Flutter by following instructions at https://flutter.dev/setup.
    ! The flutter binary is not on your path. Consider adding /Users/vardhan/development/flutter/bin to your path.
    ! The dart binary is not on your path. Consider adding /Users/vardhan/development/flutter/bin to your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 9584c6713b (11 days ago), 2026-09-10 15:25:10 -0700
    • Engine revision 06a2e2a110
    • Dart version 3.13.3
    • 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, enable-riscv64

[✗] Android toolchain - develop for Android devices [121ms]
    ✗ Unable to locate Android SDK.

[!] Xcode - develop for iOS and macOS (Xcode 26.6) [2.1s]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 17F113
    ✗ Unable to get list of installed Simulator runtimes.
    ! CocoaPods not installed.

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

[✓] Connected device (2 available) [6.6s]
    • macOS (desktop) • macos  • darwin-arm64   • macOS 27.0 26A428 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 152.0.7977.83

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

! Doctor found issues in 3 categories.

Activity

  1. added
    in triage
    on Sep 22, 2026
  2. saurabh-mirajkar commented on Sep 22, 2026

    @saurabh-mirajkar
    Contributor

    @vardhan Thanks for the detailed report. Could you please provide a complete runnable macOS reproduction, including the native host-side code, so we can reproduce this without having to infer how the Objective-C snippet is integrated into the Swift macOS runner?

    Also, could you verify whether the issue reproduces on the latest stable Flutter 3.47.5 rather than the [user-branch] 3.47.4 setup?

  3. added
    waiting for response
    and removed
    in triage
    on Sep 22, 2026
  4. vardhan commented on Sep 22, 2026

    @vardhan
    Author
  5. removed
    waiting for response
    on Sep 22, 2026
  6. added
    in triage
    on Sep 23, 2026
  7. saurabh-mirajkar commented on Sep 23, 2026

    @saurabh-mirajkar
    Contributor

    @vardhan Thanks for providing the complete reproduction. I was able to reproduce this on Flutter (3.47.5) stable and on current master (3.49.0-1.0.pre-92). Routing this to the team for further investigation.

  8. added
    c: crash
    engine
    platform-macos
    a: desktop
    has reproducible steps
    team-macos
    and removed
    in triage
    on Sep 23, 2026
  9. added
    P2
    triaged-macos
    on Sep 24, 2026
  10. added a commit that references this issue on Nov 20, 2026
    60a19c9
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

    P2a: desktopc: crashenginehas reproducible stepsplatform-macosteam-macostriaged-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions