Skip to content

stream: recompute the close error when eos() defers - #65689

Open
bitpshr wants to merge 1 commit into
nodejs:mainfrom
bitpshr:stream/eos-recompute-close-error
Open

stream: recompute the close error when eos() defers#65689
bitpshr wants to merge 1 commit into
nodejs:mainfrom
bitpshr:stream/eos-recompute-close-error

Conversation

@bitpshr

@bitpshr bitpshr commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Fixes: #65677

Great report, the analysis in the issue is spot on and made this easy to confirm.

When eos() runs against a stream that has already closed, it computed the premature close error immediately and then delivered it on the next tick. A closed stream can still be holding its whole body in the readable buffer, so anything that drains that buffer in between, such as async iteration starting up, leaves the callback reporting a state that no longer applies. An HTTP/2 response that was fully buffered before the stream closed normally fails with ERR_STREAM_PREMATURE_CLOSE.

This recomputes the result inside the deferred callback instead, which is what the 'close' handler further down the same file already does. Only the already-closed branch is affected. The other branches capture an error that was already emitted, so they cannot go stale, and the synchronous callback path returns before the tick.

Verification, since this sits under a lot of streams:

  • The reproduction from the issue goes from ERR_STREAM_PREMATURE_CLOSE to passing, and v22 passes it both ways as a control.
  • The added regression test fails on an unpatched build with exactly ERR_STREAM_PREMATURE_CLOSE, so it is not passing vacuously.
  • Full parallel and sequential runs on patched and unpatched builds produced nearly disjoint failure sets, 9 unique to patched and 14 unique to baseline, with only a local fixture issue common to both. The unpatched run failed more tests than the patched one. A handful of TLS tests appeared in the patched run, so I ran those five six more times against the patched build and they passed every time.

Worth flagging that the reporter suspects #62394 / cbee0de as the origin, which matches the version boundary in their table, so v24.x and v26.x are likely to want this too.

When eos() is called on a stream that has already closed, the premature
close error was computed right away and then delivered a tick later.
A closed stream can still hold data in its readable buffer, so anything
that consumes the buffer in between, such as async iteration starting,
left the callback reporting a state that no longer applied. HTTP/2
responses that were fully buffered before the stream closed normally
failed with ERR_STREAM_PREMATURE_CLOSE.

Recompute the result inside the deferred callback instead, which is what
the 'close' handler in the same file already does. Only the closed
stream branch is affected; the others capture an already emitted error
that cannot go stale.

Fixes: nodejs#65677
Signed-off-by: Paul Bouchon <mail@bitpshr.net>
@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 Aug 31, 2026
@codecov

codecov Bot commented Aug 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.06%. Comparing base (705646f) to head (7f7dc98).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65689      +/-   ##
==========================================
- Coverage   90.07%   90.06%   -0.01%     
==========================================
  Files         754      754              
  Lines      256395   256406      +11     
  Branches    48499    48504       +5     
==========================================
- Hits       230937   230932       -5     
- Misses      16569    16578       +9     
- Partials     8889     8896       +7     
Files with missing lines Coverage Δ
lib/internal/streams/end-of-stream.js 98.56% <100.00%> (+0.03%) ⬆️

... and 28 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.

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.

http2: async iteration reports premature close with buffered responses in v26.3.0+ and v24.20.0

2 participants