diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 5b57a1bc..5982864f 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -90,6 +90,8 @@ Peer.prototype._init = function init() { }; Peer.prototype.broadcast = function broadcast(items) { + if (this.destroyed) + return; if (!Array.isArray(items)) items = [ items ]; @@ -185,6 +187,9 @@ Peer.prototype._error = function error(err) { }; Peer.prototype._req = function _req(cmd, cb) { + if (this.destroyed) + return cb(new Error('Destroyed, sorry')); + var self = this; var entry = { cmd: cmd, @@ -216,7 +221,8 @@ Peer.prototype._res = function _res(cmd, payload) { assert(!entry.cmd); // Restart timer - entry.timer = setTimeout(entry.ontimeout, this._request.timeout); + if (!this.destroyed) + entry.timer = setTimeout(entry.ontimeout, this._request.timeout); return true; } else if (res !== this._request.skip) { this._request.queue.shift(); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 101b15b8..322b0a98 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -130,7 +130,6 @@ Pool.prototype._addLoader = function _addLoader() { // Chain is full and up-to-date if (self.chain.isFull()) { clearTimeout(timer); - self._removePeer(peer); self.emit('full'); self.block.lastHash = null; return; @@ -453,6 +452,8 @@ Pool.prototype._response = function _response(entity) { }; Pool.prototype._scheduleRequests = function _scheduleRequests() { + if (this.destroyed) + return; if (this.request.active > this.parallel / 2) return; @@ -605,12 +606,19 @@ Pool.prototype.destroy = function destroy() { this.request.queue.slice().forEach(function(item) { item.finish(null); }); + this.tx.list.forEach(function(tx) { + clearTimeout(tx.timer); + tx.timer = null; + }); this.peers.pending.slice().forEach(function(peer) { peer.destroy(); }); this.peers.block.slice().forEach(function(peer) { peer.destroy(); }); + if (this.load.timer) + clearTimeout(this.load.timer); + this.load.timer = null; }; Pool.prototype.toJSON = function toJSON() {