Skip to content

Commit c3922af

Browse files
RaisinTentargos
authored andcommitted
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: #42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <raisinten@gmail.com> PR-URL: #42756 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 7c97347 commit c3922af

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/node_worker.cc

+8
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,12 @@ void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
680680
}
681681
}
682682

683+
void Worker::HasRef(const FunctionCallbackInfo<Value>& args) {
684+
Worker* w;
685+
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
686+
args.GetReturnValue().Set(w->has_ref_);
687+
}
688+
683689
void Worker::Unref(const FunctionCallbackInfo<Value>& args) {
684690
Worker* w;
685691
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
@@ -842,6 +848,7 @@ void InitWorker(Local<Object> target,
842848

843849
env->SetProtoMethod(w, "startThread", Worker::StartThread);
844850
env->SetProtoMethod(w, "stopThread", Worker::StopThread);
851+
env->SetProtoMethod(w, "hasRef", Worker::HasRef);
845852
env->SetProtoMethod(w, "ref", Worker::Ref);
846853
env->SetProtoMethod(w, "unref", Worker::Unref);
847854
env->SetProtoMethod(w, "getResourceLimits", Worker::GetResourceLimits);
@@ -905,6 +912,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
905912
registry->Register(Worker::New);
906913
registry->Register(Worker::StartThread);
907914
registry->Register(Worker::StopThread);
915+
registry->Register(Worker::HasRef);
908916
registry->Register(Worker::Ref);
909917
registry->Register(Worker::Unref);
910918
registry->Register(Worker::GetResourceLimits);

src/node_worker.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Worker : public AsyncWrap {
6161
static void SetEnvVars(const v8::FunctionCallbackInfo<v8::Value>& args);
6262
static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
6363
static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
64+
static void HasRef(const v8::FunctionCallbackInfo<v8::Value>& args);
6465
static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
6566
static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
6667
static void GetResourceLimits(

test/parallel/test-worker-hasref.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
const { Worker } = require('worker_threads');
5+
const { createHook } = require('async_hooks');
6+
const { strictEqual } = require('assert');
7+
8+
let handle;
9+
10+
createHook({
11+
init(asyncId, type, triggerAsyncId, resource) {
12+
if (type === 'WORKER') {
13+
handle = resource;
14+
this.disable();
15+
}
16+
}
17+
}).enable();
18+
19+
const w = new Worker('', { eval: true });
20+
21+
strictEqual(handle.hasRef(), true);
22+
w.unref();
23+
strictEqual(handle.hasRef(), false);
24+
w.ref();
25+
strictEqual(handle.hasRef(), true);
26+
27+
w.on('exit', common.mustCall((exitCode) => {
28+
strictEqual(exitCode, 0);
29+
strictEqual(handle.hasRef(), true);
30+
setTimeout(common.mustCall(() => {
31+
strictEqual(handle.hasRef(), undefined);
32+
}), 0);
33+
}));

0 commit comments

Comments
 (0)