From b0ab45f3f7734b7283d12d43ff6fef0165951937 Mon Sep 17 00:00:00 2001 From: Patrick Nagurny Date: Fri, 21 Aug 2015 15:39:46 -0400 Subject: [PATCH] handle signals and stop correctly --- bin/start.js | 24 +++++------------------- lib/node.js | 12 +++++++----- src/libbitcoind.cc | 6 ++++-- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/bin/start.js b/bin/start.js index 72f8dd04..5579e310 100644 --- a/bin/start.js +++ b/bin/start.js @@ -146,8 +146,6 @@ node.on('stopping', function() { clearInterval(interval); }); -process.stdin.resume();//so the program will not close instantly - function exitHandler(options, err) { if (err) { log.error('uncaught exception:', err); @@ -157,7 +155,6 @@ function exitHandler(options, err) { process.exit(-1); } if (options.sigint) { - log.info('Stopping Services'); node.stop(function(err) { if(err) { log.error('Failed to stop services: ' + err); @@ -170,21 +167,10 @@ function exitHandler(options, err) { } } -//catches ctrl+c event -process.on('SIGINT', exitHandler.bind(null, {sigint:true})); - -/*setTimeout(function() { - log.info('Stopping Services'); - node.stop(function(err) { - if(err) { - log.error('Failed to stop services: ' + err); - return process.exit(1); - } - - log.info('Halted'); - process.exit(0); - }); -}, 10000);*/ - //catches uncaught exceptions + + process.on('uncaughtException', exitHandler.bind(null, {exit:true})); +//catches ctrl+c event +process.on('exit', exitHandler.bind(null, {exit: true})); +process.on('SIGINT', exitHandler.bind(null, {sigint:true})); diff --git a/lib/node.js b/lib/node.js index 30466269..6495dcfd 100644 --- a/lib/node.js +++ b/lib/node.js @@ -400,10 +400,12 @@ Node.prototype._initialize = function() { // Notify that there is a new tip this.bitcoind.on('tip', function(height) { - var percentage = self.bitcoind.syncPercentage(); - log.info('Bitcoin Core Daemon New Height:', height, 'Percentage:', percentage); - self.bitcoindHeight = height; - self._syncBitcoind(); + if(!self.stopping) { + var percentage = self.bitcoind.syncPercentage(); + log.info('Bitcoin Core Daemon New Height:', height, 'Percentage:', percentage); + self.bitcoindHeight = height; + self._syncBitcoind(); + } }); this.bitcoind.on('error', function(err) { @@ -478,7 +480,7 @@ Node.prototype.start = function(callback) { }; Node.prototype.stop = function(callback) { - log.info('Stopping Bitcore Node'); + log.info('Beginning shutdown'); var self = this; var services = this.getServiceOrder().reverse(); diff --git a/src/libbitcoind.cc b/src/libbitcoind.cc index 5c3c0add..35b72c6e 100644 --- a/src/libbitcoind.cc +++ b/src/libbitcoind.cc @@ -104,6 +104,8 @@ static bool g_testnet = false; static bool g_regtest = false; static bool g_txindex = false; +static boost::thread_group threadGroup; + /** * Private Structs * Used for async functions and necessary linked lists at points. @@ -685,7 +687,6 @@ start_node(void) { static void start_node_thread(void) { - boost::thread_group threadGroup; CScheduler scheduler; // Workaround for AppInit2() arg parsing. Not ideal, but it works. @@ -798,7 +799,6 @@ start_node_thread(void) { */ NAN_METHOD(StopBitcoind) { - fprintf(stderr, "Stopping Bitcoind please wait!\n"); Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); @@ -840,7 +840,9 @@ NAN_METHOD(StopBitcoind) { static void async_stop_node(uv_work_t *req) { async_node_data *data = static_cast(req->data); + StartShutdown(); + while(!shutdown_complete) { usleep(1E6); }