@@ -159,6 +159,7 @@ class AgentImpl {
159159
160160 static void ThreadCbIO (void * agent);
161161 static void WriteCbIO (uv_async_t * async);
162+ static void MainThreadAsyncCb (uv_async_t * req);
162163
163164 void InstallInspectorOnProcess ();
164165
@@ -190,6 +191,7 @@ class AgentImpl {
190191 node::Environment* parent_env_;
191192
192193 uv_async_t io_thread_req_;
194+ uv_async_t main_thread_req_;
193195 V8NodeInspector* inspector_;
194196 v8::Platform* platform_;
195197 MessageQueue<InspectorAction> incoming_message_queue_;
@@ -334,6 +336,9 @@ AgentImpl::AgentImpl(Environment* env) : delegate_(nullptr),
334336 dispatching_messages_(false ),
335337 session_id_(0 ),
336338 server_(nullptr ) {
339+ CHECK_EQ (0 , uv_async_init (env->event_loop (), &main_thread_req_,
340+ AgentImpl::MainThreadAsyncCb));
341+ uv_unref (reinterpret_cast <uv_handle_t *>(&main_thread_req_));
337342 CHECK_EQ (0 , uv_sem_init (&start_sem_, 0 ));
338343 memset (&io_thread_req_, 0 , sizeof (io_thread_req_));
339344}
@@ -416,10 +421,7 @@ bool AgentImpl::Start(v8::Platform* platform, const char* path,
416421
417422 InstallInspectorOnProcess ();
418423
419- int err = uv_loop_init (&child_loop_);
420- CHECK_EQ (err, 0 );
421-
422- err = uv_thread_create (&thread_, AgentImpl::ThreadCbIO, this );
424+ int err = uv_thread_create (&thread_, AgentImpl::ThreadCbIO, this );
423425 CHECK_EQ (err, 0 );
424426 uv_sem_wait (&start_sem_);
425427
@@ -606,6 +608,7 @@ void AgentImpl::PostIncomingMessage(InspectorAction action, int session_id,
606608 platform_->CallOnForegroundThread (isolate,
607609 new DispatchOnInspectorBackendTask (this ));
608610 isolate->RequestInterrupt (InterruptCallback, this );
611+ CHECK_EQ (0 , uv_async_send (&main_thread_req_));
609612 }
610613 NotifyMessageReceived ();
611614}
@@ -662,6 +665,12 @@ void AgentImpl::DispatchMessages() {
662665 dispatching_messages_ = false ;
663666}
664667
668+ // static
669+ void AgentImpl::MainThreadAsyncCb (uv_async_t * req) {
670+ AgentImpl* agent = node::ContainerOf (&AgentImpl::main_thread_req_, req);
671+ agent->DispatchMessages ();
672+ }
673+
665674void AgentImpl::Write (TransportAction action, int session_id,
666675 const StringView& inspector_message) {
667676 AppendMessage (&outgoing_message_queue_, action, session_id,
0 commit comments