Skip to content

stream: keep data buffered during synchronous flow - #67599

Open
jakecastelli wants to merge 1 commit into
nodejs:mainfrom
jakecastelli:stream-fix-unshift-flowsync
Open

jakecastelli wants to merge 1 commit into
nodejs:mainfrom
jakecastelli:stream-fix-unshift-flowsync

Conversation

@jakecastelli

@jakecastelli jakecastelli commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

The flowing fast path keeps the prefetched chunk off the buffer and
resets state.length before each _read(). A chunk put on the buffer
by unshift() or push() from a 'data' listener while a prefetched
chunk was pending was not accounted for, so it was never delivered and
the stream ended with data still buffered.

Move the prefetched chunk onto the buffer before unshift() and
push() add to it. That keeps the chunks in order and makes the fast
path fall back to read().

Also leave the fast push mode when _read() calls unshift(),
read() or setEncoding(). A following push() then adds to the
buffer and goes through the decoder instead of overwriting
state.length or delivering an undecoded chunk.

Fixes: #67598

Assisted-by: claude, manually reviewed by a human

The flowing fast path keeps the prefetched chunk off the buffer and
resets `state.length` before each `_read()`. A chunk put on the buffer
by `unshift()` or `push()` from a 'data' listener while a prefetched
chunk was pending was not accounted for, so it was never delivered and
the stream ended with data still buffered.

Move the prefetched chunk onto the buffer before `unshift()` and
`push()` add to it. That keeps the chunks in order and makes the fast
path fall back to `read()`.

Also leave the fast push mode when `_read()` calls `unshift()`,
`read()` or `setEncoding()`. A following `push()` then adds to the
buffer and goes through the decoder instead of overwriting
`state.length` or delivering an undecoded chunk.

Signed-off-by: jakecastelli <38635403+jakecastelli@users.noreply.github.com>
Assisted-by: claude:fable-5.2
@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 19, 2026
@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.72%. Comparing base (b7df44b) to head (db750c7).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67599      +/-   ##
==========================================
- Coverage   90.73%   90.72%   -0.01%     
==========================================
  Files         801      801              
  Lines      280787   280810      +23     
  Branches    54221    54220       -1     
==========================================
+ Hits       254765   254777      +12     
+ Misses      16203    16198       -5     
- Partials     9819     9835      +16     
Files with missing lines Coverage Δ
lib/internal/streams/readable.js 98.98% <100.00%> (+0.05%) ⬆️

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

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.

Readable.unshift() can silently drop data during synchronous flow

3 participants