Skip to content
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

process: add process.ref() and process.unref() methods #56400

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 34 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -3232,6 +3232,23 @@ const { ppid } = require('node:process');
console.log(`The parent process is pid ${ppid}`);
```

## `process.ref(maybeRefable)`

<!-- YAML
added: REPLACEME
-->

* `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('node:ref')`
and `Symbol.for('node:unref')` methods. "Ref'd" objects will keep the Node.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell All other Node.js symbols use nodejs. as the prefix rather than node:. Can we fix that up here as well before it gets released?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah damn, it already is released. Welp.

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 @@ -4272,6 +4289,23 @@ console.log(

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

## `process.unref(maybeRefable)`

<!-- YAML
added: REPLACEME
-->

* `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('node:ref')`
and `Symbol.for('node: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 @@ -178,6 +178,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
14 changes: 14 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 @@ -418,6 +420,16 @@ function toggleTraceCategoryState(asyncHooksEnabled) {

const { arch, platform, version } = process;

function ref(maybeRefable) {
const fn = maybeRefable?.[SymbolFor('node:ref')] || maybeRefable?.ref;
if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable);
}

function unref(maybeRefable) {
const fn = maybeRefable?.[SymbolFor('node:unref')] || maybeRefable?.unref;
if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable);
}

module.exports = {
toggleTraceCategoryState,
buildAllowedFlags,
Expand All @@ -427,4 +439,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('node:ref')]() {
this.refCalled++;
}
[Symbol.for('node: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