http: fixes memory retention issue with FreeList and HTTPParser

Fixes: https://github.com/nodejs/node/issues/29394
Refs: https://github.com/nodejs/node/pull/33167#issuecomment-622102450

PR-URL: https://github.com/nodejs/node/pull/33190
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
pull/33251/head
John Leidegren 2020-05-01 08:14:25 +02:00 committed by Gerhard Stoebich
parent c17dcb3253
commit 26f150022f
2 changed files with 44 additions and 2 deletions

View File

@ -161,12 +161,10 @@ const parsers = new FreeList('parsers', 1000, function parsersCb() {
cleanParser(parser);
parser.onIncoming = null;
parser[kOnHeaders] = parserOnHeaders;
parser[kOnHeadersComplete] = parserOnHeadersComplete;
parser[kOnBody] = parserOnBody;
parser[kOnMessageComplete] = parserOnMessageComplete;
parser[kOnTimeout] = null;
return parser;
});
@ -232,7 +230,9 @@ function cleanParser(parser) {
parser.outgoing = null;
parser.maxHeaderPairs = MAX_HEADER_PAIRS;
parser[kOnExecute] = null;
parser[kOnTimeout] = null;
parser._consumed = false;
parser.onIncoming = null;
}
function prepareError(err, parser, rawPacket) {

View File

@ -0,0 +1,42 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const { HTTPParser } = require('_http_common');
// Test that the `HTTPParser` instance is cleaned up before being returned to
// the pool to avoid memory retention issues.
const kOnTimeout = HTTPParser.kOnTimeout | 0;
const server = http.createServer();
server.on('request', common.mustCall((request, response) => {
const parser = request.socket.parser;
assert.strictEqual(typeof parser[kOnTimeout], 'function');
request.socket.on('close', common.mustCall(() => {
assert.strictEqual(parser[kOnTimeout], null);
}));
response.end();
server.close();
}));
server.listen(common.mustCall(() => {
const request = http.get({ port: server.address().port });
let parser;
request.on('socket', common.mustCall(() => {
parser = request.parser;
assert.strictEqual(typeof parser.onIncoming, 'function');
}));
request.on('response', common.mustCall((response) => {
response.resume();
response.on('end', common.mustCall(() => {
assert.strictEqual(parser.onIncoming, null);
}));
}));
}));