Skip to content
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
14 changes: 9 additions & 5 deletions test-app/runtime/src/main/cpp/WorkerInspectorClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ std::string ToUtf8String(const StringView& view) {
} // namespace

WorkerInspectorClient::WorkerInspectorClient(int workerId, Isolate* isolate, ALooper* workerLooper,
const std::string& url)
const std::string& url,
const std::atomic_bool& workerTerminating)
: workerId_(workerId),
sessionId_("NS_WORKER_" + std::to_string(workerId)),
targetId_("ns-worker-" + std::to_string(workerId)),
url_(url),
isolate_(isolate),
workerLooper_(workerLooper) {
workerLooper_(workerLooper),
workerTerminating_(workerTerminating) {
// Wakes the worker looper when CDP messages arrive on the socket thread;
// same mechanism as the worker's message inbox (ConcurrentQueue).
eventFd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
Expand Down Expand Up @@ -206,13 +208,13 @@ void WorkerInspectorClient::MaybeResetSession() {
}

void WorkerInspectorClient::runMessageLoopOnPause(int contextGroupId) {
if (runningPauseLoop_.load(std::memory_order_acquire) || dying_) {
if (runningPauseLoop_.load(std::memory_order_acquire) || dying_ || workerTerminating_) {
return;
}
runningPauseLoop_.store(true, std::memory_order_release);
pauseTerminated_ = false;

while (!pauseTerminated_ && !dying_) {
while (!pauseTerminated_ && !dying_ && !workerTerminating_) {
std::string message = this->PopMessage();
bool shouldWait = message.empty();
if (!shouldWait) {
Expand All @@ -225,8 +227,10 @@ void WorkerInspectorClient::runMessageLoopOnPause(int contextGroupId) {
->GetEventLoop(isolate_)
->RunNestableV8Tasks();

if (shouldWait && !pauseTerminated_ && !dying_) {
if (shouldWait && !pauseTerminated_ && !dying_ && !workerTerminating_) {
std::unique_lock<std::mutex> lock(messageArrivedMutex_);
// Timed on purpose: nothing notifies when workerTerminating_ is
// set, so the timeout is what brings the loop back to read it.
messageArrived_.wait_for(lock, std::chrono::milliseconds(1));
}
}
Expand Down
9 changes: 8 additions & 1 deletion test-app/runtime/src/main/cpp/WorkerInspectorClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ class WorkerInspectorClient final : public v8_inspector::V8InspectorClient,
public v8_inspector::V8Inspector::Channel {
public:
// Worker thread, with the worker isolate locked and its context created.
// `workerTerminating` is the worker's own termination flag; the
// worker outlives this client.
WorkerInspectorClient(int workerId, v8::Isolate* isolate, ALooper* workerLooper,
const std::string& url);
const std::string& url, const std::atomic_bool& workerTerminating);
~WorkerInspectorClient() override;

int WorkerId() const {
Expand Down Expand Up @@ -127,6 +129,11 @@ class WorkerInspectorClient final : public v8_inspector::V8InspectorClient,

std::atomic<bool> dying_{false};
std::atomic<bool> pauseTerminated_{false};
// Read by the pause loop, which leaves on it without NotifyTerminating:
// a pause entered while WorkerWrapper::ConsoleLog holds
// inspectorMutex_ would otherwise keep Terminate() waiting for that
// mutex.
const std::atomic_bool& workerTerminating_;
std::atomic<bool> runningPauseLoop_{false}; // written on the worker thread only
bool pendingReset_ = false; // worker thread only
};
Expand Down
7 changes: 5 additions & 2 deletions test-app/runtime/src/main/cpp/WorkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ void WorkerWrapper::Terminate() {
{
// A worker paused at a breakpoint sits in the inspector's nested pause
// loop, not in Looper.loop() - kick it loose so TerminateExecution and
// the looper quit below can take effect.
// the looper quit below can take effect. A pause entered while
// ConsoleLog holds inspectorMutex_ has already left on isTerminating_,
// set above: this lock is only granted once that ConsoleLog returns.
std::lock_guard<std::recursive_mutex> lock(inspectorMutex_);
if (inspector_ != nullptr) {
inspector_->NotifyTerminating();
Expand Down Expand Up @@ -950,7 +952,8 @@ void WorkerWrapper::CreateInspector(Isolate* isolate) {
? workerPath_
: "file://" + workerPath_;

auto* client = new WorkerInspectorClient(workerId_, isolate, ALooper_forThread(), url);
auto* client = new WorkerInspectorClient(workerId_, isolate, ALooper_forThread(), url,
isTerminating_);
{
std::lock_guard<std::recursive_mutex> lock(inspectorMutex_);
inspector_ = client;
Expand Down
Loading