Skip to content

Commit f4fcf93

Browse files
isaacsindexzero
authored andcommitted
Memory leak hunting.
Detach listeners and clean up buffer on error
1 parent 7beead5 commit f4fcf93

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lib/node-http-proxy.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ HttpProxy.prototype.buffer = function (obj) {
344344
obj.removeListener('data', onData);
345345
obj.removeListener('end', onEnd);
346346
},
347+
destroy: function () {
348+
this.end();
349+
this.resume = function () {
350+
console.error("Cannot resume buffer after destroying it.");
351+
};
352+
onData = onEnd = events = obj = null;
353+
},
347354
resume: function () {
348355
this.end();
349356
for (var i = 0, len = events.length; i < len; ++i) {
@@ -586,8 +593,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
586593
});
587594

588595
// If we have been passed buffered data, resume it.
589-
if (options.buffer && !errState) {
590-
options.buffer.resume();
596+
if (options.buffer) {
597+
if (!errState) {
598+
options.buffer.resume();
599+
} else {
600+
options.buffer.destroy();
601+
}
591602
}
592603
};
593604

@@ -733,6 +744,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
733744
reverseProxy.incoming.socket.write(data);
734745
}
735746
catch (e) {
747+
detach();
736748
reverseProxy.incoming.socket.end();
737749
proxySocket.end();
738750
}
@@ -749,6 +761,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
749761
proxySocket.write(data);
750762
}
751763
catch (e) {
764+
detach();
752765
proxySocket.end();
753766
socket.end();
754767
}
@@ -833,7 +846,6 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
833846
if (self.emit('webSocketProxyError', req, socket, head)) {
834847
return;
835848
}
836-
837849
socket.end();
838850
}
839851

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

935-
//
936947
// If we have been passed buffered data, resume it.
937-
//
938-
if (options.buffer && !errState) {
939-
options.buffer.resume();
948+
if (options.buffer) {
949+
if (!errState) {
950+
options.buffer.resume();
951+
} else {
952+
options.buffer.destroy();
953+
}
940954
}
941955
};

0 commit comments

Comments
 (0)