| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 464136f commit 43acce1
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -224,18 +224,19 @@ class Worker extends EventEmitter { | |||
| 224 | 224 | } | |
| 225 | 225 | ||
| 226 | 226 | terminate(callback) { | |
| 227 | - if (this[kHandle] === null) return; | ||
| 228 | - | ||
| 229 | 227 | debug(`[${threadId}] terminates Worker with ID ${this.threadId}`); | |
| 230 | 228 | ||
| 231 | 229 | if (typeof callback === 'function') { | |
| 232 | 230 | process.emitWarning( | |
| 233 | 231 | 'Passing a callback to worker.terminate() is deprecated. ' + | |
| 234 | 232 | 'It returns a Promise instead.', | |
| 235 | 233 | 'DeprecationWarning', 'DEP0132'); | |
| 234 | + if (this[kHandle] === null) return Promise.resolve(); | ||
| 236 | 235 | this.once('exit', (exitCode) => callback(null, exitCode)); | |
| 237 | 236 | } | |
| 238 | 237 | ||
| 238 | + if (this[kHandle] === null) return Promise.resolve(); | ||
| 239 | + | ||
| 239 | 240 | this[kHandle].stopThread(); | |
| 240 | 241 | ||
| 241 | 242 | // Do not use events.once() here, because the 'exit' event will always be | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const { Worker } = require('worker_threads'); | ||
| 5 | + | ||
| 6 | + // Test that calling worker.terminate() if kHandler is null should return an | ||
| 7 | + // empty promise that resolves to undefined, even when a callback is passed | ||
| 8 | + | ||
| 9 | + const worker = new Worker(` | ||
| 10 | + const { parentPort } = require('worker_threads'); | ||
| 11 | + parentPort.postMessage({ hello: 'world' }); | ||
| 12 | + `, { eval: true }); | ||
| 13 | + | ||
| 14 | + process.once('beforeExit', common.mustCall(() => { | ||
| 15 | + console.log('beforeExit'); | ||
| 16 | + worker.ref(); | ||
| 17 | + })); | ||
| 18 | + | ||
| 19 | + worker.on('exit', common.mustCall(() => { | ||
| 20 | + console.log('exit'); | ||
| 21 | + worker.terminate().then((res) => assert.strictEqual(res, undefined)); | ||
| 22 | + worker.terminate(() => null).then( | ||
| 23 | + (res) => assert.strictEqual(res, undefined) | ||
| 24 | + ); | ||
| 25 | + })); | ||
| 26 | + | ||
| 27 | + worker.unref(); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments