Skip to content

Commit

Permalink
Memory leak hunting.
Browse files Browse the repository at this point in the history
Detach listeners and clean up buffer on error
  • Loading branch information
isaacs authored and indexzero committed Aug 31, 2011
1 parent 7beead5 commit f4fcf93
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ HttpProxy.prototype.buffer = function (obj) {
obj.removeListener('data', onData);
obj.removeListener('end', onEnd);
},
destroy: function () {
this.end();
this.resume = function () {
console.error("Cannot resume buffer after destroying it.");
};
onData = onEnd = events = obj = null;
},
resume: function () {
this.end();
for (var i = 0, len = events.length; i < len; ++i) {
Expand Down Expand Up @@ -586,8 +593,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
});

// If we have been passed buffered data, resume it.
if (options.buffer && !errState) {
options.buffer.resume();
if (options.buffer) {
if (!errState) {
options.buffer.resume();
} else {
options.buffer.destroy();
}
}
};

Expand Down Expand Up @@ -733,6 +744,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
reverseProxy.incoming.socket.write(data);
}
catch (e) {
detach();
reverseProxy.incoming.socket.end();
proxySocket.end();
}
Expand All @@ -749,6 +761,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
proxySocket.write(data);
}
catch (e) {
detach();
proxySocket.end();
socket.end();
}
Expand Down Expand Up @@ -833,7 +846,6 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
if (self.emit('webSocketProxyError', req, socket, head)) {
return;
}

socket.end();
}

Expand Down Expand Up @@ -932,10 +944,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
proxyError(ex);
}

//
// If we have been passed buffered data, resume it.
//
if (options.buffer && !errState) {
options.buffer.resume();
if (options.buffer) {
if (!errState) {
options.buffer.resume();
} else {
options.buffer.destroy();
}
}
};

0 comments on commit f4fcf93

Please sign in to comment.