From 9f66421ae7317f8091d9fc7763ff58fad416beda Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 27 Sep 2016 10:44:24 +0200 Subject: [PATCH] fix(bootstrap): C-c twice force stop the server --- src/index.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index f03fa29bf..381cb1a60 100644 --- a/src/index.js +++ b/src/index.js @@ -608,16 +608,24 @@ export default async function main (args) { await registerPlugins(xo) } + // Gracefully shutdown on signals. + // // TODO: implements a timeout? (or maybe it is the services launcher // responsibility?) - const shutdown = signal => { - debug('%s caught, closing…', signal) - xo.stop() - } + forEach([ 'SIGINT', 'SIGTERM' ], signal => { + let alreadyCalled = false - // Gracefully shutdown on signals. - process.on('SIGINT', () => shutdown('SIGINT')) - process.on('SIGTERM', () => shutdown('SIGTERM')) + process.on(signal, () => { + if (alreadyCalled) { + warn('forced exit') + process.exit(1) + } + alreadyCalled = true + + debug('%s caught, closing…', signal) + xo.stop() + }) + }) await eventToPromise(xo, 'stopped')