From de36d64d4a07fbe05d9596bd83bf332ee3fddaad Mon Sep 17 00:00:00 2001 From: Mokhtar Naamani Date: Thu, 26 Jan 2017 18:19:55 +0200 Subject: [PATCH 1/3] pool: graceful disconnect --- lib/net/pool.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/net/pool.js b/lib/net/pool.js index e6bcb178..d918d5a1 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -354,6 +354,8 @@ Pool.prototype._disconnect = co(function* disconnect() { this.peers.destroy(); + this.nonces = new NonceList(); + this.requestMap.reset(); if (this.pendingFilter != null) { @@ -3727,8 +3729,15 @@ PeerList.prototype.destroy = function destroy() { for (peer = this.list.head; peer; peer = next) { next = peer.next; + + // stop processing peer events + peer.onPacket = null; + peer.removeAllListeners() + peer.destroy(); } + + this.list = new List(); }; /** From 06d25a9007cc714089c43d3875fe2a3084e53b68 Mon Sep 17 00:00:00 2001 From: Mokhtar Naamani Date: Fri, 27 Jan 2017 09:56:58 +0200 Subject: [PATCH 2/3] pool: graceful disconnect - simpler approach --- lib/net/pool.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/lib/net/pool.js b/lib/net/pool.js index d918d5a1..e0c6113d 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -352,9 +352,10 @@ Pool.prototype._disconnect = co(function* disconnect() { item.resolve(); } - this.peers.destroy(); + // prevent peer list from trying to refill + this.disconnecting = true; - this.nonces = new NonceList(); + this.peers.destroy(); this.requestMap.reset(); @@ -376,6 +377,7 @@ Pool.prototype._disconnect = co(function* disconnect() { yield this.unlisten(); + this.disconnecting = false; this.syncing = false; this.connected = false; }); @@ -1154,7 +1156,8 @@ Pool.prototype.handleClose = co(function* handleClose(peer, connected) { if (!outbound) return; - this.refill(); + if(!this.disconnecting) + this.refill(); }); /** @@ -3722,22 +3725,10 @@ PeerList.prototype.has = function has(hostname) { PeerList.prototype.destroy = function destroy() { var peer, next; - this.map = {}; - this.load = null; - this.inbound = 0; - this.outbound = 0; - for (peer = this.list.head; peer; peer = next) { next = peer.next; - - // stop processing peer events - peer.onPacket = null; - peer.removeAllListeners() - peer.destroy(); } - - this.list = new List(); }; /** From e651338ff37055eca87fa17077483f26714bde9b Mon Sep 17 00:00:00 2001 From: Mokhtar Naamani Date: Fri, 27 Jan 2017 10:01:35 +0200 Subject: [PATCH 3/3] pool: graceful disconnect - minor fix --- lib/net/pool.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/net/pool.js b/lib/net/pool.js index e0c6113d..77d0885d 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1156,8 +1156,10 @@ Pool.prototype.handleClose = co(function* handleClose(peer, connected) { if (!outbound) return; - if(!this.disconnecting) - this.refill(); + if(this.disconnecting) + return; + + this.refill(); }); /**