From 57001fb72edcb9d9b0289f1c166f666cae5ca64b Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 10 Dec 2024 01:22:33 +0000 Subject: [PATCH] Do not handle Dart isolate messages if the isolate is being shut down 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 --- third_party/tonic/dart_message_handler.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/third_party/tonic/dart_message_handler.cc b/third_party/tonic/dart_message_handler.cc index b1c501ea1f225..1429927aaad78 100644 --- a/third_party/tonic/dart_message_handler.cc +++ b/third_party/tonic/dart_message_handler.cc @@ -39,7 +39,9 @@ void DartMessageHandler::OnMessage(DartState* dart_state) { auto weak_dart_state = dart_state->GetWeakPtr(); task_dispatcher_([weak_dart_state]() { if (auto dart_state = weak_dart_state.lock()) { - dart_state->message_handler().OnHandleMessage(dart_state.get()); + if (!dart_state->IsShuttingDown()) { + dart_state->message_handler().OnHandleMessage(dart_state.get()); + } } }); }