From 9c48cc93331fee9ba0a966dba2c7d17f0e690817 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 23 Nov 2016 17:59:05 -0800 Subject: [PATCH] pool: fix destroyed race condition. --- lib/net/peer.js | 8 +++++++- lib/net/pool.js | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/net/peer.js b/lib/net/peer.js index 2dee24e1..44cc583e 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -126,7 +126,7 @@ function Peer(pool, addr, socket) { this.banScore = 0; this.pingTimeout = null; - this.pingInterval = 120000; + this.pingInterval = 30000; this.requestTimeout = 10000; this.requestMap = {}; @@ -658,6 +658,9 @@ Peer.prototype.sendVersion = function sendVersion() { */ Peer.prototype.sendPing = function sendPing() { + if (this.maybeStall()) + return Promise.resolve(); + if (!this.version) return Promise.resolve(); @@ -1018,6 +1021,9 @@ Peer.prototype.getData = function getData(items) { Peer.prototype._onPacket = co(function* onPacket(packet) { var unlock; + if (this.destroyed) + throw new Error('Destroyed peer sent a packet.'); + switch (packet.type) { case packetTypes.VERSION: case packetTypes.CMPCTBLOCK: diff --git a/lib/net/pool.js b/lib/net/pool.js index 03f77bd3..028033a8 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1562,6 +1562,9 @@ Pool.prototype.getData = co(function* getData(peer, type, hash) { if (exists) return true; + if (peer.destroyed) + throw new Errror('Peer is already destroyed (getdata).'); + item = new LoadRequest(this, peer, type, hash); if (type === this.txType) { @@ -2359,7 +2362,7 @@ LoadRequest.prototype.start = function start() { */ LoadRequest.prototype.finish = function finish() { - if (this.pool.requestMap[this.hash]) { + if (this.pool.requestMap[this.hash] === this) { delete this.pool.requestMap[this.hash]; if (this.active) { this.active = false;