GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: skip unobserved 'readable' emission at EOF · nodejs/node@9192fbb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/streams
    • readable.js
  • test/parallel
    • test-stream-readable-unobserved-eof-readable.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/streams/readable.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/readable.js
+23-4Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ const kHasPaused = 1 << 25;
138138
const kPaused = 1 << 26;
139139
const kDataListening = 1 << 27;
140140
const kEndScheduled = 1 << 28;
141+
const kEofReadablePending = 1 << 29;
141142

142143
// TODO(benjamingr) it is likely slower to do it this way than with free functions
143144
function makeBitMapDescriptor(bit) {
@@ -682,7 +683,7 @@ Readable.prototype.read = function(n) {
682683
state.highWaterMark = computeNewHighWaterMark(n);
683684

684685
if (n !== 0)
685-
state[kState] &= ~kEmittedReadable;
686+
state[kState] &= ~(kEmittedReadable | kEofReadablePending);
686687

687688
// If we're doing read(0) to trigger a readable event, but we
688689
// already have a bunch of data in the buffer, then just trigger
@@ -825,7 +826,16 @@ function onEofChunk(stream, state) {
825826
// If we are sync, wait until next tick to emit the data.
826827
// Otherwise we risk emitting data in the flow()
827828
// the readable code triggers during a read() call.
828-
emitReadable(stream);
829+
if ((state[kState] & (kFlowing | kNeedReadable)) !== 0 ||
830+
stream.listenerCount('readable') > 0) {
831+
emitReadable(stream);
832+
} else {
833+
// Nobody is observing the stream: do not schedule the 'readable'
834+
// emission at all. A 'readable' listener attached later redeems it
835+
// (see Readable.prototype.on), and read() and resume() reach the
836+
// end of the stream on their own.
837+
state[kState] |= kEofReadablePending;
838+
}
829839
} else {
830840
// Emit 'readable' now to make sure it gets picked up.
831841
state[kState] &= ~kNeedReadable;
@@ -1178,8 +1188,17 @@ Readable.prototype.on = function(ev, fn) {
11781188
debug('on readable');
11791189
if (state.length) {
11801190
emitReadable(this);
1181-
} else if ((state[kState] & kReading) === 0) {
1182-
process.nextTick(nReadingNextTick, this);
1191+
} else {
1192+
if ((state[kState] & kEofReadablePending) !== 0) {
1193+
// The end-of-stream 'readable' emission was skipped because
1194+
// nobody was listening when the stream ended (see onEofChunk):
1195+
// emit it now.
1196+
state[kState] &= ~kEofReadablePending;
1197+
emitReadable(this);
1198+
}
1199+
if ((state[kState] & kReading) === 0) {
1200+
process.nextTick(nReadingNextTick, this);
1201+
}
11831202
}
11841203
}
11851204
}
Collapse file

‎test/parallel/test-stream-readable-unobserved-eof-readable.js‎

Copy file name to clipboard
+76Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Readable } = require('stream');
4+
const assert = require('assert');
5+
6+
// When a stream reaches its end while nobody is observing it, the
7+
// end-of-stream 'readable' emission is not scheduled. It is emitted
8+
// later if a 'readable' listener is attached, and read()/resume()
9+
// still reach 'end' on their own.
10+
11+
{
12+
// Listener attached synchronously after push(null) gets 'readable'
13+
// and then 'end'.
14+
const r = new Readable({ read() {} });
15+
r.push(null);
16+
let readableEmitted = false;
17+
r.on('readable', common.mustCall(() => {
18+
readableEmitted = true;
19+
assert.strictEqual(r.read(), null);
20+
}));
21+
r.on('end', common.mustCall(() => {
22+
assert.strictEqual(readableEmitted, true);
23+
}));
24+
}
25+
26+
{
27+
// Listener attached one macrotask after the unobserved end still gets
28+
// the owed 'readable' before 'end'.
29+
const r = new Readable({ read() {} });
30+
r.push(null);
31+
setImmediate(common.mustCall(() => {
32+
let readableEmitted = false;
33+
r.on('readable', common.mustCall(() => {
34+
readableEmitted = true;
35+
assert.strictEqual(r.read(), null);
36+
}));
37+
r.on('end', common.mustCall(() => {
38+
assert.strictEqual(readableEmitted, true);
39+
}));
40+
}));
41+
}
42+
43+
{
44+
// A stream that ends unobserved still emits 'end' when resumed later.
45+
const r = new Readable({ read() {} });
46+
r.push(null);
47+
setImmediate(common.mustCall(() => {
48+
r.resume();
49+
r.on('end', common.mustCall());
50+
}));
51+
}
52+
53+
{
54+
// read() after an unobserved end consumes the owed notification: a
55+
// 'readable' listener attached afterwards does not receive it, but
56+
// 'end' is still emitted.
57+
const r = new Readable({ read() {} });
58+
r.push(null);
59+
setImmediate(common.mustCall(() => {
60+
assert.strictEqual(r.read(), null);
61+
r.on('end', common.mustCall());
62+
}));
63+
}
64+
65+
{
66+
// A 'data' listener attached after an unobserved end still gets 'end'.
67+
const r = new Readable({ read() {} });
68+
r.push('x');
69+
r.push(null);
70+
setImmediate(common.mustCall(() => {
71+
r.on('data', common.mustCall((chunk) => {
72+
assert.strictEqual(chunk.toString(), 'x');
73+
}));
74+
r.on('end', common.mustCall());
75+
}));
76+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.