From c3922afa1c9acad431812a1bd45725ae3410522b 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 04cd60b2453543..2a509165c250c0 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -680,6 +680,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()); @@ -842,6 +848,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); @@ -905,6 +912,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 d400c4c991dcbc..f2a9386aeabff3 100644 --- a/src/node_worker.h +++ b/src/node_worker.h @@ -61,6 +61,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); +}));