diff options
-rw-r--r-- | lib/internal/worker.js | 2 | ||||
-rw-r--r-- | test/parallel/test-worker-terminate-unrefed.js | 16 |
2 files changed, 18 insertions, 0 deletions
diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 3cd5472b93..df0646ff4f 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 0000000000..adf6bbf145 --- /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()); |