child_process: Pull through untouched stdio streams

Otherwise child procs will never emit a 'close' event if you don't
ever consume their streams, because they will never hit the EOF.
This commit is contained in:
isaacs 2013-01-07 06:29:00 -08:00
Родитель 3e6f737eaf
Коммит df3563aa65
2 изменённых файлов: 30 добавлений и 0 удалений

Просмотреть файл

@ -696,12 +696,33 @@ function ChildProcess() {
self.emit('exit', self.exitCode, self.signalCode);
}
// if any of the stdio streams have not been touched,
// then pull all the data through so that it can get the
// eof and emit a 'close' event.
// Do it on nextTick so that the user has one last chance
// to consume the output, if for example they only want to
// start reading the data once the process exits.
process.nextTick(function() {
flushStdio(self);
});
maybeClose(self);
};
}
util.inherits(ChildProcess, EventEmitter);
function flushStdio(subprocess) {
subprocess.stdio.forEach(function(stream, fd, stdio) {
if (!stream || !stream.readable || stream._consuming ||
stream._readableState.flowing)
return;
stream.resume();
});
}
function getHandleWrapType(stream) {
if (stream instanceof handleWraps.Pipe) return 'pipe';
if (stream instanceof handleWraps.TTY) return 'tty';

Просмотреть файл

@ -244,6 +244,15 @@ function onSocketEnd() {
exports.Socket = Socket;
exports.Stream = Socket; // Legacy naming.
Socket.prototype.read = function(n) {
if (n === 0)
return stream.Readable.prototype.read.call(this, n);
this.read = stream.Readable.prototype.read;
this._consuming = true;
return this.read(n);
};
Socket.prototype.listen = function() {
debug('socket.listen');