diff --git a/lib/server.js b/lib/server.js index 715818aab..d4bd6b452 100644 --- a/lib/server.js +++ b/lib/server.js @@ -190,6 +190,7 @@ var start = function(injector, config, launcher, globalEmitter, preprocess, file }); } + var webServerCloseTimeout = 3000; var disconnectBrowsers = function(code) { // Slightly hacky way of removing disconnect listeners // to suppress "browser disconnect" warnings @@ -199,16 +200,28 @@ var start = function(injector, config, launcher, globalEmitter, preprocess, file sockets[key].removeAllListeners('disconnect'); }); + var removeAllListenersDone = false; + var removeAllListeners = function() { + // make sure we don't execute cleanup twice + if (removeAllListenersDone) { + return; + } + removeAllListenersDone = true; + webServer.removeAllListeners(); + processWrapper.removeAllListeners(); + done(code || 0); + }; + globalEmitter.emitAsync('exit').then(function() { - webServer.close(function() { - webServer.removeAllListeners(); - processWrapper.removeAllListeners(); - done(code || 0); - }); + // shutdown the server... + webServer.close(removeAllListeners); + + // ...but don't wait forever on webServer.close() because + // pending client connections prevent it from closing. + setTimeout(removeAllListeners, webServerCloseTimeout); }); }; - try { processWrapper.on('SIGINT', disconnectBrowsers); processWrapper.on('SIGTERM', disconnectBrowsers);