http2: fix eos() for HTTP/2 streams after clean end & close #67553
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 314 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.344
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #67553"
general
weight: 0.35
mcollina
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 https://github.com/nodejs/node/pull/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 likefinished(),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
closedproperty represents whether the actual stream is closed on the wire. It predates the Node streamclosedfield, 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.closedbecomestruebefore'end'has been emitted, and soeos()reports this as a premature close.For a normal Node stream, in the equivalent case
'close'wouldn't fire andclosedwould stay false until the data was actually read (the close is implicitly queued behind the data) soeos()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 #66693 case.This PR fixes all that:
It defines an
isEndPendingfunction, to check whether a stream has happily ended data buffered already.eos()now checksisEndPendingfor streams that are already closed (fixing #65677) and for streams that are later aborted (fixing #66693).In both cases, if there's a buffered successful end, it doesn't consider the stream failed, and instead continues its normal logic (waiting for other events to fire, either when the data is read later or when an actual error appears).
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.