From 5d7045960628084f79f5da74272000b7d0b8256f Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 24 Mar 2018 23:11:10 +0100 Subject: [PATCH] lib: defer pausing stdin to the next tick This is done to match the stream implementation, which also only actually stops reading in the next tick after the `'pause'` event is emitted. PR-URL: https://github.com/nodejs/node/pull/19377 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- lib/internal/process/stdio.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/internal/process/stdio.js b/lib/internal/process/stdio.js index 9a16a5ab15f..ce84142938f 100644 --- a/lib/internal/process/stdio.js +++ b/lib/internal/process/stdio.js @@ -109,15 +109,22 @@ function setupStdio() { stdin._handle.readStop(); } - // if the user calls stdin.pause(), then we need to stop reading - // immediately, so that the process can close down. + // If the user calls stdin.pause(), then we need to stop reading + // once the stream implementation does so (one nextTick later), + // so that the process can close down. stdin.on('pause', () => { + process.nextTick(onpause); + }); + + function onpause() { if (!stdin._handle) return; - stdin._readableState.reading = false; - stdin._handle.reading = false; - stdin._handle.readStop(); - }); + if (stdin._handle.reading && !stdin._readableState.flowing) { + stdin._readableState.reading = false; + stdin._handle.reading = false; + stdin._handle.readStop(); + } + } return stdin; }