Skip to content

Commit

Permalink
fix(server): Remove Socket.IO listeners
Browse files Browse the repository at this point in the history
Not removing Socket.IO listeners can cause memory issues
in long running processes that spawn many Karma instance.

Fixes karma-runner#2980
  • Loading branch information
briandipalma committed Apr 23, 2018
1 parent dae189d commit 10993bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion test/unit/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ describe('server', () => {
}

mockSocketServer = {
close: () => {},
flashPolicyServer: {
close: () => {}
},
sockets: {
sockets: {},
on: () => {},
emit: () => {}
emit: () => {},
removeAllListeners: () => {}
}
}

Expand Down

0 comments on commit 10993bd

Please sign in to comment.