Skip to content

Commit f01c5c5

Browse files
addaleaxtargos
authored andcommitted
inspector: turn platform tasks that outlive Agent into no-ops
Turn tasks scheduled on the `v8::Isolate` or on the given platform into no-ops if the underlying `MainThreadInterface` has gone away before the task could be run (which would happen when the `Environment` instance and with it the `inspector::Agent` instance are destroyed). This addresses an issue that Electron has been having with inspector support, and generally just seems like the right thing to do, as we may not fully be in control of the relative timing of Environment teardown, platform tasksexecution, and the execution of `RequestInterrupt()` callbacks (although the former two always happen in the same order in our own code). PR-URL: #30031 Reviewed-By: Eugene Ostroukhov <[email protected]> Reviewed-By: Shelley Vohr <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 5ca5864 commit f01c5c5

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/inspector/main_thread_interface.cc

+13-8
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ class CallRequest : public Request {
8787

8888
class DispatchMessagesTask : public v8::Task {
8989
public:
90-
explicit DispatchMessagesTask(MainThreadInterface* thread)
90+
explicit DispatchMessagesTask(std::weak_ptr<MainThreadInterface> thread)
9191
: thread_(thread) {}
9292

9393
void Run() override {
94-
thread_->DispatchMessages();
94+
if (auto thread = thread_.lock()) thread->DispatchMessages();
9595
}
9696

9797
private:
98-
MainThreadInterface* thread_;
98+
std::weak_ptr<MainThreadInterface> thread_;
9999
};
100100

101101
template <typename T>
@@ -231,11 +231,16 @@ void MainThreadInterface::Post(std::unique_ptr<Request> request) {
231231
if (needs_notify) {
232232
if (isolate_ != nullptr && platform_ != nullptr) {
233233
std::shared_ptr<v8::TaskRunner> taskrunner =
234-
platform_->GetForegroundTaskRunner(isolate_);
235-
taskrunner->PostTask(std::make_unique<DispatchMessagesTask>(this));
236-
isolate_->RequestInterrupt([](v8::Isolate* isolate, void* thread) {
237-
static_cast<MainThreadInterface*>(thread)->DispatchMessages();
238-
}, this);
234+
platform_->GetForegroundTaskRunner(isolate_);
235+
std::weak_ptr<MainThreadInterface>* interface_ptr =
236+
new std::weak_ptr<MainThreadInterface>(shared_from_this());
237+
taskrunner->PostTask(
238+
std::make_unique<DispatchMessagesTask>(*interface_ptr));
239+
isolate_->RequestInterrupt([](v8::Isolate* isolate, void* opaque) {
240+
std::unique_ptr<std::weak_ptr<MainThreadInterface>> interface_ptr {
241+
static_cast<std::weak_ptr<MainThreadInterface>*>(opaque) };
242+
if (auto iface = interface_ptr->lock()) iface->DispatchMessages();
243+
}, static_cast<void*>(interface_ptr));
239244
}
240245
}
241246
incoming_message_cond_.Broadcast(scoped_lock);

src/inspector/main_thread_interface.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class MainThreadHandle : public std::enable_shared_from_this<MainThreadHandle> {
7070
friend class MainThreadInterface;
7171
};
7272

73-
class MainThreadInterface {
73+
class MainThreadInterface :
74+
public std::enable_shared_from_this<MainThreadInterface> {
7475
public:
7576
MainThreadInterface(Agent* agent, uv_loop_t*, v8::Isolate* isolate,
7677
v8::Platform* platform);

src/inspector_agent.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,10 @@ class NodeInspectorClient : public V8InspectorClient {
665665
}
666666

667667
std::shared_ptr<MainThreadHandle> getThreadHandle() {
668-
if (interface_ == nullptr) {
669-
interface_.reset(new MainThreadInterface(
668+
if (!interface_) {
669+
interface_ = std::make_shared<MainThreadInterface>(
670670
env_->inspector_agent(), env_->event_loop(), env_->isolate(),
671-
env_->isolate_data()->platform()));
671+
env_->isolate_data()->platform());
672672
}
673673
return interface_->GetHandle();
674674
}
@@ -739,7 +739,7 @@ class NodeInspectorClient : public V8InspectorClient {
739739
bool waiting_for_frontend_ = false;
740740
bool waiting_for_sessions_disconnect_ = false;
741741
// Allows accessing Inspector from non-main threads
742-
std::unique_ptr<MainThreadInterface> interface_;
742+
std::shared_ptr<MainThreadInterface> interface_;
743743
std::shared_ptr<WorkerManager> worker_manager_;
744744
};
745745

0 commit comments

Comments
 (0)