diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 3cd5472b9341bd..df0646ff4f68b8 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -226,6 +226,8 @@ class Worker extends EventEmitter { terminate(callback) { debug(`[${threadId}] terminates Worker with ID ${this.threadId}`); + this.ref(); + if (typeof callback === 'function') { process.emitWarning( 'Passing a callback to worker.terminate() is deprecated. ' + diff --git a/test/parallel/test-worker-terminate-unrefed.js b/test/parallel/test-worker-terminate-unrefed.js new file mode 100644 index 00000000000000..adf6bbf14563cd --- /dev/null +++ b/test/parallel/test-worker-terminate-unrefed.js @@ -0,0 +1,16 @@ +'use strict'; +const common = require('../common'); +const { once } = require('events'); +const { Worker } = require('worker_threads'); + +// Test that calling worker.terminate() on an unref()’ed Worker instance +// still resolves the returned Promise. + +async function test() { + const worker = new Worker('setTimeout(() => {}, 1000000);', { eval: true }); + await once(worker, 'online'); + worker.unref(); + await worker.terminate(); +} + +test().then(common.mustCall());