Skip to content

Commit 1c61205

Browse files
lundibunditargos
authored andcommitted
stream: fix readable behavior for highWaterMark === 0
Avoid trying to emit 'readable' due to the fact that state.length is always >= state.highWaterMark if highWaterMark is 0. Therefore upon .read(0) call (through .on('readable')) stream assumed that it has enough data to emit 'readable' even though state.length === 0 instead of issuing _read(). Which led to the TTY not recognizing that someone is waiting for the input. Fixes: #20503 Refs: #18372 PR-URL: #21690 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 7135822 commit 1c61205

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

‎lib/_stream_readable.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ Readable.prototype.read = function(n){
383383
// the 'readable' event and move on.
384384
if(n===0&&
385385
state.needReadable&&
386-
(state.length>=state.highWaterMark||state.ended)){
386+
((state.highWaterMark!==0 ?
387+
state.length>=state.highWaterMark :
388+
state.length>0)||
389+
state.ended)){
387390
debug('read: emitReadable',state.length,state.ended);
388391
if(state.length===0&&state.ended)
389392
endReadable(this);
@@ -808,6 +811,7 @@ Readable.prototype.on = function(ev, fn){
808811
if(!state.endEmitted&&!state.readableListening){
809812
state.readableListening=state.needReadable=true;
810813
state.emittedReadable=false;
814+
debug('on readable',state.length,state.reading);
811815
if(state.length){
812816
emitReadable(this);
813817
}elseif(!state.reading){
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
const{ Readable }=require('stream');
5+
6+
// This test ensures that there will not be an additional empty 'readable'
7+
// event when stream has ended (only 1 event signalling about end)
8+
9+
constr=newReadable({
10+
read: ()=>{},
11+
});
12+
13+
r.push(null);
14+
15+
r.on('readable',common.mustCall());
16+
r.on('end',common.mustCall());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
5+
// This test ensures that Readable stream will call _read() for streams
6+
// with highWaterMark === 0 upon .read(0) instead of just trying to
7+
// emit 'readable' event.
8+
9+
constassert=require('assert');
10+
const{ Readable }=require('stream');
11+
12+
constr=newReadable({
13+
// must be called only once upon setting 'readable' listener
14+
read: common.mustCall(),
15+
highWaterMark: 0,
16+
});
17+
18+
letpushedNull=false;
19+
// this will trigger read(0) but must only be called after push(null)
20+
// because the we haven't pushed any data
21+
r.on('readable',common.mustCall(()=>{
22+
assert.strictEqual(r.read(),null);
23+
assert.strictEqual(pushedNull,true);
24+
}));
25+
r.on('end',common.mustCall());
26+
process.nextTick(()=>{
27+
assert.strictEqual(r.read(),null);
28+
pushedNull=true;
29+
r.push(null);
30+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constassert=require('assert');
5+
6+
// This test ensures that Node.js will not ignore tty 'readable' subscribers
7+
// when it's the only tty subscriber and the only thing keeping event loop alive
8+
// https://github.com/nodejs/node/issues/20503
9+
10+
process.stdin.setEncoding('utf8');
11+
12+
constexpectedInput=['foo','bar',null];
13+
14+
process.stdin.on('readable',common.mustCall(function(){
15+
constdata=process.stdin.read();
16+
assert.strictEqual(data,expectedInput.shift());
17+
},3));// first 2 data, then end
18+
19+
process.stdin.on('end',common.mustCall());
20+
21+
setTimeout(()=>{
22+
process.stdin.push('foo');
23+
process.nextTick(()=>{
24+
process.stdin.push('bar');
25+
process.nextTick(()=>{
26+
process.stdin.push(null);
27+
});
28+
});
29+
},1);

‎test/pseudo-tty/test-readable-tty-keepalive.out‎

Whitespace-only changes.

0 commit comments

Comments
(0)