Merge pull request #208 from feross/fix-205

Fix socket.infoHashes access error
This commit is contained in:
Feross Aboukhadijeh 2017-02-28 15:31:05 -08:00 committed by GitHub
commit 7652e7465c
2 changed files with 5 additions and 11 deletions

View File

@ -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
})
}
@ -102,7 +94,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)
}

View File

@ -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',
@ -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`'))
@ -587,6 +588,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) {