From fe3dca781def0a5f813e598fe73eb97b3f55d223 Mon Sep 17 00:00:00 2001 From: sylvain-hamel Date: Fri, 21 Mar 2014 14:14:54 -0400 Subject: [PATCH] fix(web-server): implement a timeout on webServer.close() Closes #952 --- lib/server.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) 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);