Skip to content

Commit 8df7256

Browse files
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.
1 parent 98e808f commit 8df7256

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎test-app/runtime/src/main/cpp/WorkerInspectorClient.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ std::string ToUtf8String(const StringView& view) {
3434
} // namespace
3535

3636
WorkerInspectorClient::WorkerInspectorClient(int workerId, Isolate* isolate, ALooper* workerLooper,
37-
const std::string& url)
37+
const std::string& url,
38+
const std::atomic_bool& workerTerminating)
3839
: workerId_(workerId),
3940
sessionId_("NS_WORKER_" + std::to_string(workerId)),
4041
targetId_("ns-worker-" + std::to_string(workerId)),
4142
url_(url),
4243
isolate_(isolate),
43-
workerLooper_(workerLooper) {
44+
workerLooper_(workerLooper),
45+
workerTerminating_(workerTerminating) {
4446
// Wakes the worker looper when CDP messages arrive on the socket thread;
4547
// same mechanism as the worker's message inbox (ConcurrentQueue).
4648
eventFd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
@@ -206,13 +208,13 @@ void WorkerInspectorClient::MaybeResetSession() {
206208
}
207209

208210
void WorkerInspectorClient::runMessageLoopOnPause(int contextGroupId) {
209-
if (runningPauseLoop_.load(std::memory_order_acquire) || dying_) {
211+
if (runningPauseLoop_.load(std::memory_order_acquire) || dying_ || workerTerminating_) {
210212
return;
211213
}
212214
runningPauseLoop_.store(true, std::memory_order_release);
213215
pauseTerminated_ = false;
214216

215-
while (!pauseTerminated_ && !dying_) {
217+
while (!pauseTerminated_ && !dying_ && !workerTerminating_) {
216218
std::string message = this->PopMessage();
217219
bool shouldWait = message.empty();
218220
if (!shouldWait) {
@@ -225,8 +227,10 @@ void WorkerInspectorClient::runMessageLoopOnPause(int contextGroupId) {
225227
->GetEventLoop(isolate_)
226228
->RunNestableV8Tasks();
227229

228-
if (shouldWait && !pauseTerminated_ && !dying_) {
230+
if (shouldWait && !pauseTerminated_ && !dying_ && !workerTerminating_) {
229231
std::unique_lock<std::mutex> lock(messageArrivedMutex_);
232+
// Timed on purpose: nothing notifies when workerTerminating_ is
233+
// set, so the timeout is what brings the loop back to read it.
230234
messageArrived_.wait_for(lock, std::chrono::milliseconds(1));
231235
}
232236
}

‎test-app/runtime/src/main/cpp/WorkerInspectorClient.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ class WorkerInspectorClient final : public v8_inspector::V8InspectorClient,
3535
public v8_inspector::V8Inspector::Channel {
3636
public:
3737
// Worker thread, with the worker isolate locked and its context created.
38+
// `workerTerminating` is the worker's own termination flag; the
39+
// worker outlives this client.
3840
WorkerInspectorClient(int workerId, v8::Isolate* isolate, ALooper* workerLooper,
39-
const std::string& url);
41+
const std::string& url, const std::atomic_bool& workerTerminating);
4042
~WorkerInspectorClient() override;
4143

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

128130
std::atomic<bool> dying_{false};
129131
std::atomic<bool> pauseTerminated_{false};
132+
// Read by the pause loop, which leaves on it without NotifyTerminating:
133+
// a pause entered while WorkerWrapper::ConsoleLog holds
134+
// inspectorMutex_ would otherwise keep Terminate() waiting for that
135+
// mutex.
136+
const std::atomic_bool& workerTerminating_;
130137
std::atomic<bool> runningPauseLoop_{false}; // written on the worker thread only
131138
bool pendingReset_ = false; // worker thread only
132139
};

‎test-app/runtime/src/main/cpp/WorkerWrapper.cpp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ void WorkerWrapper::Terminate() {
264264
{
265265
// A worker paused at a breakpoint sits in the inspector's nested pause
266266
// loop, not in Looper.loop() - kick it loose so TerminateExecution and
267-
// the looper quit below can take effect.
267+
// the looper quit below can take effect. A pause entered while
268+
// ConsoleLog holds inspectorMutex_ has already left on isTerminating_,
269+
// set above: this lock is only granted once that ConsoleLog returns.
268270
std::lock_guard<std::recursive_mutex> lock(inspectorMutex_);
269271
if (inspector_ != nullptr) {
270272
inspector_->NotifyTerminating();
@@ -950,7 +952,8 @@ void WorkerWrapper::CreateInspector(Isolate* isolate) {
950952
? workerPath_
951953
: "file://" + workerPath_;
952954

953-
auto* client = new WorkerInspectorClient(workerId_, isolate, ALooper_forThread(), url);
955+
auto* client = new WorkerInspectorClient(workerId_, isolate, ALooper_forThread(), url,
956+
isTerminating_);
954957
{
955958
std::lock_guard<std::recursive_mutex> lock(inspectorMutex_);
956959
inspector_ = client;

0 commit comments

Comments
 (0)