Skip to content

Commit 034c37a

Browse files
committed
fixes
1 parent 2780ac9 commit 034c37a

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

lib/child_process.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const { Buffer } = require('buffer');
6262
const { Pipe, constants: PipeConstants } = internalBinding('pipe_wrap');
6363

6464
const {
65+
AbortError,
6566
codes: errorCodes,
6667
genericNodeError,
6768
} = require('internal/errors');
@@ -85,7 +86,6 @@ const {
8586
} = require('internal/validators');
8687
const child_process = require('internal/child_process');
8788
const {
88-
abortChildProcess,
8989
getValidStdio,
9090
setupChannel,
9191
ChildProcess,
@@ -713,6 +713,18 @@ function normalizeSpawnArguments(file, args, options) {
713713
};
714714
}
715715

716+
function abortChildProcess(child, killSignal, reason) {
717+
if (!child)
718+
return;
719+
try {
720+
if (child.kill(killSignal)) {
721+
child.emit('error', new AbortError(undefined, { cause: reason }));
722+
}
723+
} catch (err) {
724+
child.emit('error', err);
725+
}
726+
}
727+
716728
/**
717729
* Spawns a new process using the given `file`.
718730
* @param {string} file

lib/internal/child_process.js

+12-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const {
99
FunctionPrototypeCall,
1010
ObjectDefineProperty,
1111
ObjectSetPrototypeOf,
12-
Promise,
1312
ReflectApply,
1413
StringPrototypeSlice,
1514
Symbol,
@@ -41,7 +40,6 @@ const {
4140
const EventEmitter = require('events');
4241
const net = require('net');
4342
const dgram = require('dgram');
44-
const { kEmptyObject } = require('internal/util');
4543
const inspect = require('internal/util/inspect').inspect;
4644
const assert = require('internal/assert');
4745

@@ -523,10 +521,18 @@ ChildProcess.prototype.kill = function(sig) {
523521
};
524522

525523
ChildProcess.prototype[SymbolAsyncDispose] = async function() {
526-
if (!this.closed) {
527-
const promise = EventEmitter.once(this, 'close');
528-
abortChildProcess(this, this.killSignal);
529-
await promise;
524+
if (this.closed) {
525+
return;
526+
}
527+
const promise = EventEmitter.once(this, 'close');
528+
try {
529+
if (this.kill(this.killSignal)) {
530+
await promise;
531+
this.emit('error', new AbortError());
532+
}
533+
} catch (err) {
534+
this.emit('error', err);
535+
throw err;
530536
}
531537
};
532538

@@ -1138,22 +1144,8 @@ function spawnSync(options) {
11381144
}
11391145

11401146

1141-
function abortChildProcess(child, killSignal, reason) {
1142-
if (!child)
1143-
return;
1144-
try {
1145-
if (child.kill(killSignal)) {
1146-
child.emit('error', new AbortError(undefined, arguments.length < 3 ? kEmptyObject : { cause: reason }));
1147-
}
1148-
} catch (err) {
1149-
child.emit('error', err);
1150-
}
1151-
}
1152-
1153-
11541147
module.exports = {
11551148
ChildProcess,
1156-
abortChildProcess,
11571149
kChannelHandle,
11581150
setupChannel,
11591151
getValidStdio,

test/parallel/test-child-process-destroy.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ cat.on('exit', common.mustCall((code, signal) => {
1414
assert.strictEqual(cat.signalCode, 'SIGTERM');
1515
}));
1616
cat.on('error', common.mustCall((err) => {
17-
assert.strictEqual(cat.signalCode, null);
17+
assert.strictEqual(cat.signalCode, 'SIGTERM');
1818
assert.strictEqual(err.name, 'AbortError');
1919
}));
2020

2121
assert.strictEqual(cat.signalCode, null);
2222
assert.strictEqual(cat.killed, false);
2323
cat[Symbol.asyncDispose]().then(common.mustCall(() => {
2424
assert.strictEqual(cat.signalCode, 'SIGTERM');
25-
})).catch(e=>{
26-
console.log(111, e);
27-
});
25+
}));
2826
assert.strictEqual(cat.killed, true);

0 commit comments

Comments
 (0)