Repository navigation
Description
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
nilhandler for unregistering the existing handler."
Note that iOS doesn't have this issue; only macOS does.
There are bugs in two places:
setMessageHandlerOnChannel:binaryMessageHandler:which retains nil handlersengineCallbackOnPlatformMessage:checks thathandlerInfois not null, but doesn't checkhandlerInfo.handler.
Proposed Fix
- In
setMessageHandlerOnChannel:binaryMessageHandler:, remove the channel from_messengerHandlersifhandler == nil. - In
engineCallbackOnPlatformMessage:, verifyhandlerInfo.handleris 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 + 72Flutter 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
saurabh-mirajkar commentedon Sep 22, 2026
@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?vardhan commentedon Sep 22, 2026
thanks @saurabh-mirajkar, here's a minimal repro: https://github.com/vardhan/nil_handler_repro for stable 3.47.5
when you run it (
flutter run -d macos), it will automatically crash.saurabh-mirajkar commentedon Sep 23, 2026
@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.
[macOS] Unregister nil binary message handlers (flutter#193672)