Skip to content

Commit

Permalink
fix(web-server): implement a timeout on webServer.close()
Browse files Browse the repository at this point in the history
Closes #952
  • Loading branch information
sylvain-hamel committed Mar 25, 2014
1 parent 65b5f1b commit fe3dca7
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit fe3dca7

Please sign in to comment.