Skip to content

[v22.x backport] process: add process.ref() and process.unref() methods #56571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -3221,6 +3221,25 @@ const { ppid } = require('node:process');
console.log(`The parent process is pid ${ppid}`);
```

## `process.ref(maybeRefable)`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

* `maybeRefable` {any} An object that may be "refable".

An object is "refable" if it implements the Node.js "Refable protocol".
Specifically, this means that the object implements the `Symbol.for('nodejs.ref')`
and `Symbol.for('nodejs.unref')` methods. "Ref'd" objects will keep the Node.js
event loop alive, while "unref'd" objects will not. Historically, this was
implemented by using `ref()` and `unref()` methods directly on the objects.
This pattern, however, is being deprecated in favor of the "Refable protocol"
in order to better support Web Platform API types whose APIs cannot be modified
to add `ref()` and `unref()` methods but still need to support that behavior.

## `process.release`

<!-- YAML
Expand Down Expand Up @@ -4261,6 +4280,25 @@ console.log(

In [`Worker`][] threads, `process.umask(mask)` will throw an exception.

## `process.unref(maybeRefable)`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

* `maybeUnfefable` {any} An object that may be "unref'd".

An object is "unrefable" if it implements the Node.js "Refable protocol".
Specifically, this means that the object implements the `Symbol.for('nodejs.ref')`
and `Symbol.for('nodejs.unref')` methods. "Ref'd" objects will keep the Node.js
event loop alive, while "unref'd" objects will not. Historically, this was
implemented by using `ref()` and `unref()` methods directly on the objects.
This pattern, however, is being deprecated in favor of the "Refable protocol"
in order to better support Web Platform API types whose APIs cannot be modified
to add `ref()` and `unref()` methods but still need to support that behavior.

## `process.uptime()`

<!-- YAML
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ const rawMethods = internalBinding('process_methods');
process.availableMemory = rawMethods.availableMemory;
process.kill = wrapped.kill;
process.exit = wrapped.exit;
process.ref = perThreadSetup.ref;
process.unref = perThreadSetup.unref;

let finalizationMod;
ObjectDefineProperty(process, 'finalization', {
Expand Down
18 changes: 18 additions & 0 deletions lib/internal/process/per_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
ArrayPrototypeSplice,
BigUint64Array,
Float64Array,
FunctionPrototypeCall,
NumberMAX_SAFE_INTEGER,
ObjectDefineProperty,
ObjectFreeze,
Expand All @@ -26,6 +27,7 @@ const {
StringPrototypeReplace,
StringPrototypeSlice,
Symbol,
SymbolFor,
SymbolIterator,
} = primordials;

Expand Down Expand Up @@ -422,6 +424,20 @@ function toggleTraceCategoryState(asyncHooksEnabled) {

const { arch, platform, version } = process;

let refSymbol;
function ref(maybeRefable) {
if (maybeRefable == null) return;
const fn = maybeRefable[refSymbol ??= SymbolFor('nodejs.ref')] || maybeRefable.ref;
if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable);
}

let unrefSymbol;
function unref(maybeRefable) {
if (maybeRefable == null) return;
const fn = maybeRefable[unrefSymbol ??= SymbolFor('nodejs.unref')] || maybeRefable.unref;
if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable);
}

module.exports = {
toggleTraceCategoryState,
assert,
Expand All @@ -432,4 +448,6 @@ module.exports = {
arch,
platform,
version,
ref,
unref,
};
60 changes: 60 additions & 0 deletions test/parallel/test-process-ref-unref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

require('../common');

const {
describe,
it,
} = require('node:test');

const {
strictEqual,
} = require('node:assert');

class Foo {
refCalled = 0;
unrefCalled = 0;
ref() {
this.refCalled++;
}
unref() {
this.unrefCalled++;
}
}

class Foo2 {
refCalled = 0;
unrefCalled = 0;
[Symbol.for('nodejs.ref')]() {
this.refCalled++;
}
[Symbol.for('nodejs.unref')]() {
this.unrefCalled++;
}
}

describe('process.ref/unref work as expected', () => {
it('refs...', () => {
// Objects that implement the new Symbol-based API
// just work.
const foo1 = new Foo();
const foo2 = new Foo2();
process.ref(foo1);
process.unref(foo1);
process.ref(foo2);
process.unref(foo2);
strictEqual(foo1.refCalled, 1);
strictEqual(foo1.unrefCalled, 1);
strictEqual(foo2.refCalled, 1);
strictEqual(foo2.unrefCalled, 1);

// Objects that implement the legacy API also just work.
const i = setInterval(() => {}, 1000);
strictEqual(i.hasRef(), true);
process.unref(i);
strictEqual(i.hasRef(), false);
process.ref(i);
strictEqual(i.hasRef(), true);
clearInterval(i);
});
});
Loading