Skip to content

inspector: make dispatching_messages_ atomic #56929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions src/inspector/main_thread_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,26 +240,32 @@ void MainThreadInterface::StopWaitingForFrontendEvent() {
}

void MainThreadInterface::DispatchMessages() {
if (dispatching_messages_)
return;
dispatching_messages_ = true;
bool had_messages = false;
do {
if (dispatching_message_queue_.empty()) {
Mutex::ScopedLock scoped_lock(requests_lock_);
requests_.swap(dispatching_message_queue_);
}
had_messages = !dispatching_message_queue_.empty();
while (!dispatching_message_queue_.empty()) {
MessageQueue::value_type task;
std::swap(dispatching_message_queue_.front(), task);
dispatching_message_queue_.pop_front();

v8::SealHandleScope seal_handle_scope(agent_->env()->isolate());
task->Call(this);
}
} while (had_messages);
dispatching_messages_ = false;
bool expected = false;
// compare_exchange_strong returns true if the value was successfully changed
// from false to true.
if (dispatching_messages_.compare_exchange_strong(
expected,
true,
std::memory_order_acquire,
std::memory_order_relaxed)) {
bool had_messages = false;
do {
if (dispatching_message_queue_.empty()) {
Mutex::ScopedLock scoped_lock(requests_lock_);
requests_.swap(dispatching_message_queue_);
}
had_messages = !dispatching_message_queue_.empty();
while (!dispatching_message_queue_.empty()) {
MessageQueue::value_type task;
std::swap(dispatching_message_queue_.front(), task);
dispatching_message_queue_.pop_front();

v8::SealHandleScope seal_handle_scope(agent_->env()->isolate());
Copy link
Member

@legendecas legendecas Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MainThreadInterface::DispatchMessages accesses the Isolate and can only be accessed on the JavaScript thread. The dispatching_messages_ is only a guard for re-entrance and is not intended to be used across multiple threads.

// We allow DispatchMessages reentry as we enter the pause. This is important
// to support debugging the code invoked by an inspector call, such
// as Runtime.evaluate

The MainThreadInterface::Post can be invoked from another IO thread, but not all methods are. I don't think making dispatching_messages_ is necessary.

task->Call(this);
}
} while (had_messages);
dispatching_messages_.store(false, std::memory_order_release);
}
}

std::shared_ptr<MainThreadHandle> MainThreadInterface::GetHandle() {
Expand Down
2 changes: 1 addition & 1 deletion src/inspector/main_thread_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class MainThreadInterface :
// This queue is to maintain the order of the messages for the cases
// when we reenter the DispatchMessages function.
MessageQueue dispatching_message_queue_;
bool dispatching_messages_ = false;
std::atomic<bool> dispatching_messages_ = false;
// This flag indicates an internal request to exit the loop in
// WaitForFrontendEvent(). It's set to true by calling
// StopWaitingForFrontendEvent().
Expand Down
Loading