child_process: fire close event from stdio

PR-URL: https://github.com/nodejs/node/pull/22892
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
kohta ito 2018-09-17 16:16:44 +09:00 committed by Anna Henningsen
parent 82a256ac67
commit 1133e0bf8b
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 25 additions and 1 deletions

View File

@ -387,6 +387,9 @@ ChildProcess.prototype.spawn = function(options) {
// The stream is already cloned and piped, thus close it. // The stream is already cloned and piped, thus close it.
if (stream.type === 'wrap') { if (stream.type === 'wrap') {
stream.handle.close(); stream.handle.close();
if (stream._stdio && stream._stdio instanceof EventEmitter) {
stream._stdio.emit('close');
}
continue; continue;
} }
@ -946,7 +949,8 @@ function _validateStdio(stdio, sync) {
acc.push({ acc.push({
type: 'wrap', type: 'wrap',
wrapType: getHandleWrapType(handle), wrapType: getHandleWrapType(handle),
handle: handle handle: handle,
_stdio: stdio
}); });
} else if (isArrayBufferView(stdio) || typeof stdio === 'string') { } else if (isArrayBufferView(stdio) || typeof stdio === 'string') {
if (!sync) { if (!sync) {

View File

@ -0,0 +1,20 @@
'use strict';
const common = require('../common');
const { spawn } = require('child_process');
const net = require('net');
const server = net.createServer((conn) => {
conn.on('close', common.mustCall());
spawn(process.execPath, ['-v'], {
stdio: ['ignore', conn, 'ignore']
}).on('close', common.mustCall());
}).listen(common.PIPE, () => {
const client = net.connect(common.PIPE, common.mustCall());
client.on('data', () => {
client.end(() => {
server.close();
});
});
});