Skip to content

Commit ca1d12c

Browse files
committed
[fix] Memory leak hunting.
1 parent e1c41d0 commit ca1d12c

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/node-http-proxy.js

+8
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ exports.buffer = function (obj) {
174174
obj.removeListener('data', onData);
175175
obj.removeListener('end', onEnd);
176176
},
177+
destroy: function () {
178+
this.end();
179+
this.resume = function () {
180+
console.error("Cannot resume buffer after destroying it.");
181+
};
182+
183+
onData = onEnd = events = obj = null;
184+
},
177185
resume: function () {
178186
this.end();
179187
for (var i = 0, len = events.length; i < len; ++i) {

lib/node-http-proxy/http-proxy.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,13 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
279279
}
280280
});
281281

282+
//
282283
// If we have been passed buffered data, resume it.
283-
if (buffer && !errState) {
284-
buffer.resume();
284+
//
285+
if (buffer) {
286+
return !errState
287+
? buffer.resume()
288+
: buffer.destroy();
285289
}
286290
};
287291

@@ -419,6 +423,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
419423
reverseProxy.incoming.socket.write(data);
420424
}
421425
catch (ex) {
426+
detach();
422427
reverseProxy.incoming.socket.end();
423428
proxySocket.end();
424429
}
@@ -429,12 +434,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
429434
// Any outgoing data on this Websocket from the proxy target
430435
// will be written to the `proxySocket` socket.
431436
//
432-
reverseProxy.incoming.socket.on('data', listeners.onOutgoing = function(data) {
437+
reverseProxy.incoming.socket.on('data', listeners.onOutgoing = function (data) {
433438
try {
434439
self.emit('websocket:incoming', reverseProxy, reverseProxy.incoming, head, data);
435440
proxySocket.write(data);
436441
}
437442
catch (ex) {
443+
detach();
438444
proxySocket.end();
439445
socket.end();
440446
}
@@ -625,7 +631,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
625631
//
626632
// If we have been passed buffered data, resume it.
627633
//
628-
if (buffer && !errState) {
629-
buffer.resume();
634+
if (buffer) {
635+
return !errState
636+
? buffer.resume()
637+
: buffer.destroy();
630638
}
631639
};

0 commit comments

Comments
 (0)