diff --git a/lib/child_process.js b/lib/child_process.js index 336ec19a0cf881..07cc49ec0f9271 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -62,6 +62,7 @@ const { Buffer } = require('buffer'); const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap'); const { + AbortError, codes: errorCodes, genericNodeError, } = require('internal/errors'); @@ -85,7 +86,6 @@ const { } = require('internal/validators'); const child_process = require('internal/child_process'); const { - abortChildProcess, getValidStdio, setupChannel, ChildProcess, @@ -713,6 +713,18 @@ function normalizeSpawnArguments(file, args, options) { }; } +function abortChildProcess(child, killSignal, reason) { + if (!child) + return; + try { + if (child.kill(killSignal)) { + child.emit('error', new AbortError(undefined, { cause: reason })); + } + } catch (err) { + child.emit('error', err); + } +} + /** * Spawns a new process using the given `file`. * @param {string} file diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 210669f24a807a..5ef2504abbf5a0 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -9,7 +9,6 @@ const { FunctionPrototypeCall, ObjectDefineProperty, ObjectSetPrototypeOf, - Promise, ReflectApply, StringPrototypeSlice, Symbol, @@ -41,7 +40,6 @@ const { const EventEmitter = require('events'); const net = require('net'); const dgram = require('dgram'); -const { kEmptyObject } = require('internal/util'); const inspect = require('internal/util/inspect').inspect; const assert = require('internal/assert'); @@ -523,10 +521,18 @@ ChildProcess.prototype.kill = function(sig) { }; ChildProcess.prototype[SymbolAsyncDispose] = async function() { - if (!this.closed) { - const promise = EventEmitter.once(this, 'close'); - abortChildProcess(this, this.killSignal); - await promise; + if (this.closed) { + return; + } + const promise = EventEmitter.once(this, 'close'); + try { + if (this.kill(this.killSignal)) { + await promise; + this.emit('error', new AbortError()); + } + } catch (err) { + this.emit('error', err); + throw err; } }; @@ -1138,22 +1144,8 @@ function spawnSync(options) { } -function abortChildProcess(child, killSignal, reason) { - if (!child) - return; - try { - if (child.kill(killSignal)) { - child.emit('error', new AbortError(undefined, arguments.length < 3 ? kEmptyObject : { cause: reason })); - } - } catch (err) { - child.emit('error', err); - } -} - - module.exports = { ChildProcess, - abortChildProcess, kChannelHandle, setupChannel, getValidStdio, diff --git a/test/parallel/test-child-process-destroy.js b/test/parallel/test-child-process-destroy.js index 6ac4dfcf9ad0ed..f0ad421e4abedc 100644 --- a/test/parallel/test-child-process-destroy.js +++ b/test/parallel/test-child-process-destroy.js @@ -14,7 +14,7 @@ cat.on('exit', common.mustCall((code, signal) => { assert.strictEqual(cat.signalCode, 'SIGTERM'); })); cat.on('error', common.mustCall((err) => { - assert.strictEqual(cat.signalCode, null); + assert.strictEqual(cat.signalCode, 'SIGTERM'); assert.strictEqual(err.name, 'AbortError'); })); @@ -22,7 +22,5 @@ assert.strictEqual(cat.signalCode, null); assert.strictEqual(cat.killed, false); cat[Symbol.asyncDispose]().then(common.mustCall(() => { assert.strictEqual(cat.signalCode, 'SIGTERM'); -})).catch(e=>{ - console.log(111, e); -}); +})); assert.strictEqual(cat.killed, true);