Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[windows] Fix crash when calling FlutterDesktopMessengerSetCallback after engine shutdown #38941

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions shell/platform/windows/flutter_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,12 @@ void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger,
const char* channel,
FlutterDesktopMessageCallback callback,
void* user_data) {
flutter::FlutterDesktopMessenger::FromRef(messenger)
->GetEngine()
->message_dispatcher()
->SetMessageCallback(channel, callback, user_data);
auto engine =
flutter::FlutterDesktopMessenger::FromRef(messenger)->GetEngine();
if (engine) {
engine->message_dispatcher()->SetMessageCallback(channel, callback,
user_data);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move forward with this approach, we should also update FlutterDesktopMessengerSendWithReply and FlutterDesktopMessengerSendResponse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a difference between setting the handler and sending a message. I think the OP was trying to set the handler to nullptr so that they know the proper cleanup is happening instead of relying on the engine to do so. They wanted to make that not an error. I think it would be acceptable to make that not a crash, but to keep sending messages in the destructors a crash (we can improve the crash messaging with an assert if we don't already).

}

FlutterDesktopMessengerRef FlutterDesktopMessengerAddRef(
Expand Down
20 changes: 20 additions & 0 deletions shell/platform/windows/flutter_windows_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,25 @@ TEST_F(WindowsTest, GetGraphicsAdapter) {
ASSERT_TRUE(SUCCEEDED(dxgi_adapter->GetDesc(&desc)));
}

// Verify that calling FlutterDesktopMessengerSetCallback
// after shutting the engine down is safe.
// Prevent regression: https://github.com/flutter/flutter/issues/118611
TEST_F(WindowsTest, SetMessengerCallbackAfterEngineShutdown) {
auto& context = GetContext();
WindowsConfigBuilder builder(context);
EnginePtr engine{builder.InitializeEngine()};
ASSERT_NE(engine, nullptr);

auto messenger = FlutterDesktopEngineGetMessenger(engine.get());
FlutterDesktopMessengerAddRef(messenger);
ASSERT_TRUE(FlutterDesktopMessengerIsAvailable(messenger));

engine.reset();

ASSERT_FALSE(FlutterDesktopMessengerIsAvailable(messenger));
FlutterDesktopMessengerSetCallback(messenger, "my_channel", nullptr, nullptr);
FlutterDesktopMessengerRelease(messenger);
}

} // namespace testing
} // namespace flutter