From c2ae4afb8f98c0180e9e67f8f1c7cf76cb88ce9c Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Fri, 4 Apr 2025 18:02:54 +0200 Subject: [PATCH] fix: destroy parent port backend when JS env exits (#46437) * fix: destroy parent port backend when JS env exits * fix: close parent port before destroying --------- Co-authored-by: deepak1556 --- shell/services/node/node_service.cc | 2 ++ shell/services/node/parent_port.cc | 4 ++-- shell/services/node/parent_port.h | 5 ++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/shell/services/node/node_service.cc b/shell/services/node/node_service.cc index c7a0140e39c9..fd42dedad309 100644 --- a/shell/services/node/node_service.cc +++ b/shell/services/node/node_service.cc @@ -96,6 +96,7 @@ NodeService::NodeService( NodeService::~NodeService() { if (!node_env_stopped_) { node_env_->set_trace_sync_io(false); + ParentPort::GetInstance()->Close(); js_env_->DestroyMicrotasksRunner(); node::Stop(node_env_.get(), node::StopFlags::kDoNotTerminateIsolate); } @@ -147,6 +148,7 @@ void NodeService::Initialize( node_env_.get(), [this](node::Environment* env, int exit_code) { // Destroy node platform. env->set_trace_sync_io(false); + ParentPort::GetInstance()->Close(); js_env_->DestroyMicrotasksRunner(); node::Stop(env, node::StopFlags::kDoNotTerminateIsolate); node_env_stopped_ = true; diff --git a/shell/services/node/parent_port.cc b/shell/services/node/parent_port.cc index 012a588474bd..9b1393a007e1 100644 --- a/shell/services/node/parent_port.cc +++ b/shell/services/node/parent_port.cc @@ -23,8 +23,8 @@ namespace electron { gin::WrapperInfo ParentPort::kWrapperInfo = {gin::kEmbedderNativeGin}; ParentPort* ParentPort::GetInstance() { - static base::NoDestructor instance; - return instance.get(); + static ParentPort* instance = new ParentPort(); + return instance; } ParentPort::ParentPort() = default; diff --git a/shell/services/node/parent_port.h b/shell/services/node/parent_port.h index b39d825c7521..fd950a34ea44 100644 --- a/shell/services/node/parent_port.h +++ b/shell/services/node/parent_port.h @@ -10,6 +10,7 @@ #include "gin/wrappable.h" #include "mojo/public/cpp/bindings/connector.h" #include "mojo/public/cpp/bindings/message.h" +#include "shell/common/gin_helper/cleaned_up_at_exit.h" #include "third_party/blink/public/common/messaging/message_port_descriptor.h" namespace v8 { @@ -31,6 +32,7 @@ namespace electron { // for the lifetime of a Utility Process which // also means that GC lifecycle is ignored by this class. class ParentPort final : public gin::Wrappable, + public gin_helper::CleanedUpAtExit, private mojo::MessageReceiver { public: static ParentPort* GetInstance(); @@ -49,9 +51,10 @@ class ParentPort final : public gin::Wrappable, v8::Isolate* isolate) override; const char* GetTypeName() override; + void Close(); + private: void PostMessage(v8::Local message_value); - void Close(); void Start(); void Pause();