Skip to content

Commit 677d10c

Browse files
addaleaxrvagg
authored andcommitted
worker: fix deadlock when calling terminate from exit handler
Just before we call the `'exit'` handlers of a Worker, we drain the public port’s message queue to ensure proper ordering. Previously, we held the Worker’s `mutex_` during the exit handler call, so calling `terminate()` on the worker could lead to a deadlock if called from one of those message handlers. This fixes flakiness in the `parallel/test-worker-dns-terminate` test. PR-URL: #22073 Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5018661 commit 677d10c

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/node_worker.cc

+12-12
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,21 @@ void Worker::JoinThread() {
287287
}
288288

289289
void Worker::OnThreadStopped() {
290-
Mutex::ScopedLock lock(mutex_);
291-
scheduled_on_thread_stopped_ = false;
290+
{
291+
Mutex::ScopedLock lock(mutex_);
292+
scheduled_on_thread_stopped_ = false;
292293

293-
Debug(this, "Worker %llu thread stopped", thread_id_);
294+
Debug(this, "Worker %llu thread stopped", thread_id_);
294295

295-
{
296-
Mutex::ScopedLock stopped_lock(stopped_mutex_);
297-
CHECK(stopped_);
298-
}
296+
{
297+
Mutex::ScopedLock stopped_lock(stopped_mutex_);
298+
CHECK(stopped_);
299+
}
299300

300-
CHECK_EQ(child_port_, nullptr);
301-
parent_port_ = nullptr;
301+
CHECK_EQ(child_port_, nullptr);
302+
parent_port_ = nullptr;
303+
}
302304

303-
// It's okay to join the thread while holding the mutex because
304-
// OnThreadStopped means it's no longer doing any work that might grab it
305-
// and really just silently exiting.
306305
JoinThread();
307306

308307
{
@@ -369,6 +368,7 @@ void Worker::StopThread(const FunctionCallbackInfo<Value>& args) {
369368
Worker* w;
370369
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
371370

371+
Debug(w, "Worker %llu is getting stopped by parent", w->thread_id_);
372372
w->Exit(1);
373373
w->JoinThread();
374374
}

0 commit comments

Comments
 (0)