Skip to content

http2: fix eos() for HTTP/2 streams after clean end & close - #67553

Open
pimterry wants to merge 1 commit into
nodejs:mainfrom
pimterry:fix-stream-premature-close
Open

pimterry wants to merge 1 commit into
nodejs:mainfrom
pimterry:fix-stream-premature-close

Conversation

@pimterry

Copy link
Copy Markdown
Member

This fixes #65677, replacing #65689. The latter does also fix that repro and a subset of the affected cases, but it's not complete and it's now stalled in review.

It also fixes #66693 for v26 (but not fully for v27 & main yet, which need a non-backportable fix to cover the new breaking change semantics in #63249 too - separate PR for that coming shortly).

The problem:

  • eos() takes a stream and a callback, and calls the callback when the stream cleanly completes, or with an error if not. This is used internally to implement various public APIs like finished(), pipeline(), and async iteration.
  • When first called, this checks stream.closed, and if it's already closed but 'end' hasn't been emitted then it treats this as a PREMATURE_CLOSE error.
  • HTTP/2 stream's closed property represents whether the actual stream is closed on the wire. It predates the Node stream closed field, which is only set after 'close' is emitted.
  • If an HTTP/2 server sends a complete data stream (with a clean end) after a completed request, immediately closing the stream on the wire, but the client doesn't immediately start reading, then h2Stream.closed becomes true before 'end' has been emitted, and so eos() reports this as a premature close.
  • For a normal Node stream, in the equivalent case 'close' wouldn't fire and closed would stay false until the data was actually read (the close is implicitly queued behind the data) so eos() wouldn't treat this as an error.
  • That's correct - this shouldn't be an error, and it shouldn't be an error in the HTTP/2 case either. The data is still there, and reading will be successful to the end of the stream.
  • Very similar logic applies to 'aborted' events while the full data is already buffered, this is the http2: async iterator rejects complete buffered response after NO_ERROR reset with request side open #66693 case.

This PR fixes all that:

This improves on the fix in #65689 for #65677, which only handled the case where the data was all read within the next tick, and still treated this as an error otherwise.

Previously this could fail - not having read 'end' when the close
arrived was treated as a premature close error for finished() and
related APIs, even though the body data was cleanly ended and complete.
We now check whether there is a clean end buffered. If so, we wait
until it's read before evaluating whether the stream ended cleanly.

Signed-off-by: Tim Perry <pimterry@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 17, 2026
@pimterry

Copy link
Copy Markdown
Member Author

cc @nodejs/http2 as well (streams code, but fix only applies to HTTP/2 streams I think)

@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.68%. Comparing base (1c40cfa) to head (34b064d).
⚠️ Report is 41 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67553      +/-   ##
==========================================
+ Coverage   90.67%   90.68%   +0.01%     
==========================================
  Files         800      800              
  Lines      280889   281019     +130     
  Branches    54274    54303      +29     
==========================================
+ Hits       254684   254850     +166     
+ Misses      16295    16262      -33     
+ Partials     9910     9907       -3     
Files with missing lines Coverage Δ
lib/internal/streams/end-of-stream.js 99.76% <100.00%> (+0.01%) ⬆️

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

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@pimterry pimterry added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

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

4 participants