stream: fix readable behavior for highWaterMark === 0 · nodejs/node@1c61205 · GitHub
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 <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 7135822 commit 1c61205

5 files changed

Lines changed: 80 additions & 1 deletion

File tree

lib/_stream_readable.js

Lines changed: 5 additions & 1 deletion
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
const common = 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+
const r = new Readable({
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+
const common = 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+
const assert = require('assert');
10+
const { Readable } = require('stream');
11+
12+
const r = new Readable({
13+
// must be called only once upon setting 'readable' listener
14+
read: common.mustCall(),
15+
highWaterMark: 0,
16+
});
17+
18+
let pushedNull = 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

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

Whitespace-only changes.

0 commit comments

Comments
 (0)