Skip to content

Commit

Permalink
http: free parser on keepalive
Browse files Browse the repository at this point in the history
Free parser when putting socket back in the agent
keep alive free list.

Fixes: nodejs#29394
  • Loading branch information
ronag committed Apr 30, 2020
1 parent e10e292 commit e7ff8cf
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,6 @@ function socketCloseListener() {

// NOTE: It's important to get parser here, because it could be freed by
// the `socketOnData`.
const parser = socket.parser;
const res = req.res;
if (res) {
// Socket closed before we emitted 'end' below.
Expand Down Expand Up @@ -443,10 +442,7 @@ function socketCloseListener() {
if (req.outputData)
req.outputData.length = 0;

if (parser) {
parser.finish();
freeParser(parser, req, socket);
}
freeSocket(req, socket);
}

function socketErrorListener(err) {
Expand All @@ -461,11 +457,7 @@ function socketErrorListener(err) {
req.emit('error', err);
}

const parser = socket.parser;
if (parser) {
parser.finish();
freeParser(parser, req, socket);
}
freeSocket(req, socket);

// Ensure that no further data will come out of the socket
socket.removeListener('data', socketOnData);
Expand All @@ -476,18 +468,14 @@ function socketErrorListener(err) {
function socketOnEnd() {
const socket = this;
const req = this._httpMessage;
const parser = this.parser;

if (!req.res && !req.socket._hadError) {
// If we don't have a response then we know that the socket
// ended prematurely and we need to emit an error on the request.
req.socket._hadError = true;
req.emit('error', connResetException('socket hang up'));
}
if (parser) {
parser.finish();
freeParser(parser, req, socket);
}
freeSocket(req, this);
socket.destroy();
}

Expand Down Expand Up @@ -519,8 +507,7 @@ function socketOnData(d) {
if (req.timeoutCb)
socket.removeListener('timeout', req.timeoutCb);

parser.finish();
freeParser(parser, req, socket);
freeSocket(req, this);

const bodyHead = d.slice(bytesParsed, d.length);

Expand Down Expand Up @@ -712,10 +699,19 @@ function emitFreeNT(req) {
}

if (req.socket) {
freeSocket(req, req.socket);
req.socket.emit('free');
}
}

function freeSocket(req, socket) {
const parser = socket.parser;
if (parser) {
parser.finish();
freeParser(parser, req, socket);
}
}

function tickOnSocket(req, socket) {
const parser = parsers.alloc();
req.socket = socket;
Expand Down

0 comments on commit e7ff8cf

Please sign in to comment.