diff --git a/lib/internal/process/stdio.js b/lib/internal/process/stdio.js index 9a16a5ab15f166..ce84142938f066 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; }