GeezerVison is showing github.com — http2: fix eos() for HTTP/2 streams after clean end & close · nodejs/node@e3eef84
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/end-of-stream.js +27-9Lines changed: 27 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -107,6 +107,16 @@ function getEosOnCloseError(stream, readable, readableFinished, writable, writab | 107 | 107 | return null; | 108 | 108 | } | 109 | 109 |
| | 110 | +// A stream can report itself closed or aborted at the protocol level (e.g. | | 111 | +// HTTP/2) while its complete body is still buffered and readable. | | 112 | +function isEndPending(stream, readable, readableFinished, writable, writableFinished) { | | 113 | + if (stream.destroyed) return false; | | 114 | + const rState = stream._readableState; | | 115 | + const rPending = readable && !readableFinished && | | 116 | + rState?.ended === true && !rState.errored; | | 117 | + return rPending && (!writable || writableFinished); | | 118 | +} | | 119 | + | 110 | 120 | // Internal only: if eos() can settle immediately, invoke the callback before | 111 | 121 | // returning cleanup. Callers must tolerate cleanup yet to be assigned. | 112 | 122 | const kEosNodeSynchronousCallback = Symbol('kEosNodeSynchronousCallback'); | @@ -156,13 +166,15 @@ function eos(stream, options, callback) { | 156 | 166 | */ | 157 | 167 | let immediateResult; | 158 | 168 | if (isClosed(stream)) { | 159 | | - immediateResult = getEosOnCloseError( | 160 | | - stream, | 161 | | - readable, | 162 | | - readableFinished, | 163 | | - writable, | 164 | | - writableFinished, | 165 | | - ); | | 169 | + if (!isEndPending(stream, readable, readableFinished, writable, writableFinished)) { | | 170 | + immediateResult = getEosOnCloseError( | | 171 | + stream, | | 172 | + readable, | | 173 | + readableFinished, | | 174 | + writable, | | 175 | + writableFinished, | | 176 | + ); | | 177 | + } | 166 | 178 | } else if (wState?.errorEmitted || rState?.errorEmitted) { | 167 | 179 | if (!willEmitClose) { | 168 | 180 | immediateResult = getEosErrored(stream); | @@ -270,6 +282,12 @@ function eos(stream, options, callback) { | 270 | 282 | } | 271 | 283 | }; | 272 | 284 |
| | 285 | + const onaborted = () => { | | 286 | + if (!isEndPending(stream, readable, readableFinished, writable, writableFinished)) { | | 287 | + onclose(); | | 288 | + } | | 289 | + }; | | 290 | + | 273 | 291 | const onrequest = () => { | 274 | 292 | stream.req.on('finish', onfinish); | 275 | 293 | }; | @@ -291,7 +309,7 @@ function eos(stream, options, callback) { | 291 | 309 |
| 292 | 310 | // Not all streams will emit 'close' after 'aborted'. | 293 | 311 | if (!willEmitClose && typeof stream.aborted === 'boolean') { | 294 | | - stream.on('aborted', onclose); | | 312 | + stream.on('aborted', onaborted); | 295 | 313 | } | 296 | 314 |
| 297 | 315 | stream.on('end', onend); | @@ -303,7 +321,7 @@ function eos(stream, options, callback) { | 303 | 321 |
| 304 | 322 | cleanup = () => { | 305 | 323 | callback = nop; | 306 | | - stream.removeListener('aborted', onclose); | | 324 | + stream.removeListener('aborted', onaborted); | 307 | 325 | stream.removeListener('complete', onfinish); | 308 | 326 | stream.removeListener('abort', onclose); | 309 | 327 | stream.removeListener('request', onrequest); | |
Collapse file Copy file name to clipboard +79Lines changed: 79 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// A client stream that closed normally can still hold its whole response in | | 4 | +// the readable buffer. Consuming it from then on must succeed rather than | | 5 | +// report ERR_STREAM_PREMATURE_CLOSE. | | 6 | +// Refs: https://github.com/nodejs/node/issues/65677 | | 7 | + | | 8 | +const common = require('../common'); | | 9 | +if (!common.hasCrypto) | | 10 | + common.skip('missing crypto'); | | 11 | + | | 12 | +const assert = require('assert'); | | 13 | +const http2 = require('http2'); | | 14 | +const { once } = require('events'); | | 15 | +const { Writable } = require('stream'); | | 16 | +const { finished, pipeline } = require('stream/promises'); | | 17 | +const { text } = require('stream/consumers'); | | 18 | +const { setImmediate: setImmediateAsync } = require('timers/promises'); | | 19 | + | | 20 | +const body = 'complete response'; | | 21 | + | | 22 | +const consumers = { | | 23 | + async asyncIterator(request) { | | 24 | + let received = ''; | | 25 | + for await (const chunk of request) | | 26 | + received += chunk; | | 27 | + return received; | | 28 | + }, | | 29 | + async finishedThenData(request) { | | 30 | + let received = ''; | | 31 | + const done = finished(request, { writable: false }); | | 32 | + request.on('data', (chunk) => { received += chunk; }); | | 33 | + await done; | | 34 | + return received; | | 35 | + }, | | 36 | + async pipeline(request) { | | 37 | + let received = ''; | | 38 | + await pipeline(request, new Writable({ | | 39 | + write(chunk, encoding, callback) { | | 40 | + received += chunk; | | 41 | + callback(); | | 42 | + }, | | 43 | + })); | | 44 | + return received; | | 45 | + }, | | 46 | + text, | | 47 | +}; | | 48 | + | | 49 | +const server = http2.createServer(); | | 50 | +server.on('stream', (stream) => { | | 51 | + stream.resume(); | | 52 | + stream.on('end', () => { | | 53 | + stream.respond({ ':status': 200 }, { waitForTrailers: true }); | | 54 | + stream.on('wantTrailers', () => stream.sendTrailers({ 'grpc-status': '0' })); | | 55 | + stream.end(body); | | 56 | + }); | | 57 | +}); | | 58 | + | | 59 | +server.listen(0, common.mustCall(async () => { | | 60 | + const session = http2.connect(`http://localhost:${server.address().port}`); | | 61 | + | | 62 | + for (const [name, consume] of Object.entries(consumers)) { | | 63 | + const request = session.request({ ':method': 'POST' }); | | 64 | + request.end('request'); | | 65 | + await once(request, 'response'); | | 66 | + | | 67 | + // Attempt to read the response _after_ the server has cleanly closed the stream: | | 68 | + while (!request.closed) | | 69 | + await setImmediateAsync(); | | 70 | + | | 71 | + assert.strictEqual(request.rstCode, http2.constants.NGHTTP2_NO_ERROR); | | 72 | + assert.strictEqual(request.destroyed, false); | | 73 | + assert.strictEqual(request.readableLength, Buffer.byteLength(body)); | | 74 | + assert.strictEqual(await consume(request), body, name); | | 75 | + } | | 76 | + | | 77 | + session.close(); | | 78 | + server.close(); | | 79 | +})); |
Collapse file Copy file name to clipboard +93Lines changed: 93 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Some streams (e.g. Http2Stream) report `closed` or emit 'aborted' at the | | 4 | +// protocol level while their complete body is still buffered. That body is | | 5 | +// still consumable, so eos() must wait for 'end' rather than reporting a | | 6 | +// premature close. | | 7 | + | | 8 | +const common = require('../common'); | | 9 | +const assert = require('assert'); | | 10 | +const { Duplex, Readable, finished } = require('stream'); | | 11 | + | | 12 | +function createStream(Type = Readable) { | | 13 | + const stream = new Type({ read() {}, write() {}, autoDestroy: false }); | | 14 | + stream.aborted = false; | | 15 | + let protocolClosed = false; | | 16 | + Object.defineProperty(stream, 'closed', { | | 17 | + get: () => protocolClosed, | | 18 | + set: (value) => { protocolClosed = value; }, | | 19 | + }); | | 20 | + return stream; | | 21 | +} | | 22 | + | | 23 | +{ | | 24 | + // 'aborted' after the readable ended, with eos() already attached. | | 25 | + const stream = createStream(); | | 26 | + stream.push('body'); | | 27 | + finished(stream, common.mustSucceed(() => { | | 28 | + assert.strictEqual(stream.readableEnded, true); | | 29 | + })); | | 30 | + stream.push(null); | | 31 | + stream.aborted = true; | | 32 | + stream.emit('aborted'); | | 33 | + setImmediate(common.mustCall(() => stream.resume())); | | 34 | +} | | 35 | + | | 36 | +{ | | 37 | + // Already closed when eos() is attached, consumed on a later tick. | | 38 | + const stream = createStream(); | | 39 | + stream.push('body'); | | 40 | + stream.push(null); | | 41 | + stream.closed = true; | | 42 | + let received = ''; | | 43 | + finished(stream, common.mustSucceed(() => { | | 44 | + assert.strictEqual(received, 'body'); | | 45 | + })); | | 46 | + setImmediate(common.mustCall(() => { | | 47 | + stream.on('data', (chunk) => { received += chunk; }); | | 48 | + })); | | 49 | +} | | 50 | + | | 51 | +{ | | 52 | + // 'aborted' before the readable ended is still a premature close. | | 53 | + const stream = createStream(); | | 54 | + stream.push('body'); | | 55 | + finished(stream, common.mustCall((err) => { | | 56 | + assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE'); | | 57 | + })); | | 58 | + stream.aborted = true; | | 59 | + stream.emit('aborted'); | | 60 | +} | | 61 | + | | 62 | +{ | | 63 | + // Closed before the readable ended is still a premature close. | | 64 | + const stream = createStream(); | | 65 | + stream.push('body'); | | 66 | + stream.closed = true; | | 67 | + finished(stream, common.mustCall((err) => { | | 68 | + assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE'); | | 69 | + })); | | 70 | +} | | 71 | + | | 72 | +{ | | 73 | + // Destroyed before the buffered body is consumed is a premature close. | | 74 | + const stream = createStream(); | | 75 | + stream.push('body'); | | 76 | + stream.push(null); | | 77 | + stream.closed = true; | | 78 | + finished(stream, common.mustCall((err) => { | | 79 | + assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE'); | | 80 | + })); | | 81 | + setImmediate(common.mustCall(() => stream.destroy())); | | 82 | +} | | 83 | + | | 84 | +{ | | 85 | + // A Duplex with an unfinished writable side is still a premature close. | | 86 | + const stream = createStream(Duplex); | | 87 | + stream.push('body'); | | 88 | + stream.push(null); | | 89 | + stream.closed = true; | | 90 | + finished(stream, common.mustCall((err) => { | | 91 | + assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE'); | | 92 | + })); | | 93 | +} |
|