From 8df72564c45e257d290ba24f21ef542c8b4cbdd1 Mon Sep 17 00:00:00 2001 From: Adrian Niculescu <15037449+adrian-niculescu@users.noreply.github.com> Date: Wed, 7 Oct 2026 02:18:58 +0300 Subject: [PATCH] fix(worker): leave the inspector pause loop when the worker is terminating A breakpoint hit while console.log converts its arguments pauses the worker inside ConsoleLog, which holds the worker's inspector mutex. terminate() could only end that pause through NotifyTerminating, which takes the same mutex, so the parent's terminate() blocked until the debugger resumed. The pause loop now also leaves on the worker's own termination flag, which terminate() sets before it takes the mutex. --- .../runtime/src/main/cpp/WorkerInspectorClient.cpp | 14 +++++++++----- .../runtime/src/main/cpp/WorkerInspectorClient.h | 9 ++++++++- test-app/runtime/src/main/cpp/WorkerWrapper.cpp | 7 +++++-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/test-app/runtime/src/main/cpp/WorkerInspectorClient.cpp b/test-app/runtime/src/main/cpp/WorkerInspectorClient.cpp index fbe7a7192..b4873989b 100644 --- a/test-app/runtime/src/main/cpp/WorkerInspectorClient.cpp +++ b/test-app/runtime/src/main/cpp/WorkerInspectorClient.cpp @@ -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); @@ -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) { @@ -225,8 +227,10 @@ void WorkerInspectorClient::runMessageLoopOnPause(int contextGroupId) { ->GetEventLoop(isolate_) ->RunNestableV8Tasks(); - if (shouldWait && !pauseTerminated_ && !dying_) { + if (shouldWait && !pauseTerminated_ && !dying_ && !workerTerminating_) { std::unique_lock 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)); } } diff --git a/test-app/runtime/src/main/cpp/WorkerInspectorClient.h b/test-app/runtime/src/main/cpp/WorkerInspectorClient.h index 6cfcee6b2..7b8ff56d6 100644 --- a/test-app/runtime/src/main/cpp/WorkerInspectorClient.h +++ b/test-app/runtime/src/main/cpp/WorkerInspectorClient.h @@ -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 { @@ -127,6 +129,11 @@ class WorkerInspectorClient final : public v8_inspector::V8InspectorClient, std::atomic dying_{false}; std::atomic 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 runningPauseLoop_{false}; // written on the worker thread only bool pendingReset_ = false; // worker thread only }; diff --git a/test-app/runtime/src/main/cpp/WorkerWrapper.cpp b/test-app/runtime/src/main/cpp/WorkerWrapper.cpp index 6b257896a..93f798592 100644 --- a/test-app/runtime/src/main/cpp/WorkerWrapper.cpp +++ b/test-app/runtime/src/main/cpp/WorkerWrapper.cpp @@ -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 lock(inspectorMutex_); if (inspector_ != nullptr) { inspector_->NotifyTerminating(); @@ -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 lock(inspectorMutex_); inspector_ = client;