Skip to content

stream: emit end when paused on the last chunk - #67769

Open
christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:stream-pipe-end-backpressure
Open

christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:stream-pipe-end-backpressure

Conversation

@christianaurichzm

Copy link
Copy Markdown
Member

Since v26.11.0, a flowing Readable doesn't emit 'end' when _read() pushes EOF in the call after the last chunk and the consumer pauses on that chunk:

const { Readable } = require('node:stream');
const chunks = [Buffer.from('a'), Buffer.from('b')];
const r = new Readable({ read() { this.push(chunks.shift() ?? null); } });
r.on('data', (chunk) => { if (chunk.toString() === 'b') r.pause(); });
r.on('end', () => console.log('end'));

This prints end on v22 and nothing on main. pipe() pauses there when write() returns false, so the destination isn't ended unless it drains again.

flowSync() from #66182 runs _read() before delivering each chunk, like read(), but skips the endReadable() that read() schedules when that _read() hits EOF. This schedules it at the same point.

Thanks to @AZMCode for tracing this to flowSync().

Fixes: #67762

flowSync() calls _read() before delivering each chunk, like read(),
but did not schedule 'end' when that call pushed EOF and nothing was
left to deliver. A consumer that paused on the last chunk, such as
pipe() after write() returned false, then got no 'end' until it
resumed. Schedule endReadable() there, as read() does.

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams. labels Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stream: pipe() no longer ends the destination when EOF follows a backpressured last chunk (regression in v26.11.0)

2 participants