From abbe3135c4f051e80cb687ffb6a503e27237b3d2 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 16 Apr 2022 18:17:50 +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 --- 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 c9743fcf583a08..2f5604c5febbfc 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -665,6 +665,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()); @@ -827,6 +833,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); @@ -890,6 +897,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); +}));