Skip to content

Commit 3a000eb

Browse files
committed
worker: add hasRef()
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: nodejs#42091 Refs: mafintosh/why-is-node-running#59 Signed-off-by: Darshan Sen <raisinten@gmail.com>
1 parent 24adba6 commit 3a000eb

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

doc/api/worker_threads.md

+12
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,18 @@ Send a message to the worker that is received via
11441144
[`require('worker_threads').parentPort.on('message')`][].
11451145
See [`port.postMessage()`][] for more details.
11461146

1147+
### `worker.hasRef()`
1148+
1149+
<!-- YAML
1150+
added: REPLACEME
1151+
-->
1152+
1153+
> Stability: 1 - Experimental
1154+
1155+
* Returns: {boolean}
1156+
1157+
If `true`, the `Worker` object will keep the Node.js event loop active.
1158+
11471159
### `worker.ref()`
11481160

11491161
<!-- YAML

lib/internal/worker.js

+6
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ class Worker extends EventEmitter {
367367
});
368368
}
369369

370+
hasRef() {
371+
if (this[kHandle] === null) return false;
372+
373+
return this[kHandle].hasRef();
374+
}
375+
370376
ref() {
371377
if (this[kHandle] === null) return;
372378

src/node_worker.cc

+8
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,12 @@ void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
665665
}
666666
}
667667

668+
void Worker::HasRef(const FunctionCallbackInfo<Value>& args) {
669+
Worker* w;
670+
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
671+
args.GetReturnValue().Set(w->has_ref_);
672+
}
673+
668674
void Worker::Unref(const FunctionCallbackInfo<Value>& args) {
669675
Worker* w;
670676
ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
@@ -827,6 +833,7 @@ void InitWorker(Local<Object> target,
827833

828834
env->SetProtoMethod(w, "startThread", Worker::StartThread);
829835
env->SetProtoMethod(w, "stopThread", Worker::StopThread);
836+
env->SetProtoMethod(w, "hasRef", Worker::HasRef);
830837
env->SetProtoMethod(w, "ref", Worker::Ref);
831838
env->SetProtoMethod(w, "unref", Worker::Unref);
832839
env->SetProtoMethod(w, "getResourceLimits", Worker::GetResourceLimits);
@@ -890,6 +897,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
890897
registry->Register(Worker::New);
891898
registry->Register(Worker::StartThread);
892899
registry->Register(Worker::StopThread);
900+
registry->Register(Worker::HasRef);
893901
registry->Register(Worker::Ref);
894902
registry->Register(Worker::Unref);
895903
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

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
const { Worker } = require('worker_threads');
5+
const { strictEqual } = require('assert');
6+
7+
const w = new Worker('', { eval: true });
8+
9+
strictEqual(w.hasRef(), true);
10+
w.unref();
11+
strictEqual(w.hasRef(), false);
12+
w.ref();
13+
strictEqual(w.hasRef(), true);
14+
15+
w.on('exit', common.mustCall(() => strictEqual(w.hasRef(), false)));

0 commit comments

Comments
 (0)