From 1b61fe40fdb84e872af8312c5092836efadc04f6 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Mon, 9 Jan 2012 15:49:51 -0700
Subject: [PATCH] explicitly call .removeAllListeners() during http forwarding
 to eliminate memory leak.  closes #839 (with extreme prejudice)

---
 lib/http_forward.js | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/lib/http_forward.js b/lib/http_forward.js
index 46b976815..16b757703 100644
--- a/lib/http_forward.js
+++ b/lib/http_forward.js
@@ -6,6 +6,14 @@ logger = require('./logging.js').logger,
 querystring = require('querystring');
 
 module.exports = function(dest, req, res, cb) {
+  function cleanupReq() {
+    if (preq) {
+      preq.removeAllListeners();
+      preq.destroy();
+      preq = undefined;
+    }
+  }
+
   var u = url.parse(dest.toString());
 
   var m = u.protocol === 'http:' ? http : https;
@@ -32,9 +40,14 @@ module.exports = function(dest, req, res, cb) {
       res.write(chunk);
     }).on('end', function() {
       res.end();
+      pres.removeAllListeners();
+      pres.destroy();
+      pres = undefined;
+      cleanupReq();
       cb();
     });
   }).on('error', function(e) {
+    cleanupReq();
     cb(e);
   });
 
-- 
GitLab