@@ -197,7 +197,7 @@ class AgentImpl {
197
197
void SetConnected (bool connected);
198
198
void DispatchMessages ();
199
199
void Write (int session_id, const String16& message);
200
- void AppendMessage (MessageQueue* vector, int session_id,
200
+ bool AppendMessage (MessageQueue* vector, int session_id,
201
201
const String16& message);
202
202
void SwapBehindLock (MessageQueue* vector1, MessageQueue* vector2);
203
203
void PostIncomingMessage (const String16& message);
@@ -666,10 +666,12 @@ void AgentImpl::WorkerRunIO() {
666
666
CHECK_EQ (err, 0 );
667
667
}
668
668
669
- void AgentImpl::AppendMessage (MessageQueue* queue, int session_id,
669
+ bool AgentImpl::AppendMessage (MessageQueue* queue, int session_id,
670
670
const String16& message) {
671
671
Mutex::ScopedLock scoped_lock (queue_lock_);
672
+ bool trigger_pumping = queue->empty ();
672
673
queue->push_back (std::make_pair (session_id, message));
674
+ return trigger_pumping;
673
675
}
674
676
675
677
void AgentImpl::SwapBehindLock (MessageQueue* vector1, MessageQueue* vector2) {
@@ -678,12 +680,13 @@ void AgentImpl::SwapBehindLock(MessageQueue* vector1, MessageQueue* vector2) {
678
680
}
679
681
680
682
void AgentImpl::PostIncomingMessage (const String16& message) {
681
- AppendMessage (&incoming_message_queue_, frontend_session_id_, message);
682
- v8::Isolate* isolate = parent_env_->isolate ();
683
- platform_->CallOnForegroundThread (isolate,
684
- new DispatchOnInspectorBackendTask (this ));
685
- isolate->RequestInterrupt (InterruptCallback, this );
686
- uv_async_send (data_written_);
683
+ if (AppendMessage (&incoming_message_queue_, frontend_session_id_, message)) {
684
+ v8::Isolate* isolate = parent_env_->isolate ();
685
+ platform_->CallOnForegroundThread (isolate,
686
+ new DispatchOnInspectorBackendTask (this ));
687
+ isolate->RequestInterrupt (InterruptCallback, this );
688
+ uv_async_send (data_written_);
689
+ }
687
690
}
688
691
689
692
void AgentImpl::OnInspectorConnectionIO (inspector_socket_t * socket) {
@@ -698,33 +701,40 @@ void AgentImpl::OnInspectorConnectionIO(inspector_socket_t* socket) {
698
701
}
699
702
700
703
void AgentImpl::DispatchMessages () {
704
+ // This function can be reentered if there was an incoming message while
705
+ // V8 was processing another inspector request (e.g. if the user is
706
+ // evaluating a long-running JS code snippet). This can happen only at
707
+ // specific points (e.g. the lines that call inspector_ methods)
701
708
if (dispatching_messages_)
702
709
return ;
703
710
dispatching_messages_ = true ;
704
711
MessageQueue tasks;
705
- SwapBehindLock (&incoming_message_queue_, &tasks);
706
- for (const MessageQueue::value_type& pair : tasks) {
707
- const String16& message = pair.second ;
708
- if (message == TAG_CONNECT) {
709
- CHECK_EQ (State::kAccepting , state_);
710
- backend_session_id_++;
711
- state_ = State::kConnected ;
712
- fprintf (stderr, " Debugger attached.\n " );
713
- inspector_->connectFrontend ();
714
- } else if (message == TAG_DISCONNECT) {
715
- CHECK_EQ (State::kConnected , state_);
716
- if (shutting_down_) {
717
- state_ = State::kDone ;
712
+ do {
713
+ tasks.clear ();
714
+ SwapBehindLock (&incoming_message_queue_, &tasks);
715
+ for (const MessageQueue::value_type& pair : tasks) {
716
+ const String16& message = pair.second ;
717
+ if (message == TAG_CONNECT) {
718
+ CHECK_EQ (State::kAccepting , state_);
719
+ backend_session_id_++;
720
+ state_ = State::kConnected ;
721
+ fprintf (stderr, " Debugger attached.\n " );
722
+ inspector_->connectFrontend ();
723
+ } else if (message == TAG_DISCONNECT) {
724
+ CHECK_EQ (State::kConnected , state_);
725
+ if (shutting_down_) {
726
+ state_ = State::kDone ;
727
+ } else {
728
+ PrintDebuggerReadyMessage (port_);
729
+ state_ = State::kAccepting ;
730
+ }
731
+ inspector_->quitMessageLoopOnPause ();
732
+ inspector_->disconnectFrontend ();
718
733
} else {
719
- PrintDebuggerReadyMessage (port_);
720
- state_ = State::kAccepting ;
734
+ inspector_->dispatchMessageFromFrontend (message);
721
735
}
722
- inspector_->quitMessageLoopOnPause ();
723
- inspector_->disconnectFrontend ();
724
- } else {
725
- inspector_->dispatchMessageFromFrontend (message);
726
736
}
727
- }
737
+ } while (!tasks. empty ());
728
738
uv_async_send (data_written_);
729
739
dispatching_messages_ = false ;
730
740
}
0 commit comments