fix shutdown loop.

This commit is contained in:
Christopher Jeffrey 2014-09-17 14:21:28 -07:00
parent 3aacd0a089
commit 7f7ef196f4

View File

@ -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);