diff --git a/src/inspector/main_thread_interface.cc b/src/inspector/main_thread_interface.cc index ac4461baed4afd..48a964d564fec2 100644 --- a/src/inspector/main_thread_interface.cc +++ b/src/inspector/main_thread_interface.cc @@ -87,15 +87,15 @@ class CallRequest : public Request { class DispatchMessagesTask : public v8::Task { public: - explicit DispatchMessagesTask(MainThreadInterface* thread) + explicit DispatchMessagesTask(std::weak_ptr thread) : thread_(thread) {} void Run() override { - thread_->DispatchMessages(); + if (auto thread = thread_.lock()) thread->DispatchMessages(); } private: - MainThreadInterface* thread_; + std::weak_ptr thread_; }; template @@ -231,11 +231,16 @@ void MainThreadInterface::Post(std::unique_ptr request) { if (needs_notify) { if (isolate_ != nullptr && platform_ != nullptr) { std::shared_ptr taskrunner = - platform_->GetForegroundTaskRunner(isolate_); - taskrunner->PostTask(std::make_unique(this)); - isolate_->RequestInterrupt([](v8::Isolate* isolate, void* thread) { - static_cast(thread)->DispatchMessages(); - }, this); + platform_->GetForegroundTaskRunner(isolate_); + std::weak_ptr* interface_ptr = + new std::weak_ptr(shared_from_this()); + taskrunner->PostTask( + std::make_unique(*interface_ptr)); + isolate_->RequestInterrupt([](v8::Isolate* isolate, void* opaque) { + std::unique_ptr> interface_ptr { + static_cast*>(opaque) }; + if (auto iface = interface_ptr->lock()) iface->DispatchMessages(); + }, static_cast(interface_ptr)); } } incoming_message_cond_.Broadcast(scoped_lock); diff --git a/src/inspector/main_thread_interface.h b/src/inspector/main_thread_interface.h index 9a48192cd374e5..bcea19f3f3937e 100644 --- a/src/inspector/main_thread_interface.h +++ b/src/inspector/main_thread_interface.h @@ -70,7 +70,8 @@ class MainThreadHandle : public std::enable_shared_from_this { friend class MainThreadInterface; }; -class MainThreadInterface { +class MainThreadInterface : + public std::enable_shared_from_this { public: MainThreadInterface(Agent* agent, uv_loop_t*, v8::Isolate* isolate, v8::Platform* platform); diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index cf0110144a535d..fb3415c275e6db 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -665,10 +665,10 @@ class NodeInspectorClient : public V8InspectorClient { } std::shared_ptr getThreadHandle() { - if (interface_ == nullptr) { - interface_.reset(new MainThreadInterface( + if (!interface_) { + interface_ = std::make_shared( env_->inspector_agent(), env_->event_loop(), env_->isolate(), - env_->isolate_data()->platform())); + env_->isolate_data()->platform()); } return interface_->GetHandle(); } @@ -739,7 +739,7 @@ class NodeInspectorClient : public V8InspectorClient { bool waiting_for_frontend_ = false; bool waiting_for_sessions_disconnect_ = false; // Allows accessing Inspector from non-main threads - std::unique_ptr interface_; + std::shared_ptr interface_; std::shared_ptr worker_manager_; };