Skip to content

Commit b84b4d8

Browse files
committed
http2: fix tracking received data for maxSessionMemory
Track received data correctly. Specifically, for the buffer that is used for receiving data, we previously would try to increment the current memory usage by its length, and later decrement it by that, but in the meantime the buffer had been turned over to V8 and its length reset to zero. This gave the impression that more and more memory was consumed by the HTTP/2 session when it was in fact not. Fixes: #27416 Refs: #26207 PR-URL: #27914 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent eb4c1d5 commit b84b4d8

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/node_http2.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -1782,11 +1782,13 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
17821782
// Shrink to the actual amount of used data.
17831783
buf.Resize(nread);
17841784

1785-
IncrementCurrentSessionMemory(buf.size());
1785+
IncrementCurrentSessionMemory(nread);
17861786
OnScopeLeave on_scope_leave([&]() {
17871787
// Once finished handling this write, reset the stream buffer.
17881788
// The memory has either been free()d or was handed over to V8.
1789-
DecrementCurrentSessionMemory(buf.size());
1789+
// We use `nread` instead of `buf.size()` here, because the buffer is
1790+
// cleared as part of the `.ToArrayBuffer()` call below.
1791+
DecrementCurrentSessionMemory(nread);
17901792
stream_buf_ab_ = Local<ArrayBuffer>();
17911793
stream_buf_ = uv_buf_init(nullptr, 0);
17921794
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
const http2 = require('http2');
6+
7+
// Regression test for https://github.com/nodejs/node/issues/27416.
8+
// Check that received data is accounted for correctly in the maxSessionMemory
9+
// mechanism.
10+
11+
const bodyLength = 8192;
12+
const maxSessionMemory = 1; // 1 MB
13+
const requestCount = 1000;
14+
15+
const server = http2.createServer({ maxSessionMemory });
16+
server.on('stream', (stream) => {
17+
stream.respond();
18+
stream.end();
19+
});
20+
21+
server.listen(common.mustCall(() => {
22+
const client = http2.connect(`http://localhost:${server.address().port}`, {
23+
maxSessionMemory
24+
});
25+
26+
function request() {
27+
return new Promise((resolve, reject) => {
28+
const stream = client.request({
29+
':method': 'POST',
30+
'content-length': bodyLength
31+
});
32+
stream.on('error', reject);
33+
stream.on('response', resolve);
34+
stream.end('a'.repeat(bodyLength));
35+
});
36+
}
37+
38+
(async () => {
39+
for (let i = 0; i < requestCount; i++) {
40+
await request();
41+
}
42+
43+
client.close();
44+
server.close();
45+
})().then(common.mustCall());
46+
}));

0 commit comments

Comments
 (0)