From 9485e582890e88cb30e07ce4d3ecf9f800d372be Mon Sep 17 00:00:00 2001 From: Alexis Campailla Date: Tue, 8 Mar 2016 17:22:56 +0100 Subject: [PATCH 1/2] tty: don't read from console stream upon creation The tty.ReadStream constructor initializes this as a socket, which causes a read to be initiated. Even though during stdin initalization we call readStop shortly after, the read operation can consume keypress events from the system buffers. Fixes: https://github.com/nodejs/node/issues/5384 --- lib/tty.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/tty.js b/lib/tty.js index 72efcd078b2892..ee6c0407f6a135 100644 --- a/lib/tty.js +++ b/lib/tty.js @@ -17,17 +17,23 @@ function ReadStream(fd, options) { if (!(this instanceof ReadStream)) return new ReadStream(fd, options); + // pauseOnCreate to avoid reading from the sytem buffer options = util._extend({ highWaterMark: 0, readable: true, writable: false, - handle: new TTY(fd, true) + handle: new TTY(fd, true), + pauseOnCreate: true }, options); net.Socket.call(this, options); this.isRaw = false; this.isTTY = true; + + // Let the stream resume automatically when 'data' event handlers + // are added, even though it was paused on creation + this._readableState.flowing = null; } inherits(ReadStream, net.Socket); From f9768a0384acff612666fb12c728c0cac6983706 Mon Sep 17 00:00:00 2001 From: Alexis Campailla Date: Fri, 18 Mar 2016 02:05:20 +0100 Subject: [PATCH 2/2] stream: emit 'pause' on nextTick Readable.resume() schedules the resume operation onto the next tick, whereas pause() has immediate effect. This means that in a sequence stream.resume(); stream.pause(); .. the 'pause' event will be triggered before the resume operation is performed. For process.stdin, we are relying on the 'pause' event to stop reading on the underlying handle. This fix ensures that reads are started and stopped in the same order as resume() and pause() are called. --- lib/_stream_readable.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 338bf2a7539bfc..2ee59b4286496d 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -738,7 +738,8 @@ Readable.prototype.pause = function() { if (false !== this._readableState.flowing) { debug('pause'); this._readableState.flowing = false; - this.emit('pause'); + // Emit 'pause' on next tick as we do for 'resume' + process.nextTick(() => this.emit('pause')); } return this; };