Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: reset parser.incoming when server request is finished #29297

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,27 @@ function onParserExecuteCommon(server, socket, parser, state, ret, d) {
}
}

function clearIncoming(req) {
req = req || this;
const parser = req.socket && req.socket.parser;
// Reset the .incoming property so that the request object can be gc'ed.
if (parser && parser.incoming === req) {
if (req.readableEnded) {
parser.incoming = null;
} else {
req.on('end', clearIncoming);
}
}
}

function resOnFinish(req, res, socket, state, server) {
// Usually the first incoming element should be our request. it may
// be that in the case abortIncoming() was called that the incoming
// array will be empty.
assert(state.incoming.length === 0 || state.incoming[0] === req);

state.incoming.shift();
clearIncoming(req);

// If the user never called req.read(), and didn't pipe() or
// .resume() or .on('data'), then we call req._dump() so that the
Expand Down
15 changes: 12 additions & 3 deletions test/parallel/test-http-server-keepalive-end.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { createServer } = require('http');
const { connect } = require('net');

// Make sure that for HTTP keepalive requests, the .on('end') event is emitted
// on the incoming request object, and that the parser instance does not hold
// on to that request object afterwards.

const server = createServer(common.mustCall((req, res) => {
req.on('end', common.mustCall());
req.on('end', common.mustCall(() => {
const parser = req.socket.parser;
assert.strictEqual(parser.incoming, req);
process.nextTick(() => {
assert.strictEqual(parser.incoming, null);
});
}));
res.end('hello world');
}));

server.unref();

server.listen(0, common.mustCall(() => {

const client = connect(server.address().port);

const req = [
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-http-server-keepalive-req-gc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Flags: --expose-gc
'use strict';
const common = require('../common');
const onGC = require('../common/ongc');
const { createServer } = require('http');
const { connect } = require('net');

if (common.isWindows) {
// TODO(addaleax): Investigate why and remove the skip.
common.skip('This test is flaky on Windows.');
}

// Make sure that for HTTP keepalive requests, the req object can be
// garbage collected once the request is finished.
// Refs: https://github.com/nodejs/node/issues/9668

let client;
const server = createServer(common.mustCall((req, res) => {
onGC(req, { ongc: common.mustCall() });
req.resume();
req.on('end', common.mustCall(() => {
setImmediate(() => {
client.end();
addaleax marked this conversation as resolved.
Show resolved Hide resolved
global.gc();
});
}));
res.end('hello world');
}));

server.unref();

server.listen(0, common.mustCall(() => {
client = connect(server.address().port);

const req = [
'POST / HTTP/1.1',
`Host: localhost:${server.address().port}`,
'Connection: keep-alive',
'Content-Length: 11',
'',
'hello world',
''
].join('\r\n');

client.write(req);
client.unref();
}));