From 2fe8a79ea0f439c46a641f9ec5b76017be21f39d Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Wed, 20 Apr 2022 04:57:14 +0530 Subject: [PATCH] worker: add hasRef() to the handle object This should help projects like https://github.com/mafintosh/why-is-node-running and https://github.com/facebook/jest to detect if Worker instances are keeping the event loop active correctly. Fixes: https://github.com/nodejs/node/issues/42091 Refs: https://github.com/mafintosh/why-is-node-running/issues/59 Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/42756 Reviewed-By: Anna Henningsen Reviewed-By: Antoine du Hamel --- src/node_worker.cc | 8 +++++++ src/node_worker.h | 1 + test/parallel/test-worker-hasref.js | 33 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 test/parallel/test-worker-hasref.js diff --git a/src/node_worker.cc b/src/node_worker.cc index 752fc48141f5e1..02420cd6327047 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -676,6 +676,12 @@ void Worker::Ref(const FunctionCallbackInfo& args) { } } +void Worker::HasRef(const FunctionCallbackInfo& args) { + Worker* w; + ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); + args.GetReturnValue().Set(w->has_ref_); +} + void Worker::Unref(const FunctionCallbackInfo& args) { Worker* w; ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); @@ -838,6 +844,7 @@ void InitWorker(Local target, env->SetProtoMethod(w, "startThread", Worker::StartThread); env->SetProtoMethod(w, "stopThread", Worker::StopThread); + env->SetProtoMethod(w, "hasRef", Worker::HasRef); env->SetProtoMethod(w, "ref", Worker::Ref); env->SetProtoMethod(w, "unref", Worker::Unref); env->SetProtoMethod(w, "getResourceLimits", Worker::GetResourceLimits); @@ -901,6 +908,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(Worker::New); registry->Register(Worker::StartThread); registry->Register(Worker::StopThread); + registry->Register(Worker::HasRef); registry->Register(Worker::Ref); registry->Register(Worker::Unref); registry->Register(Worker::GetResourceLimits); diff --git a/src/node_worker.h b/src/node_worker.h index 077d2b8390e6f8..cc3d24188ef735 100644 --- a/src/node_worker.h +++ b/src/node_worker.h @@ -60,6 +60,7 @@ class Worker : public AsyncWrap { static void SetEnvVars(const v8::FunctionCallbackInfo& args); static void StartThread(const v8::FunctionCallbackInfo& args); static void StopThread(const v8::FunctionCallbackInfo& args); + static void HasRef(const v8::FunctionCallbackInfo& args); static void Ref(const v8::FunctionCallbackInfo& args); static void Unref(const v8::FunctionCallbackInfo& args); static void GetResourceLimits( diff --git a/test/parallel/test-worker-hasref.js b/test/parallel/test-worker-hasref.js new file mode 100644 index 00000000000000..51593b14725f5b --- /dev/null +++ b/test/parallel/test-worker-hasref.js @@ -0,0 +1,33 @@ +'use strict'; +const common = require('../common'); + +const { Worker } = require('worker_threads'); +const { createHook } = require('async_hooks'); +const { strictEqual } = require('assert'); + +let handle; + +createHook({ + init(asyncId, type, triggerAsyncId, resource) { + if (type === 'WORKER') { + handle = resource; + this.disable(); + } + } +}).enable(); + +const w = new Worker('', { eval: true }); + +strictEqual(handle.hasRef(), true); +w.unref(); +strictEqual(handle.hasRef(), false); +w.ref(); +strictEqual(handle.hasRef(), true); + +w.on('exit', common.mustCall((exitCode) => { + strictEqual(exitCode, 0); + strictEqual(handle.hasRef(), true); + setTimeout(common.mustCall(() => { + strictEqual(handle.hasRef(), undefined); + }), 0); +}));