From b50d22d7ab64f8348cbd1802dde6bf7fc50d20a4 Mon Sep 17 00:00:00 2001 From: Brian Di Palma Date: Mon, 23 Apr 2018 10:33:56 +0100 Subject: [PATCH] fix(server): Remove Socket.IO listeners Not removing Socket.IO listeners can cause memory issues in long running processes that spawn many Karma instance. Fixes #2980 --- lib/server.js | 4 ++++ test/unit/server.spec.js | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/server.js b/lib/server.js index dd015aca2..c9ddbe71e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -425,6 +425,10 @@ Server.prototype._start = function (config, launcher, preprocess, fileList, } self.emitAsync('exit').then(function () { + // Remove Socket.IO listeners. `connection` callback closes over `Server` + // instance so it leaks Plugin state e.g. Webpack compilations. + socketServer.sockets.removeAllListeners() + socketServer.close() // don't wait forever on webServer.close() because // pending client connections prevent it from closing. var closeTimeout = setTimeout(removeAllListeners, webServerCloseTimeout) diff --git a/test/unit/server.spec.js b/test/unit/server.spec.js index 2274355db..a16a8ad83 100644 --- a/test/unit/server.spec.js +++ b/test/unit/server.spec.js @@ -65,13 +65,15 @@ describe('server', () => { } mockSocketServer = { + close: () => {}, flashPolicyServer: { close: () => {} }, sockets: { sockets: {}, on: () => {}, - emit: () => {} + emit: () => {}, + removeAllListeners: () => {} } }