From 4410d9b9e99b12dd81c68717590540755f3845a9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 19 Sep 2014 16:45:46 -0700 Subject: [PATCH] fix shutdown. sigint/sighup handlers. --- example/index.js | 13 +++++++++---- lib/bitcoind.js | 27 ++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/example/index.js b/example/index.js index 29ed2dd6..58563dbc 100755 --- a/example/index.js +++ b/example/index.js @@ -12,10 +12,15 @@ bitcoind.start(function(err) { bitcoind.on('open', function(status) { setTimeout(function() { var genesis = '0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'; - return bitcoind.getBlock(genesis, function(err, block) { - if (err) return console.log(err.message); - print(block); - }); + (function next(hash) { + return bitcoind.getBlock(hash, function(err, block) { + if (err) return console.log(err.message); + print(block); + if (process.argv[2] === '-r' && block.nextblockhash) { + setTimeout(next.bind(null, block.nextblockhash), 200); + } + }); + })(genesis); }, 1000); console.log('bitcoind: status="%s"', status); }); diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 3afdfa37..2180c183 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -36,8 +36,29 @@ Bitcoin.prototype.start = function(callback) { var errorCaught = none; this.log_pipe = bitcoindjs.start(function(err, status) { - process.on('SIGINT', self.stop.bind(self)); - process.on('SIGHUP', self.stop.bind(self)); + process.on('SIGINT', function() { + if (process.listeners('SIGINT').length > 1) { + return; + } + if (!self._shutdown) { + process.exit(0); + } else { + self.stop(); + exitCaught = 0; + } + }); + + process.on('SIGHUP', function() { + if (process.listeners('SIGHUP').length > 1) { + return; + } + if (!self._shutdown) { + process.exit(0); + } else { + self.stop(); + exitCaught = 0; + } + }); var exit = process.exit; self._exit = function() { @@ -94,7 +115,7 @@ Bitcoin.prototype.start = function(callback) { delete self._shutdown; if (exitCaught !== none) { - return self._exit(0); + return self._exit(exitCaught); } if (errorCaught !== none) {