diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 9c00f1bb..ca670dae 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -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.'; }; diff --git a/lib/workers/workerpool.js b/lib/workers/workerpool.js index 60bbd9a2..22373746 100644 --- a/lib/workers/workerpool.js +++ b/lib/workers/workerpool.js @@ -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);