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;