Skip to content

Commit 249bf50

Browse files
mcollinatargos
authored andcommitted
stream: fix regression introduced in #26059
In #26059, we introduced a bug that caused 'readable' to be nextTicked on EOF of a ReadableStream. This breaks the dicer module on CITGM. That change was partially reverted to still fix the bug in #25810 and not break dicer. See: #26059 Fixes: #25810 PR-URL: #26643 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0b2f900 commit 249bf50

7 files changed

+28
-33
lines changed

lib/_stream_readable.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,24 @@ function onEofChunk(stream, state) {
510510
}
511511
}
512512
state.ended = true;
513-
state.needReadable = false;
514513

515-
// We are not protecting if emittedReadable = true,
516-
// so 'readable' gets scheduled anyway.
517-
state.emittedReadable = true;
518-
process.nextTick(emitReadable_, stream);
514+
if (state.sync) {
515+
// If we are sync, wait until next tick to emit the data.
516+
// Otherwise we risk emitting data in the flow()
517+
// the readable code triggers during a read() call
518+
emitReadable(stream);
519+
} else {
520+
// Emit 'readable' now to make sure it gets picked up.
521+
state.needReadable = false;
522+
state.emittedReadable = true;
523+
// We have to emit readable now that we are EOF. Modules
524+
// in the ecosystem (e.g. dicer) rely on this event being sync.
525+
if (state.ended) {
526+
emitReadable_(stream);
527+
} else {
528+
process.nextTick(emitReadable_, stream);
529+
}
530+
}
519531
}
520532

521533
// Don't emit readable right away in sync mode, because this can trigger

test/parallel/test-stream-readable-emit-readable-short-stream.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const assert = require('assert');
5454
break;
5555
assert.strictEqual(chunk.toString(), 'content');
5656
}
57-
}, 2));
57+
}));
5858
}
5959

6060
{
@@ -78,7 +78,7 @@ const assert = require('assert');
7878
break;
7979
assert.strictEqual(chunk.toString(), 'content');
8080
}
81-
}, 2));
81+
}));
8282
}
8383

8484
{
@@ -94,7 +94,7 @@ const assert = require('assert');
9494
break;
9595
assert.strictEqual(chunk.toString(), 'content');
9696
}
97-
}, 2));
97+
}));
9898

9999
t.push('content');
100100
t.push(null);

test/parallel/test-stream-readable-emittedReadable.js

+1-12
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,12 @@ const noRead = new Readable({
4343
read: () => {}
4444
});
4545

46-
noRead.once('readable', common.mustCall(() => {
46+
noRead.on('readable', common.mustCall(() => {
4747
// emittedReadable should be true when the readable event is emitted
4848
assert.strictEqual(noRead._readableState.emittedReadable, true);
4949
noRead.read(0);
5050
// emittedReadable is not reset during read(0)
5151
assert.strictEqual(noRead._readableState.emittedReadable, true);
52-
53-
noRead.on('readable', common.mustCall(() => {
54-
// The second 'readable' is emitted because we are ending
55-
56-
// emittedReadable should be true when the readable event is emitted
57-
assert.strictEqual(noRead._readableState.emittedReadable, false);
58-
noRead.read(0);
59-
// emittedReadable is not reset during read(0)
60-
assert.strictEqual(noRead._readableState.emittedReadable, false);
61-
62-
}));
6352
}));
6453

6554
noRead.push('foo');

test/parallel/test-stream-readable-needReadable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ readable.on('readable', common.mustCall(() => {
1414
// When the readable event fires, needReadable is reset.
1515
assert.strictEqual(readable._readableState.needReadable, false);
1616
readable.read();
17-
}, 2));
17+
}));
1818

1919
// If a readable listener is attached, then a readable event is needed.
2020
assert.strictEqual(readable._readableState.needReadable, true);

test/parallel/test-stream-readable-reading-readingMore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const Readable = require('stream').Readable;
3131
assert.strictEqual(state.reading, false);
3232
}
3333

34-
const expectedReadingMore = [true, false, false];
34+
const expectedReadingMore = [true, true, false];
3535
readable.on('readable', common.mustCall(() => {
3636
// There is only one readingMore scheduled from on('data'),
3737
// after which everything is governed by the .read() call

test/parallel/test-stream2-httpclient-response-end.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const server = http.createServer(function(req, res) {
1515
while ((chunk = res.read()) !== null) {
1616
data += chunk;
1717
}
18-
}, 2));
18+
}));
1919
res.on('end', common.mustCall(function() {
2020
console.log('end event');
2121
assert.strictEqual(msg, data);

test/parallel/test-stream2-transform.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,10 @@ const Transform = require('_stream_transform');
321321

322322
pt.end();
323323

324-
// The next readable is emitted on the next tick.
325-
assert.strictEqual(emits, 0);
326-
327-
process.on('nextTick', function() {
328-
assert.strictEqual(emits, 1);
329-
assert.strictEqual(pt.read(5).toString(), 'l');
330-
assert.strictEqual(pt.read(5), null);
331-
332-
assert.strictEqual(emits, 1);
333-
});
324+
assert.strictEqual(emits, 1);
325+
assert.strictEqual(pt.read(5).toString(), 'l');
326+
assert.strictEqual(pt.read(5), null);
327+
assert.strictEqual(emits, 1);
334328
}
335329

336330
{

0 commit comments

Comments
 (0)