Repository navigation
Conversation
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>
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
|
Hi @nodejs/streams, wanted to keep this on your radar. Fixes a regression from #62394 that makes buffered HTTP/2 responses fail async iteration with ERR_STREAM_PREMATURE_CLOSE. Green, and the test hangs on an unpatched build so it does catch the bug. |
pimterry
left a comment
There was a problem hiding this comment.
I've been digging into this as part of working on #66693, and I'm not sure just deferring the check is the right fix for this part.
It does solve that original repro, but it doesn't catch other issues. Fundamentally just checking getEosOnCloseError isn't quite right here, because for HTTP/2 stream.closed can be true any time arbitrarily before the 'end' is actually delivered. E.g. if we've received the full data and the stream has closed, but the data is buffered (the stream was paused, or not read yet at all) then it won't emit end yet, even after another tick, but also it's not an premature close. Waiting a tick doesn't help for cases like this.
What if we use something like:
function isEndPending(stream, readable, readableFinished, writable, writableFinished) {
if (stream.destroyed) return false;
const rState = stream._readableState;
const rPending = readable && !readableFinished &&
rState?.ended === true && !rState.errored;
return rPending && (!writable || writableFinished);
}I.e. actually check if there is a pending EOF inside the buffered data already.
And then wrap immediateResult = getEosOnCloseError(... with if (!isEndPending(...) so if there's a pending end in the data, we do nothing for now, and wait for the 'end' to fire once it's actually read. That would let us avoid the deferred check flow, and check this more precisely for a broader set of cases. I think it should cover the original issue and the broader case too.
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 withERR_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:
ERR_STREAM_PREMATURE_CLOSEto passing, and v22 passes it both ways as a control.ERR_STREAM_PREMATURE_CLOSE, so it is not passing vacuously.parallelandsequentialruns 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.