From b2c2b8f0d0b1c42c13c43391007f06d64f7694af Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 21 Feb 2017 01:50:30 -0800 Subject: [PATCH 1/3] do not access socket.infoHashes if socket is destroyed --- lib/server/swarm.js | 2 +- server.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/server/swarm.js b/lib/server/swarm.js index 9054268..773caba 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -102,7 +102,7 @@ Swarm.prototype._onAnnounceStopped = function (params, peer, id) { // If it's a websocket, remove this swarm's infohash from the list of active // swarms that this peer is participating in. - if (peer.socket) { + if (peer.socket && !peer.socket.destroyed) { var index = peer.socket.infoHashes.indexOf(this.infoHash) arrayRemove(peer.socket.infoHashes, index) } diff --git a/server.js b/server.js index 4528f3d..9186c46 100644 --- a/server.js +++ b/server.js @@ -491,7 +491,7 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) { if (!socket.peerId) socket.peerId = params.peer_id // as hex self._onRequest(params, function (err, response) { - if (self.destroyed) return + if (self.destroyed || socket.destroyed) return if (err) { socket.send(JSON.stringify({ action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape', @@ -587,6 +587,7 @@ Server.prototype._onWebSocketSend = function (socket, err) { Server.prototype._onWebSocketClose = function (socket) { var self = this debug('websocket close %s', socket.peerId) + socket.destroyed = true if (socket.peerId) { socket.infoHashes.slice(0).forEach(function (infoHash) { From fe11c695cb4ca759839c94e9f9802c506bc28639 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 21 Feb 2017 01:50:48 -0800 Subject: [PATCH 2/3] do not close socket even when it's not in other swarms --- lib/server/swarm.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/server/swarm.js b/lib/server/swarm.js index 773caba..2c6f49a 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -29,15 +29,7 @@ function Swarm (infoHash, server) { peer_id: peer.peerId } self._onAnnounceStopped(params, peer, peer.peerId) - - // When a websocket peer is evicted, and it's not in any other swarms, close - // the websocket to conserve server resources. - if (peer.socket && peer.socket.infoHashes.length === 0) { - try { - peer.socket.close() - peer.socket = null - } catch (err) {} - } + peer.socket = null }) } From ccb50a7ee976dcce481d07bb129a489380d4c247 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 21 Feb 2017 01:51:08 -0800 Subject: [PATCH 3/3] add one missing self.destroyed check --- server.js | 1 + 1 file changed, 1 insertion(+) diff --git a/server.js b/server.js index 9186c46..8548786 100644 --- a/server.js +++ b/server.js @@ -545,6 +545,7 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) { debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id) self.getSwarm(params.info_hash, function (err, swarm) { + if (self.destroyed) return if (err) return self.emit('warning', err) if (!swarm) { return self.emit('warning', new Error('no swarm with that `info_hash`'))