Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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.
  • Loading branch information
jasnell committed May 31, 2025
commit f95328d764eecdbdb6a66cb1cbce4525489b5b36
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2764,12 +2764,15 @@

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/00000

Check warning on line 2768 in doc/api/deprecations.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
Comment thread
jasnell marked this conversation as resolved.
Outdated
description: End-of-Life.
- version: v12.5.0
pr-url: https://github.com/nodejs/node/pull/28021
description: Runtime deprecation.
-->

Type: Runtime
Type: End-of-Life

Passing a callback to [`worker.terminate()`][] is deprecated. Use the returned
`Promise` instead, or a listener to the worker's `'exit'` event.
Expand Down
18 changes: 5 additions & 13 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
ObjectEntries,
Promise,
PromiseResolve,
PromiseWithResolvers,
ReflectApply,
RegExpPrototypeExec,
SafeArrayIterator,
Expand Down Expand Up @@ -381,30 +382,21 @@ class Worker extends EventEmitter {
ReflectApply(this[kPublicPort].postMessage, this[kPublicPort], args);
}

terminate(callback) {
terminate() {
debug(`[${threadId}] terminates Worker with ID ${this.threadId}`);

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();

this[kHandle].stopThread();

// 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
// once the thread has actually stopped.
return new Promise((resolve) => {
this.once('exit', resolve);
});
const { promise, resolve } = PromiseWithResolvers();
this.once('exit', resolve);
return promise;
}

async [SymbolAsyncDispose]() {
Expand Down
9 changes: 1 addition & 8 deletions test/parallel/test-worker-nexttick-terminate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ process.nextTick(() => {
`, { eval: true });

// Test deprecation of .terminate() with callback.
Comment thread
jasnell marked this conversation as resolved.
Outdated
Comment thread
jasnell marked this conversation as resolved.
Outdated
common.expectWarning(
'DeprecationWarning',
'Passing a callback to worker.terminate() is deprecated. ' +
'It returns a Promise instead.', 'DEP0132');

w.on('message', common.mustCall(() => {
setTimeout(() => {
w.terminate(common.mustCall()).then(common.mustCall());
}, 1);
setTimeout(() => w.terminate().then(common.mustCall()), 1);
}));
4 changes: 1 addition & 3 deletions test/parallel/test-worker-terminate-null-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ process.once('beforeExit', common.mustCall(() => worker.ref()));

worker.on('exit', common.mustCall(() => {
worker.terminate().then((res) => assert.strictEqual(res, undefined));
worker.terminate(() => null).then(
(res) => assert.strictEqual(res, undefined)
);

}));

worker.unref();
Loading