mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
http2: fix eos() for HTTP/2 streams after clean end & close
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> PR-URL: https://github.com/nodejs/node/pull/67553 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
f4fab9770b
commit
db671bb416
3 files changed
+199
-9
No files matched your search
@@ -107,6 +107,16 @@ function getEosOnCloseError(stream, readable, readableFinished, writable, writab
|
||||
return null;
|
||||
}
|
||||
|
||||
// A stream can report itself closed or aborted at the protocol level (e.g.
|
||||
// HTTP/2) while its complete body is still buffered and readable.
|
||||
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);
|
||||
}
|
||||
|
||||
// Internal only: if eos() can settle immediately, invoke the callback before
|
||||
// returning cleanup. Callers must tolerate cleanup yet to be assigned.
|
||||
const kEosNodeSynchronousCallback = Symbol('kEosNodeSynchronousCallback');
|
||||
@@ -156,13 +166,15 @@ function eos(stream, options, callback) {
|
||||
*/
|
||||
let immediateResult;
|
||||
if (isClosed(stream)) {
|
||||
immediateResult = getEosOnCloseError(
|
||||
stream,
|
||||
readable,
|
||||
readableFinished,
|
||||
writable,
|
||||
writableFinished,
|
||||
);
|
||||
if (!isEndPending(stream, readable, readableFinished, writable, writableFinished)) {
|
||||
immediateResult = getEosOnCloseError(
|
||||
stream,
|
||||
readable,
|
||||
readableFinished,
|
||||
writable,
|
||||
writableFinished,
|
||||
);
|
||||
}
|
||||
} else if (wState?.errorEmitted || rState?.errorEmitted) {
|
||||
if (!willEmitClose) {
|
||||
immediateResult = getEosErrored(stream);
|
||||
@@ -270,6 +282,12 @@ function eos(stream, options, callback) {
|
||||
}
|
||||
};
|
||||
|
||||
const onaborted = () => {
|
||||
if (!isEndPending(stream, readable, readableFinished, writable, writableFinished)) {
|
||||
onclose();
|
||||
}
|
||||
};
|
||||
|
||||
const onrequest = () => {
|
||||
stream.req.on('finish', onfinish);
|
||||
};
|
||||
@@ -291,7 +309,7 @@ function eos(stream, options, callback) {
|
||||
|
||||
// Not all streams will emit 'close' after 'aborted'.
|
||||
if (!willEmitClose && typeof stream.aborted === 'boolean') {
|
||||
stream.on('aborted', onclose);
|
||||
stream.on('aborted', onaborted);
|
||||
}
|
||||
|
||||
stream.on('end', onend);
|
||||
@@ -303,7 +321,7 @@ function eos(stream, options, callback) {
|
||||
|
||||
cleanup = () => {
|
||||
callback = nop;
|
||||
stream.removeListener('aborted', onclose);
|
||||
stream.removeListener('aborted', onaborted);
|
||||
stream.removeListener('complete', onfinish);
|
||||
stream.removeListener('abort', onclose);
|
||||
stream.removeListener('request', onrequest);
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
'use strict';
|
||||
|
||||
// A client stream that closed normally can still hold its whole response in
|
||||
// the readable buffer. Consuming it from then on must succeed rather than
|
||||
// report ERR_STREAM_PREMATURE_CLOSE.
|
||||
// Refs: https://github.com/nodejs/node/issues/65677
|
||||
|
||||
const common = require('../common');
|
||||
if (!common.hasCrypto)
|
||||
common.skip('missing crypto');
|
||||
|
||||
const assert = require('assert');
|
||||
const http2 = require('http2');
|
||||
const { once } = require('events');
|
||||
const { Writable } = require('stream');
|
||||
const { finished, pipeline } = require('stream/promises');
|
||||
const { text } = require('stream/consumers');
|
||||
const { setImmediate: setImmediateAsync } = require('timers/promises');
|
||||
|
||||
const body = 'complete response';
|
||||
|
||||
const consumers = {
|
||||
async asyncIterator(request) {
|
||||
let received = '';
|
||||
for await (const chunk of request)
|
||||
received += chunk;
|
||||
return received;
|
||||
},
|
||||
async finishedThenData(request) {
|
||||
let received = '';
|
||||
const done = finished(request, { writable: false });
|
||||
request.on('data', (chunk) => { received += chunk; });
|
||||
await done;
|
||||
return received;
|
||||
},
|
||||
async pipeline(request) {
|
||||
let received = '';
|
||||
await pipeline(request, new Writable({
|
||||
write(chunk, encoding, callback) {
|
||||
received += chunk;
|
||||
callback();
|
||||
},
|
||||
}));
|
||||
return received;
|
||||
},
|
||||
text,
|
||||
};
|
||||
|
||||
const server = http2.createServer();
|
||||
server.on('stream', (stream) => {
|
||||
stream.resume();
|
||||
stream.on('end', () => {
|
||||
stream.respond({ ':status': 200 }, { waitForTrailers: true });
|
||||
stream.on('wantTrailers', () => stream.sendTrailers({ 'grpc-status': '0' }));
|
||||
stream.end(body);
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(0, common.mustCall(async () => {
|
||||
const session = http2.connect(`http://localhost:${server.address().port}`);
|
||||
|
||||
for (const [name, consume] of Object.entries(consumers)) {
|
||||
const request = session.request({ ':method': 'POST' });
|
||||
request.end('request');
|
||||
await once(request, 'response');
|
||||
|
||||
// Attempt to read the response _after_ the server has cleanly closed the stream:
|
||||
while (!request.closed)
|
||||
await setImmediateAsync();
|
||||
|
||||
assert.strictEqual(request.rstCode, http2.constants.NGHTTP2_NO_ERROR);
|
||||
assert.strictEqual(request.destroyed, false);
|
||||
assert.strictEqual(request.readableLength, Buffer.byteLength(body));
|
||||
assert.strictEqual(await consume(request), body, name);
|
||||
}
|
||||
|
||||
session.close();
|
||||
server.close();
|
||||
}));
|
||||
@@ -0,0 +1,93 @@
|
||||
'use strict';
|
||||
|
||||
// Some streams (e.g. Http2Stream) report `closed` or emit 'aborted' at the
|
||||
// protocol level while their complete body is still buffered. That body is
|
||||
// still consumable, so eos() must wait for 'end' rather than reporting a
|
||||
// premature close.
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const { Duplex, Readable, finished } = require('stream');
|
||||
|
||||
function createStream(Type = Readable) {
|
||||
const stream = new Type({ read() {}, write() {}, autoDestroy: false });
|
||||
stream.aborted = false;
|
||||
let protocolClosed = false;
|
||||
Object.defineProperty(stream, 'closed', {
|
||||
get: () => protocolClosed,
|
||||
set: (value) => { protocolClosed = value; },
|
||||
});
|
||||
return stream;
|
||||
}
|
||||
|
||||
{
|
||||
// 'aborted' after the readable ended, with eos() already attached.
|
||||
const stream = createStream();
|
||||
stream.push('body');
|
||||
finished(stream, common.mustSucceed(() => {
|
||||
assert.strictEqual(stream.readableEnded, true);
|
||||
}));
|
||||
stream.push(null);
|
||||
stream.aborted = true;
|
||||
stream.emit('aborted');
|
||||
setImmediate(common.mustCall(() => stream.resume()));
|
||||
}
|
||||
|
||||
{
|
||||
// Already closed when eos() is attached, consumed on a later tick.
|
||||
const stream = createStream();
|
||||
stream.push('body');
|
||||
stream.push(null);
|
||||
stream.closed = true;
|
||||
let received = '';
|
||||
finished(stream, common.mustSucceed(() => {
|
||||
assert.strictEqual(received, 'body');
|
||||
}));
|
||||
setImmediate(common.mustCall(() => {
|
||||
stream.on('data', (chunk) => { received += chunk; });
|
||||
}));
|
||||
}
|
||||
|
||||
{
|
||||
// 'aborted' before the readable ended is still a premature close.
|
||||
const stream = createStream();
|
||||
stream.push('body');
|
||||
finished(stream, common.mustCall((err) => {
|
||||
assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
|
||||
}));
|
||||
stream.aborted = true;
|
||||
stream.emit('aborted');
|
||||
}
|
||||
|
||||
{
|
||||
// Closed before the readable ended is still a premature close.
|
||||
const stream = createStream();
|
||||
stream.push('body');
|
||||
stream.closed = true;
|
||||
finished(stream, common.mustCall((err) => {
|
||||
assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
|
||||
}));
|
||||
}
|
||||
|
||||
{
|
||||
// Destroyed before the buffered body is consumed is a premature close.
|
||||
const stream = createStream();
|
||||
stream.push('body');
|
||||
stream.push(null);
|
||||
stream.closed = true;
|
||||
finished(stream, common.mustCall((err) => {
|
||||
assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
|
||||
}));
|
||||
setImmediate(common.mustCall(() => stream.destroy()));
|
||||
}
|
||||
|
||||
{
|
||||
// A Duplex with an unfinished writable side is still a premature close.
|
||||
const stream = createStream(Duplex);
|
||||
stream.push('body');
|
||||
stream.push(null);
|
||||
stream.closed = true;
|
||||
finished(stream, common.mustCall((err) => {
|
||||
assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user