Support cleaner Dart isolate shutdown handling. (flutter/engine#4096)

If an isolate shuts down (for example if an app calls
Isolate.current.kill()), the UIDartState* on DartController will refer
to a freed object. This wires through notification that the is shutting
down through to the DartController so it can clean up appropriately.
This commit is contained in:
Ian McKellar
2017-09-13 10:29:21 -07:00
committed by GitHub
parent e0ef51ea00
commit b8e222f45c
6 changed files with 22 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ class Window;
class IsolateClient {
public:
virtual void DidCreateSecondaryIsolate(Dart_Isolate isolate) = 0;
virtual void WillShutDownIsolate(Dart_Isolate isolate) = 0;
protected:
virtual ~IsolateClient();
@@ -35,6 +36,9 @@ class UIDartState : public tonic::DartState {
UIDartState* CreateForChildIsolate();
IsolateClient* isolate_client() { return isolate_client_; }
void set_isolate_client(IsolateClient* isolate_client) {
isolate_client_ = isolate_client;
}
Dart_Port main_port() const { return main_port_; }
const std::string& debug_name() const { return debug_name_; }
Window* window() const { return window_.get(); }

View File

@@ -49,6 +49,8 @@ DartController::DartController()
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.
@@ -215,4 +217,8 @@ void DartController::CreateIsolateFor(
Dart_ExitIsolate();
}
void DartController::IsolateShuttingDown() {
ui_dart_state_ = nullptr;
}
} // namespace blink

View File

@@ -35,6 +35,8 @@ class DartController {
UIDartState* dart_state() const { return ui_dart_state_; }
void IsolateShuttingDown();
private:
bool SendStartMessage(Dart_Handle root_library);

View File

@@ -129,7 +129,11 @@ void IsolateShutdownCallback(void* callback_data) {
Dart_Handle sticky_error = Dart_GetStickyError();
FXL_CHECK(LogIfError(sticky_error));
}
tonic::DartState* dart_state = static_cast<tonic::DartState*>(callback_data);
UIDartState* dart_state = static_cast<UIDartState*>(callback_data);
IsolateClient *isolate_client_ = dart_state->isolate_client();
if (isolate_client_) {
isolate_client_->WillShutDownIsolate(dart_state->isolate());
}
delete dart_state;
}

View File

@@ -134,6 +134,10 @@ void RuntimeController::DidCreateSecondaryIsolate(Dart_Isolate isolate) {
client_->DidCreateSecondaryIsolate(isolate);
}
void RuntimeController::WillShutDownIsolate(Dart_Isolate isolate) {
dart_controller_->IsolateShuttingDown();
}
Dart_Port RuntimeController::GetMainPort() {
if (!dart_controller_) {
return ILLEGAL_PORT;

View File

@@ -61,6 +61,7 @@ class RuntimeController : public WindowClient, public IsolateClient {
void HandlePlatformMessage(fxl::RefPtr<PlatformMessage> message) override;
void DidCreateSecondaryIsolate(Dart_Isolate isolate) override;
void WillShutDownIsolate(Dart_Isolate isolate) override;
RuntimeDelegate* client_;
std::string language_code_;