From f95328d764eecdbdb6a66cb1cbce4525489b5b36 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 31 May 2025 09:25:57 -0700 Subject: [PATCH 1/3] 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. --- doc/api/deprecations.md | 5 ++++- lib/internal/worker.js | 18 +++++------------- .../parallel/test-worker-nexttick-terminate.js | 9 +-------- .../test-worker-terminate-null-handler.js | 4 +--- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index c0f762c54860a6..5b8b1810f5af01 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2764,12 +2764,15 @@ legacy parser. -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. diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 1df638ff77864c..d89d68f2227d0a 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -11,6 +11,7 @@ const { ObjectEntries, Promise, PromiseResolve, + PromiseWithResolvers, ReflectApply, RegExpPrototypeExec, SafeArrayIterator, @@ -381,20 +382,11 @@ 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(); @@ -402,9 +394,9 @@ class Worker extends EventEmitter { // 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]() { diff --git a/test/parallel/test-worker-nexttick-terminate.js b/test/parallel/test-worker-nexttick-terminate.js index 0e5d7e096c57ec..08f4921f8f81c6 100644 --- a/test/parallel/test-worker-nexttick-terminate.js +++ b/test/parallel/test-worker-nexttick-terminate.js @@ -13,13 +13,6 @@ process.nextTick(() => { `, { 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(() => { - setTimeout(() => { - w.terminate(common.mustCall()).then(common.mustCall()); - }, 1); + setTimeout(() => w.terminate().then(common.mustCall()), 1); })); diff --git a/test/parallel/test-worker-terminate-null-handler.js b/test/parallel/test-worker-terminate-null-handler.js index 9db2e38b5c4920..e546e662655e06 100644 --- a/test/parallel/test-worker-terminate-null-handler.js +++ b/test/parallel/test-worker-terminate-null-handler.js @@ -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(); From c1bb5891985f891976bafeac90c178cc6cb53416 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 31 May 2025 09:28:24 -0700 Subject: [PATCH 2/3] Update doc/api/deprecations.md --- doc/api/deprecations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 5b8b1810f5af01..77b6a71b8b3ef9 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2765,7 +2765,7 @@ legacy parser.