diff --git a/engine/src/flutter/lib/ui/ui_dart_state.h b/engine/src/flutter/lib/ui/ui_dart_state.h index 3659c01cd6..f659fe33f5 100644 --- a/engine/src/flutter/lib/ui/ui_dart_state.h +++ b/engine/src/flutter/lib/ui/ui_dart_state.h @@ -20,6 +20,7 @@ class Window; class IsolateClient { public: virtual void DidCreateSecondaryIsolate(Dart_Isolate isolate) = 0; + virtual void DidShutdownMainIsolate() = 0; protected: virtual ~IsolateClient(); @@ -47,6 +48,8 @@ class UIDartState : public tonic::DartState { PassRefPtr font_selector(); bool is_controller_state() const { return is_controller_state_; } void set_is_controller_state(bool value) { is_controller_state_ = value; } + bool shutting_down() const { return shutting_down_; } + void set_shutting_down(bool value) { shutting_down_ = value; } private: void DidSetIsolate() override; @@ -58,6 +61,7 @@ class UIDartState : public tonic::DartState { std::unique_ptr window_; RefPtr font_selector_; bool is_controller_state_; + bool shutting_down_ = false; }; } // namespace blink diff --git a/engine/src/flutter/runtime/dart_controller.cc b/engine/src/flutter/runtime/dart_controller.cc index 4cd7145cd4..636b20f78c 100644 --- a/engine/src/flutter/runtime/dart_controller.cc +++ b/engine/src/flutter/runtime/dart_controller.cc @@ -49,12 +49,14 @@ DartController::~DartController() { if (ui_dart_state_) { ui_dart_state_->set_isolate_client(nullptr); - // Don't use a tonic::DartIsolateScope here since we never exit the isolate. - Dart_EnterIsolate(ui_dart_state_->isolate()); - // Clear the message notify callback. - Dart_SetMessageNotifyCallback(nullptr); - Dart_ShutdownIsolate(); - delete ui_dart_state_; + if (!ui_dart_state_->shutting_down()) { + // Don't use a tonic::DartIsolateScope here since we never exit the + // isolate. + Dart_EnterIsolate(ui_dart_state_->isolate()); + // Clear the message notify callback. + Dart_SetMessageNotifyCallback(nullptr); + Dart_ShutdownIsolate(); + } } } diff --git a/engine/src/flutter/runtime/dart_controller.h b/engine/src/flutter/runtime/dart_controller.h index 3eaab46b07..4b11a8660b 100644 --- a/engine/src/flutter/runtime/dart_controller.h +++ b/engine/src/flutter/runtime/dart_controller.h @@ -39,17 +39,13 @@ class DartController { UIDartState* dart_state() const { return ui_dart_state_; } - void IsolateShuttingDown(); - private: bool SendStartMessage(Dart_Handle root_library, const std::string& entrypoint = main_entrypoint_); static const std::string main_entrypoint_; - // The DartState associated with the main isolate. This is not deleted - // during isolate shutdown, instead it is deleted when the controller - // object is deleted. + // The DartState associated with the main isolate. UIDartState* ui_dart_state_; FXL_DISALLOW_COPY_AND_ASSIGN(DartController); diff --git a/engine/src/flutter/runtime/dart_init.cc b/engine/src/flutter/runtime/dart_init.cc index d40d26f6c1..db4b093149 100644 --- a/engine/src/flutter/runtime/dart_init.cc +++ b/engine/src/flutter/runtime/dart_init.cc @@ -140,12 +140,24 @@ void IsolateShutdownCallback(void* callback_data) { Dart_Handle sticky_error = Dart_GetStickyError(); FXL_CHECK(LogIfError(sticky_error)); } + UIDartState* dart_state = static_cast(callback_data); - if ((dart_state != NULL) && !dart_state->is_controller_state()) { - delete dart_state; + // If the isolate that's shutting down is the main one, tell the higher layers + // of the stack. + if ((dart_state != NULL) && dart_state->is_controller_state()) { + dart_state->set_shutting_down(true); + if (dart_state->isolate_client()) { + dart_state->isolate_client()->DidShutdownMainIsolate(); + } } } +// The cleanup callback frees the DartState object. +void IsolateCleanupCallback(void* callback_data) { + UIDartState* dart_state = static_cast(callback_data); + delete dart_state; +} + bool DartFileModifiedCallback(const char* source_url, int64_t since_ms) { if (strncmp(source_url, kFileUriPrefix, kFileUriPrefixLength) != 0u) { // Assume modified. @@ -581,6 +593,7 @@ void InitDartVM(const uint8_t* vm_snapshot_data, params.vm_snapshot_instructions = vm_snapshot_instructions; params.create = IsolateCreateCallback; params.shutdown = IsolateShutdownCallback; + params.cleanup = IsolateCleanupCallback; params.thread_exit = ThreadExitCallback; params.get_service_assets = GetVMServiceAssetsArchiveCallback; params.entropy_source = DartIO::EntropySource; diff --git a/engine/src/flutter/runtime/runtime_controller.cc b/engine/src/flutter/runtime/runtime_controller.cc index 4bab602cc8..d3348ad7c5 100644 --- a/engine/src/flutter/runtime/runtime_controller.cc +++ b/engine/src/flutter/runtime/runtime_controller.cc @@ -142,6 +142,10 @@ void RuntimeController::DidCreateSecondaryIsolate(Dart_Isolate isolate) { client_->DidCreateSecondaryIsolate(isolate); } +void RuntimeController::DidShutdownMainIsolate() { + client_->DidShutdownMainIsolate(); +} + Dart_Port RuntimeController::GetMainPort() { if (!dart_controller_) { return ILLEGAL_PORT; diff --git a/engine/src/flutter/runtime/runtime_controller.h b/engine/src/flutter/runtime/runtime_controller.h index 3783929e5b..d774c5de87 100644 --- a/engine/src/flutter/runtime/runtime_controller.h +++ b/engine/src/flutter/runtime/runtime_controller.h @@ -61,6 +61,7 @@ class RuntimeController : public WindowClient, public IsolateClient { void HandlePlatformMessage(fxl::RefPtr message) override; void DidCreateSecondaryIsolate(Dart_Isolate isolate) override; + void DidShutdownMainIsolate() override; RuntimeDelegate* client_; std::string language_code_; diff --git a/engine/src/flutter/runtime/runtime_delegate.cc b/engine/src/flutter/runtime/runtime_delegate.cc index 6df41f0220..6ec55c4c2e 100644 --- a/engine/src/flutter/runtime/runtime_delegate.cc +++ b/engine/src/flutter/runtime/runtime_delegate.cc @@ -12,4 +12,6 @@ void RuntimeDelegate::DidCreateMainIsolate(Dart_Isolate isolate) {} void RuntimeDelegate::DidCreateSecondaryIsolate(Dart_Isolate isolate) {} +void RuntimeDelegate::DidShutdownMainIsolate() {} + } // namespace blink diff --git a/engine/src/flutter/runtime/runtime_delegate.h b/engine/src/flutter/runtime/runtime_delegate.h index 880ab83dab..88d1b217c6 100644 --- a/engine/src/flutter/runtime/runtime_delegate.h +++ b/engine/src/flutter/runtime/runtime_delegate.h @@ -25,6 +25,7 @@ class RuntimeDelegate { virtual void DidCreateMainIsolate(Dart_Isolate isolate); virtual void DidCreateSecondaryIsolate(Dart_Isolate isolate); + virtual void DidShutdownMainIsolate(); protected: virtual ~RuntimeDelegate();