From 118fc21b62b8f03333831640c60d508b79790bd5 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Wed, 9 Feb 2022 19:00:08 -0600 Subject: [PATCH] fix(cosmic-swingset): straighten out shutdown signals and exit code --- packages/cosmic-swingset/src/shutdown.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/cosmic-swingset/src/shutdown.js b/packages/cosmic-swingset/src/shutdown.js index 6b8196766d8..2b7d98fa909 100644 --- a/packages/cosmic-swingset/src/shutdown.js +++ b/packages/cosmic-swingset/src/shutdown.js @@ -4,7 +4,7 @@ import anylogger from 'anylogger'; const console = anylogger('shutdown'); -export const makeFreshShutdown = () => { +export const makeFreshShutdown = (verbose = true) => { const shutdownThunks = new Set(); let shuttingDown = false; @@ -12,25 +12,27 @@ export const makeFreshShutdown = () => { const shutdown = code => { const sig = typeof code === 'string' && code.startsWith('SIG'); if (sig) { - process.exitCode = 99; + process.exitCode = 98; } if (shuttingDown) { - process.exit(); + return; } shuttingDown = true; - console.error('Shutting down cleanly...'); + // Allow an explicit exit to terminate the process. + process.off('beforeExit', shutdown); + verbose && console.error(`Shutting down cleanly...`); const shutdowners = [...shutdownThunks.keys()]; shutdownThunks.clear(); Promise.allSettled([...shutdowners].map(t => Promise.resolve().then(t))) .then(statuses => { for (const status of statuses) { if (status.status === 'rejected') { - console.warn(status.reason); + verbose && console.warn(status.reason); } } - console.log('Process terminated!'); + verbose && console.warn('Process terminated!'); }) - .catch(error => console.warn('Error shutting down', error)) + .catch(error => verbose && console.warn('Error shutting down', error)) .finally(() => { process.exit(); }); @@ -39,7 +41,6 @@ export const makeFreshShutdown = () => { // gracefully shut down the thunks on process exit process.on('SIGTERM', shutdown); process.on('SIGINT', shutdown); - process.on('SIGQUIT', shutdown); process.on('beforeExit', shutdown); process.on('uncaughtException', e => { console.error(e); @@ -57,9 +58,11 @@ export const makeFreshShutdown = () => { }; let cachedShutdown = null; -export const makeCachedShutdown = () => { +export const makeCachedShutdown = (...args) => { + // It's possible our caller has specified different arguments. + // Since they control verbosity only, first-one-wins is acceptable. if (!cachedShutdown) { - cachedShutdown = makeFreshShutdown(); + cachedShutdown = makeFreshShutdown(...args); } return cachedShutdown; };