From df3563aa65da6a45f7e9e9e02444b0ae2c862264 Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 7 Jan 2013 06:29:00 -0800 Subject: [PATCH] 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. --- lib/child_process.js | 21 +++++++++++++++++++++ lib/net.js | 9 +++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/child_process.js b/lib/child_process.js index 1fecbc7557..f69c47d390 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -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'; diff --git a/lib/net.js b/lib/net.js index a347c08507..1c581fc3d0 100644 --- a/lib/net.js +++ b/lib/net.js @@ -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');