Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Jul 2, 2023
1 parent 2780ac9 commit 034c37a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
14 changes: 13 additions & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const { Buffer } = require('buffer');
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');

const {
AbortError,
codes: errorCodes,
genericNodeError,
} = require('internal/errors');
Expand All @@ -85,7 +86,6 @@ const {
} = require('internal/validators');
const child_process = require('internal/child_process');
const {
abortChildProcess,
getValidStdio,
setupChannel,
ChildProcess,
Expand Down Expand Up @@ -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
Expand Down
32 changes: 12 additions & 20 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const {
FunctionPrototypeCall,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Promise,
ReflectApply,
StringPrototypeSlice,
Symbol,
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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;
}
};

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions test/parallel/test-child-process-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ 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');
}));

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);

0 comments on commit 034c37a

Please sign in to comment.