worker: move terminate callback to end-of-life

Passing a callback to worker.terminate() has been deprecated
for about six years now. It's time to remove it.

PR-URL: https://github.com/nodejs/node/pull/58528
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
James M Snell 2025-05-31 09:25:57 -07:00
parent d33f4b539a
commit 411cc42d22
4 changed files with 11 additions and 26 deletions

View File

@ -2770,12 +2770,15 @@ legacy parser.
<!-- YAML <!-- YAML
changes: changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/58528
description: End-of-Life.
- version: v12.5.0 - version: v12.5.0
pr-url: https://github.com/nodejs/node/pull/28021 pr-url: https://github.com/nodejs/node/pull/28021
description: Runtime deprecation. description: Runtime deprecation.
--> -->
Type: Runtime Type: End-of-Life
Passing a callback to [`worker.terminate()`][] is deprecated. Use the returned Passing a callback to [`worker.terminate()`][] is deprecated. Use the returned
`Promise` instead, or a listener to the worker's `'exit'` event. `Promise` instead, or a listener to the worker's `'exit'` event.

View File

@ -11,6 +11,7 @@ const {
ObjectEntries, ObjectEntries,
Promise, Promise,
PromiseResolve, PromiseResolve,
PromiseWithResolvers,
ReflectApply, ReflectApply,
RegExpPrototypeExec, RegExpPrototypeExec,
SafeArrayIterator, SafeArrayIterator,
@ -381,20 +382,11 @@ class Worker extends EventEmitter {
ReflectApply(this[kPublicPort].postMessage, this[kPublicPort], args); ReflectApply(this[kPublicPort].postMessage, this[kPublicPort], args);
} }
terminate(callback) { terminate() {
debug(`[${threadId}] terminates Worker with ID ${this.threadId}`); debug(`[${threadId}] terminates Worker with ID ${this.threadId}`);
this.ref(); this.ref();
if (typeof callback === 'function') {
process.emitWarning(
'Passing a callback to worker.terminate() is deprecated. ' +
'It returns a Promise instead.',
'DeprecationWarning', 'DEP0132');
if (this[kHandle] === null) return PromiseResolve();
this.once('exit', (exitCode) => callback(null, exitCode));
}
if (this[kHandle] === null) return PromiseResolve(); if (this[kHandle] === null) return PromiseResolve();
this[kHandle].stopThread(); this[kHandle].stopThread();
@ -402,9 +394,9 @@ class Worker extends EventEmitter {
// Do not use events.once() here, because the 'exit' event will always be // Do not use events.once() here, because the 'exit' event will always be
// emitted regardless of any errors, and the point is to only resolve // emitted regardless of any errors, and the point is to only resolve
// once the thread has actually stopped. // once the thread has actually stopped.
return new Promise((resolve) => { const { promise, resolve } = PromiseWithResolvers();
this.once('exit', resolve); this.once('exit', resolve);
}); return promise;
} }
async [SymbolAsyncDispose]() { async [SymbolAsyncDispose]() {

View File

@ -12,14 +12,6 @@ process.nextTick(() => {
}); });
`, { eval: true }); `, { eval: true });
// Test deprecation of .terminate() with callback.
common.expectWarning(
'DeprecationWarning',
'Passing a callback to worker.terminate() is deprecated. ' +
'It returns a Promise instead.', 'DEP0132');
w.on('message', common.mustCall(() => { w.on('message', common.mustCall(() => {
setTimeout(() => { setTimeout(() => w.terminate().then(common.mustCall()), 1);
w.terminate(common.mustCall()).then(common.mustCall());
}, 1);
})); }));

View File

@ -15,9 +15,7 @@ process.once('beforeExit', common.mustCall(() => worker.ref()));
worker.on('exit', common.mustCall(() => { worker.on('exit', common.mustCall(() => {
worker.terminate().then((res) => assert.strictEqual(res, undefined)); worker.terminate().then((res) => assert.strictEqual(res, undefined));
worker.terminate(() => null).then(
(res) => assert.strictEqual(res, undefined)
);
})); }));
worker.unref(); worker.unref();