Do not handle Dart isolate messages if the isolate is being shut down (#161824)

This fixes a race that can happen if Dart invokes an isolate's message
handling callback during isolate shutdown. The callback installed by
Tonic will invoke a dispatcher set by the engine's
DartIsolate::SetMessageHandlingTaskRunner, which will queue a message
handling task to the designated task runner.

If the queued task runs after DartIsolate::Shutdown has been called,
then Tonic's DartMessageHandler::OnHandleMessage will fail when it tries
to enter the isolate.

DartIsolate will set a shutdown flag on the DartState when Dart invokes
the isolate's shutdown callback. Tonic can avoid this race by checking
that flag before proceeding with message handling.

Fixes https://github.com/flutter/flutter/issues/160003
This commit is contained in:
Jason Simmons 2025-01-21 11:10:01 -08:00 committed by GitHub
parent 5ffb45bf78
commit bdecbaec9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -39,8 +39,10 @@ void DartMessageHandler::OnMessage(DartState* dart_state) {
auto weak_dart_state = dart_state->GetWeakPtr(); auto weak_dart_state = dart_state->GetWeakPtr();
task_dispatcher_([weak_dart_state]() { task_dispatcher_([weak_dart_state]() {
if (auto dart_state = weak_dart_state.lock()) { if (auto dart_state = weak_dart_state.lock()) {
if (!dart_state->IsShuttingDown()) {
dart_state->message_handler().OnHandleMessage(dart_state.get()); dart_state->message_handler().OnHandleMessage(dart_state.get());
} }
}
}); });
} }