From 42431d6e1e0e4ea40c31738c81332fa806486d21 Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Tue, 10 Feb 2015 08:28:20 +0700 Subject: [PATCH] Fix #1080 - domain memory leak. Domains are created per request rather than creating a single domain for all requests. This kills the memory leak, as a single domain would keep a reference to every single req and res, causing rapid heap growth. --- server/middleware/context.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/middleware/context.js b/server/middleware/context.js index 23912db4..3890aa7a 100644 --- a/server/middleware/context.js +++ b/server/middleware/context.js @@ -46,12 +46,6 @@ function context(options) { var enableHttpContext = options.enableHttpContext || false; var ns = createContext(scope); - var currentDomain = process.domain = domain.create(); - currentDomain.oldBind = currentDomain.bind; - currentDomain.bind = function(callback, context) { - return currentDomain.oldBind(ns.bind(callback, context), context); - }; - // Return the middleware return function contextHandler(req, res, next) { if (req.loopbackContext) { @@ -62,6 +56,12 @@ function context(options) { ns.bindEmitter(req); ns.bindEmitter(res); + var currentDomain = domain.create(); + currentDomain.oldBind = currentDomain.bind; + currentDomain.bind = function(callback, context) { + return currentDomain.oldBind(ns.bind(callback, context), context); + }; + currentDomain.add(req); currentDomain.add(res);