Skip to content

Commit

Permalink
child_process: fire close event from stdio
Browse files Browse the repository at this point in the history
PR-URL: #22892
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
koh110 authored and BridgeAR committed Mar 14, 2019
1 parent 2b2471b commit 8c864de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ ChildProcess.prototype.spawn = function(options) {
// The stream is already cloned and piped, thus close it.
if (stream.type === 'wrap') {
stream.handle.close();
if (stream._stdio && stream._stdio instanceof EventEmitter) {
stream._stdio.emit('close');
}
continue;
}

Expand Down Expand Up @@ -946,7 +949,8 @@ function _validateStdio(stdio, sync) {
acc.push({
type: 'wrap',
wrapType: getHandleWrapType(handle),
handle: handle
handle: handle,
_stdio: stdio
});
} else if (isArrayBufferView(stdio) || typeof stdio === 'string') {
if (!sync) {
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-child-process-server-close.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit 8c864de

Please sign in to comment.