Commit f4fcf934 authored by isaacs's avatar isaacs Committed by indexzero

Memory leak hunting.

Detach listeners and clean up buffer on error
parent 7beead54
...@@ -344,6 +344,13 @@ HttpProxy.prototype.buffer = function (obj) { ...@@ -344,6 +344,13 @@ HttpProxy.prototype.buffer = function (obj) {
obj.removeListener('data', onData); obj.removeListener('data', onData);
obj.removeListener('end', onEnd); 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 () { resume: function () {
this.end(); this.end();
for (var i = 0, len = events.length; i < len; ++i) { for (var i = 0, len = events.length; i < len; ++i) {
...@@ -586,8 +593,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) { ...@@ -586,8 +593,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
}); });
// If we have been passed buffered data, resume it. // If we have been passed buffered data, resume it.
if (options.buffer && !errState) { if (options.buffer) {
options.buffer.resume(); if (!errState) {
options.buffer.resume();
} else {
options.buffer.destroy();
}
} }
}; };
...@@ -733,6 +744,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -733,6 +744,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
reverseProxy.incoming.socket.write(data); reverseProxy.incoming.socket.write(data);
} }
catch (e) { catch (e) {
detach();
reverseProxy.incoming.socket.end(); reverseProxy.incoming.socket.end();
proxySocket.end(); proxySocket.end();
} }
...@@ -749,6 +761,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -749,6 +761,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
proxySocket.write(data); proxySocket.write(data);
} }
catch (e) { catch (e) {
detach();
proxySocket.end(); proxySocket.end();
socket.end(); socket.end();
} }
...@@ -833,7 +846,6 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -833,7 +846,6 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
if (self.emit('webSocketProxyError', req, socket, head)) { if (self.emit('webSocketProxyError', req, socket, head)) {
return; return;
} }
socket.end(); socket.end();
} }
...@@ -932,10 +944,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options ...@@ -932,10 +944,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
proxyError(ex); proxyError(ex);
} }
//
// If we have been passed buffered data, resume it. // If we have been passed buffered data, resume it.
// if (options.buffer) {
if (options.buffer && !errState) { if (!errState) {
options.buffer.resume(); options.buffer.resume();
} else {
options.buffer.destroy();
}
} }
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment