From 7f7ef196f44563516ee7d69d243bc9108d98f321 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 17 Sep 2014 14:21:28 -0700 Subject: [PATCH] fix shutdown loop. --- lib/bitcoind.js | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 858aa7dc..023a1121 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -30,6 +30,10 @@ Bitcoin.prototype.__proto__ = EventEmitter.prototype; Bitcoin.prototype.start = function(callback) { var self = this; + var none = {}; + var exitCaught = none; + var errorCaught = none; + this.log_pipe = bitcoindjs.start(function(err, status) { if (callback) { callback(err); @@ -42,12 +46,29 @@ Bitcoin.prototype.start = function(callback) { self.emit('open', status); } - function stop() { - return self.stop(); - } + process.on('SIGINT', self.stop.bind(self)); + process.on('SIGHUP', self.stop.bind(self)); - process.on('SIGINT', stop); - process.on('SIGHUP', stop); + var exit = process.exit; + self._exit = function() { + return exit.apply(process, arguments); + }; + + process.exit = function(code) { + exitCaught = code || 0; + if (!self._shutdown) { + return self._exit(exitCaught); + } + self.stop(); + }; + + process.on('uncaughtException', function(err) { + errorCaught = err; + if (!self._shutdown) { + return self._exit(exitCaught !== none ? exitCaught : 1); + } + self.stop(); + }); }); // bitcoind's boost threads aren't in the thread pool @@ -57,10 +78,23 @@ Bitcoin.prototype.start = function(callback) { self._stoppingSaid = true; self.log('shutting down...'); } + if (bitcoindjs.stopped()) { self.log('shut down.'); + clearInterval(self._shutdown); delete self._shutdown; + + if (exitCaught !== none) { + return self._exit(0); + } + + if (errorCaught !== none) { + if (errorCaught.stack) { + console.error(errorCaught.stack); + } + return self._exit(0); + } } }, 1000);