[v22.x backport] process: add `process.ref()` and `process.unref()` methods by aduh95 · Pull Request #56571 · nodejs/node · GitHub
Skip to content
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
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