workers: better signal handling.

This commit is contained in:
Christopher Jeffrey 2017-07-05 12:49:01 -07:00
parent 90f783fd05
commit 0250cf4296
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 26 additions and 22 deletions

View File

@ -202,17 +202,7 @@ RPC.prototype.stop = async function stop(args, help) {
if (help || args.length !== 0)
throw new RPCError(errs.MISC_ERROR, 'stop');
(async () => {
try {
await this.node.close();
} catch (e) {
if (process.exit)
process.exit(1);
return;
}
if (process.exit)
process.exit(0);
})();
this.node.close().catch(() => {});
return 'Stopping.';
};

View File

@ -121,7 +121,7 @@ WorkerPool.bound = false;
*/
WorkerPool.bindExit = function bindExit() {
let onSignal, onError;
let onSighup, onSigint, onSigterm, onError;
if (!HAS_CP)
return;
@ -131,13 +131,19 @@ WorkerPool.bindExit = function bindExit() {
WorkerPool.bound = true;
onSignal = () => {
WorkerPool.cleanup();
process.exit(0);
onSighup = () => {
process.exit(1 | 0x80);
};
onSigint = () => {
process.exit(2 | 0x80);
};
onSigterm = () => {
process.exit(15 | 0x80);
};
onError = (err) => {
WorkerPool.cleanup();
if (err && err.stack)
util.error(err.stack + '');
process.exit(1);
@ -147,20 +153,28 @@ WorkerPool.bindExit = function bindExit() {
WorkerPool.cleanup();
});
if (process.listeners('SIGINT').length === 0)
process.once('SIGINT', onSignal);
if (process.listenerCount('SIGHUP') === 0)
process.once('SIGHUP', onSighup);
if (process.listeners('SIGTERM').length === 0)
process.once('SIGTERM', onSignal);
if (process.listenerCount('SIGINT') === 0)
process.once('SIGINT', onSigint);
if (process.listeners('uncaughtException').length === 0)
if (process.listenerCount('SIGTERM') === 0)
process.once('SIGTERM', onSigterm);
if (process.listenerCount('uncaughtException') === 0)
process.once('uncaughtException', onError);
process.on('newListener', (name) => {
switch (name) {
case 'SIGHUP':
process.removeListener(name, onSighup);
break;
case 'SIGINT':
process.removeListener(name, onSigint);
break;
case 'SIGTERM':
process.removeListener(name, onSignal);
process.removeListener(name, onSigterm);
break;
case 'uncaughtException':
process.removeListener(name, onError);