-
Notifications
You must be signed in to change notification settings - Fork 6k
[windows] Fix crash when calling FlutterDesktopMessengerSetCallback
after engine shutdown
#38941
Conversation
…fter engine shutdown
if (engine) { | ||
engine->message_dispatcher()->SetMessageCallback(channel, callback, | ||
user_data); | ||
} |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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).
Thanks for reporting this and sending a pull request! Here are some potential alternatives we should consider before moving forward with this:
Let me do some more digging to see which approach would best. /cc @gaaclarke @cbracken in case you have thoughts already here. |
@gaaclarke The engine's shutdown invalidates the messenger before plugins are shutdown: engine/shell/platform/windows/flutter_windows_engine.cc Lines 217 to 220 in 5932bad
Could that be swapped so that plugins have access to a valid messenger during shutdown? That would prevent these kinds of issues. Here's the call stack to shut down plugins:
|
We could look into it, but I don't think the end goal is worth the risk or investment to research. I think sending messages in the destructor should be an error. I guess it's debatable if we want it to be a crash or just a log message. |
I think the C++ client wrapper should implicitly handle this scenario. According to the documentation, "the caller is responsible for unregistering the handler if it should no longer be called". Why should there be a restriction that forbids calling it from a plugin destructor? My use case is a video player plugin which has one event channel per player instance. Each event channel is owned by the corresponding player instance. Hence, it's not just about removing the
Yes, moving the check to |
In hindsight, that comment is easily misunderstood. It's supposed to mean:
In other words, it shouldn't be necessary to set a
Interesting, could you tell us more about how players get used upon plugin destruction? |
I see. But what about destructing event channels in general? Should the stream handler be unset or not?
The left players just get destroyed (and so do their event channels). |
For most cases, you shouldn't need to worry about deregistering handlers manually. You definitely don't need to do it during plugin destruction as the engine will take care of destroying all platform/event channel handlers. |
@gaaclarke and I chatted and our plan is: Short-term: add asserts to the Flutter desktop API to ensure the messenger is valid before doing operations on it. Also, improve relevant comments. This will continue to be a crash, however, the developer experience should be improved. Long-term: consider making the messenger valid during engine shut down, however, disallow sending messages. In other words, engine shutdown would have two separate phases:
This requires investigation to see if it's a good idea. @jnschulze I'll go ahead and close this as we'll be taking a slightly different approach. Thank you for reporting this issue and jumpstarting this discussion 😄 |
Fixes flutter/flutter#118611
Pre-launch Checklist
///
).