From 587602d080cc643a5350462ce7581621df335223 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 20 Apr 2016 13:03:18 -0400 Subject: [PATCH] bitcoind: stop failsafe timeout --- lib/services/bitcoind.js | 24 ++++++++++++++++++------ test/services/bitcoind.unit.js | 13 +++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index d94e49ea..96a17344 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -49,6 +49,7 @@ function Bitcoin(options) { // limits this.maxAddressesQuery = options.maxAddressesQuery || Bitcoin.DEFAULT_MAX_ADDRESSES_QUERY; + this.shutdownTimeout = options.shutdownTimeout || Bitcoin.DEFAULT_SHUTDOWN_TIMEOUT; // try all interval this.tryAllInterval = options.tryAllInterval || Bitcoin.DEFAULT_TRY_ALL_INTERVAL; @@ -61,6 +62,7 @@ util.inherits(Bitcoin, Service); Bitcoin.dependencies = []; +Bitcoin.DEFAULT_SHUTDOWN_TIMEOUT = 15000; Bitcoin.DEFAULT_MAX_ADDRESSES_QUERY = 10000; Bitcoin.DEFAULT_TRY_ALL_INTERVAL = 1000; Bitcoin.DEFAULT_REINDEX_INTERVAL = 10000; @@ -1570,16 +1572,26 @@ Bitcoin.prototype.generateBlock = function(num, callback) { */ Bitcoin.prototype.stop = function(callback) { if (this.spawn && this.spawn.process) { + var exited = false; this.spawn.process.once('exit', function(code) { - if (code !== 0) { - var error = new Error('bitcoind spawned process exited with status code: ' + code); - error.code = code; - return callback(error); - } else { - return callback(); + if (!exited) { + exited = true; + if (code !== 0) { + var error = new Error('bitcoind spawned process exited with status code: ' + code); + error.code = code; + return callback(error); + } else { + return callback(); + } } }); this.spawn.process.kill('SIGINT'); + setTimeout(function() { + if (!exited) { + exited = true; + return callback(new Error('bitcoind process did not exit')); + } + }, this.shutdownTimeout).unref(); } else { callback(); } diff --git a/test/services/bitcoind.unit.js b/test/services/bitcoind.unit.js index 39df9972..88cea586 100644 --- a/test/services/bitcoind.unit.js +++ b/test/services/bitcoind.unit.js @@ -3032,6 +3032,19 @@ describe('Bitcoin Service', function() { bitcoind.spawn.process.kill.args[0][0].should.equal('SIGINT'); bitcoind.spawn.process.emit('exit', 1); }); + it('will stop after timeout', function(done) { + var bitcoind = new BitcoinService(baseConfig); + bitcoind.shutdownTimeout = 300; + bitcoind.spawn = {}; + bitcoind.spawn.process = new EventEmitter(); + bitcoind.spawn.process.kill = sinon.stub(); + bitcoind.stop(function(err) { + err.should.be.instanceof(Error); + done(); + }); + bitcoind.spawn.process.kill.callCount.should.equal(1); + bitcoind.spawn.process.kill.args[0][0].should.equal('SIGINT'); + }); }); });