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 <hop2deep@gmail.com>
This commit is contained in:
Shelley Vohr 2025-04-04 18:02:54 +02:00 committed by GitHub
parent e81e3acd2d
commit c2ae4afb8f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 3 deletions

View file

@ -96,6 +96,7 @@ NodeService::NodeService(
NodeService::~NodeService() { NodeService::~NodeService() {
if (!node_env_stopped_) { if (!node_env_stopped_) {
node_env_->set_trace_sync_io(false); node_env_->set_trace_sync_io(false);
ParentPort::GetInstance()->Close();
js_env_->DestroyMicrotasksRunner(); js_env_->DestroyMicrotasksRunner();
node::Stop(node_env_.get(), node::StopFlags::kDoNotTerminateIsolate); 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) { node_env_.get(), [this](node::Environment* env, int exit_code) {
// Destroy node platform. // Destroy node platform.
env->set_trace_sync_io(false); env->set_trace_sync_io(false);
ParentPort::GetInstance()->Close();
js_env_->DestroyMicrotasksRunner(); js_env_->DestroyMicrotasksRunner();
node::Stop(env, node::StopFlags::kDoNotTerminateIsolate); node::Stop(env, node::StopFlags::kDoNotTerminateIsolate);
node_env_stopped_ = true; node_env_stopped_ = true;

View file

@ -23,8 +23,8 @@ namespace electron {
gin::WrapperInfo ParentPort::kWrapperInfo = {gin::kEmbedderNativeGin}; gin::WrapperInfo ParentPort::kWrapperInfo = {gin::kEmbedderNativeGin};
ParentPort* ParentPort::GetInstance() { ParentPort* ParentPort::GetInstance() {
static base::NoDestructor<ParentPort> instance; static ParentPort* instance = new ParentPort();
return instance.get(); return instance;
} }
ParentPort::ParentPort() = default; ParentPort::ParentPort() = default;

View file

@ -10,6 +10,7 @@
#include "gin/wrappable.h" #include "gin/wrappable.h"
#include "mojo/public/cpp/bindings/connector.h" #include "mojo/public/cpp/bindings/connector.h"
#include "mojo/public/cpp/bindings/message.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" #include "third_party/blink/public/common/messaging/message_port_descriptor.h"
namespace v8 { namespace v8 {
@ -31,6 +32,7 @@ namespace electron {
// for the lifetime of a Utility Process which // for the lifetime of a Utility Process which
// also means that GC lifecycle is ignored by this class. // also means that GC lifecycle is ignored by this class.
class ParentPort final : public gin::Wrappable<ParentPort>, class ParentPort final : public gin::Wrappable<ParentPort>,
public gin_helper::CleanedUpAtExit,
private mojo::MessageReceiver { private mojo::MessageReceiver {
public: public:
static ParentPort* GetInstance(); static ParentPort* GetInstance();
@ -49,9 +51,10 @@ class ParentPort final : public gin::Wrappable<ParentPort>,
v8::Isolate* isolate) override; v8::Isolate* isolate) override;
const char* GetTypeName() override; const char* GetTypeName() override;
void Close();
private: private:
void PostMessage(v8::Local<v8::Value> message_value); void PostMessage(v8::Local<v8::Value> message_value);
void Close();
void Start(); void Start();
void Pause(); void Pause();