From b4d903402b63b19caa3c7c56ada761e627283e85 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 14 May 2020 09:12:27 +0200 Subject: [PATCH] worker: fix crash when .unref() is called during exit To be more precise, fix a crash when `worker.unref()` is called from a message on the Worker that is not emitted before the Worker thread has stopped. PR-URL: https://github.com/nodejs/node/pull/33394 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- src/node_worker.cc | 4 ++-- ...test-worker-unref-from-message-during-exit.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-worker-unref-from-message-during-exit.js diff --git a/src/node_worker.cc b/src/node_worker.cc index d184d16ac8ee8b..32ee9b627a1a8b 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -680,7 +680,7 @@ void Worker::StopThread(const FunctionCallbackInfo& args) { void Worker::Ref(const FunctionCallbackInfo& args) { Worker* w; ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); - if (!w->has_ref_) { + if (!w->has_ref_ && !w->thread_joined_) { w->has_ref_ = true; w->env()->add_refs(1); } @@ -689,7 +689,7 @@ void Worker::Ref(const FunctionCallbackInfo& args) { void Worker::Unref(const FunctionCallbackInfo& args) { Worker* w; ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); - if (w->has_ref_) { + if (w->has_ref_ && !w->thread_joined_) { w->has_ref_ = false; w->env()->add_refs(-1); } diff --git a/test/parallel/test-worker-unref-from-message-during-exit.js b/test/parallel/test-worker-unref-from-message-during-exit.js new file mode 100644 index 00000000000000..3e696f369e4179 --- /dev/null +++ b/test/parallel/test-worker-unref-from-message-during-exit.js @@ -0,0 +1,16 @@ +'use strict'; +const common = require('../common'); +const { Worker } = require('worker_threads'); + +// This used to crash because the `.unref()` was unexpected while the Worker +// was exiting. + +const w = new Worker(` +require('worker_threads').parentPort.postMessage({}); +`, { eval: true }); +w.on('message', common.mustCall(() => { + w.unref(); +})); + +// Wait a bit so that the 'message' event is emitted while the Worker exits. +Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 100);