Skip to content

Commit ec8c98c

Browse files
committed
http2: do not reset stream before END_STREAM is sent
When a compat API response finishes and the request had no body, kMaybeDestroy() takes the graceful-close path and sends RST_STREAM(NO_ERROR) right away. If the connection flow-control window is exhausted at that point, the empty END_STREAM DATA frame is still queued, so the reset goes out first and the END_STREAM frame is dropped, truncating an otherwise complete response. Skip the graceful close when the request already ended with its HEADERS frame, since there is no request body to abort. Fixes: nodejs#66525 Refs: nodejs#64265 Refs: nodejs#64663 Signed-off-by: Barath <[email protected]>
1 parent 7615781 commit ec8c98c

2 files changed

Lines changed: 134 additions & 3 deletions

File tree

‎lib/internal/http2/core.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,15 +2595,18 @@ class Http2Stream extends Duplex {
25952595
}
25962596

25972597
// We've submitted a response from our server session, have not attempted
2598-
// to process any incoming data, and have no trailers. This means we can
2599-
// attempt to gracefully close the session.
2598+
// to process any incoming data, and have no trailers. If the client may
2599+
// still be sending a request body, gracefully close the stream to stop it.
2600+
// If the request already ended with its headers, there is nothing to stop,
2601+
// and closing early could reset the stream before END_STREAM is sent.
26002602
const state = this[kState];
26012603
if (this.headersSent &&
26022604
this[kSession] &&
26032605
this[kSession][kType] === NGHTTP2_SESSION_SERVER &&
26042606
!(state.flags & STREAM_FLAGS_HAS_TRAILERS) &&
26052607
!state.didRead &&
2606-
this.readableFlowing === null) {
2608+
this.readableFlowing === null &&
2609+
!this.endAfterHeaders) {
26072610
// By using setImmediate we allow pushStreams to make it through
26082611
// before the stream is officially closed. This prevents a bug
26092612
// in most browsers where those pushStreams would be rejected.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
3+
// Regression test for https://github.com/nodejs/node/issues/66525.
4+
// A compat API response to a request without a body must end with
5+
// END_STREAM, even if the connection flow-control window is exhausted
6+
// when the empty END_STREAM DATA frame is submitted. Previously the
7+
// stream was reset with RST_STREAM(NO_ERROR) and the queued END_STREAM
8+
// frame was dropped.
9+
10+
const common = require('../common');
11+
if (!common.hasCrypto)
12+
common.skip('missing crypto');
13+
14+
const assert = require('assert');
15+
const http2 = require('http2');
16+
const net = require('net');
17+
18+
const kData = 0;
19+
const kHeaders = 1;
20+
const kRstStream = 3;
21+
const kSettings = 4;
22+
const kWindowUpdate = 8;
23+
const kFlagEndStream = 0x1;
24+
const kFlagAck = 0x1;
25+
const kFlagsEndStreamEndHeaders = 0x5;
26+
27+
function frame(type, flags, id, payload = Buffer.alloc(0)) {
28+
const header = Buffer.alloc(9);
29+
header.writeUIntBE(payload.length, 0, 3);
30+
header[3] = type;
31+
header[4] = flags;
32+
header.writeUInt32BE(id, 5);
33+
return Buffer.concat([header, payload]);
34+
}
35+
36+
// Minimal HPACK encoding of a GET request for `path`: indexed :method GET,
37+
// indexed :scheme http, and literal :path and :authority without Huffman.
38+
function requestHeaders(path) {
39+
const literal = (index, value) =>
40+
Buffer.concat([Buffer.from([index, value.length]), Buffer.from(value)]);
41+
return Buffer.concat([
42+
Buffer.from([0x82, 0x86]),
43+
literal(0x04, path),
44+
literal(0x01, 'localhost'),
45+
]);
46+
}
47+
48+
let client;
49+
let endStreamReceived = false;
50+
let serverStreamClosed = false;
51+
52+
function maybeFinish() {
53+
if (endStreamReceived && serverStreamClosed) {
54+
client.destroy();
55+
server.close();
56+
}
57+
}
58+
59+
const server = http2.createServer();
60+
61+
server.on('stream', common.mustCall((stream, headers) => {
62+
if (headers[':path'] === '/small') {
63+
// The stream must still close on its own once END_STREAM has been sent.
64+
stream.on('close', common.mustCall(() => {
65+
serverStreamClosed = true;
66+
maybeFinish();
67+
}));
68+
}
69+
}, 2));
70+
71+
const responses = {};
72+
server.on('request', common.mustCall((req, res) => {
73+
responses[req.url] = res;
74+
if (!responses['/small'] || !responses['/big']) return;
75+
// After /small's body has been written, let /big exhaust the connection
76+
// window before /small's END_STREAM frame is submitted.
77+
responses['/small'].stream.once('wantTrailers', () => {
78+
responses['/big'].end('y'.repeat(1_000_000));
79+
});
80+
responses['/small'].end('x'.repeat(1000));
81+
}, 2));
82+
83+
server.listen(0, common.mustCall(() => {
84+
// A raw client that does not send WINDOW_UPDATE until later, so the
85+
// connection window stays exhausted once /big has drained it.
86+
client = net.connect(server.address().port, common.mustCall(() => {
87+
client.write(Buffer.concat([
88+
Buffer.from('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'),
89+
frame(kSettings, 0, 0),
90+
frame(kHeaders, kFlagsEndStreamEndHeaders, 1, requestHeaders('/big')),
91+
frame(kHeaders, kFlagsEndStreamEndHeaders, 3, requestHeaders('/small')),
92+
]));
93+
}));
94+
95+
let buffered = Buffer.alloc(0);
96+
client.on('data', common.mustCallAtLeast((chunk) => {
97+
buffered = Buffer.concat([buffered, chunk]);
98+
while (buffered.length >= 9 &&
99+
buffered.length >= 9 + buffered.readUIntBE(0, 3)) {
100+
const length = buffered.readUIntBE(0, 3);
101+
const type = buffered[3];
102+
const flags = buffered[4];
103+
const id = buffered.readUInt32BE(5) & 0x7fffffff;
104+
buffered = buffered.subarray(9 + length);
105+
106+
if (type === kSettings && !(flags & kFlagAck))
107+
client.write(frame(kSettings, kFlagAck, 0));
108+
if (id !== 3) continue;
109+
110+
assert.ok(type !== kRstStream || endStreamReceived,
111+
'stream /small was reset before END_STREAM was sent');
112+
if ((type === kData || type === kHeaders) && (flags & kFlagEndStream)) {
113+
endStreamReceived = true;
114+
maybeFinish();
115+
}
116+
}
117+
}));
118+
119+
// Reopen the windows later. A correct server sends /small's END_STREAM then.
120+
setTimeout(common.mustCall(() => {
121+
const increment = Buffer.alloc(4);
122+
increment.writeUInt32BE(10_000_000);
123+
client.write(Buffer.concat([
124+
frame(kWindowUpdate, 0, 0, increment),
125+
frame(kWindowUpdate, 0, 1, increment),
126+
]));
127+
}), common.platformTimeout(300));
128+
}));

0 commit comments

Comments
 (0)