Skip to content

Commit 7652e74

Browse files
authored
Merge pull request webtorrent#208 from feross/fix-205
Fix socket.infoHashes access error
2 parents 40707a1 + ccb50a7 commit 7652e74

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

lib/server/swarm.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,7 @@ function Swarm (infoHash, server) {
2929
peer_id: peer.peerId
3030
}
3131
self._onAnnounceStopped(params, peer, peer.peerId)
32-
33-
// When a websocket peer is evicted, and it's not in any other swarms, close
34-
// the websocket to conserve server resources.
35-
if (peer.socket && peer.socket.infoHashes.length === 0) {
36-
try {
37-
peer.socket.close()
38-
peer.socket = null
39-
} catch (err) {}
40-
}
32+
peer.socket = null
4133
})
4234
}
4335

@@ -102,7 +94,7 @@ Swarm.prototype._onAnnounceStopped = function (params, peer, id) {
10294

10395
// If it's a websocket, remove this swarm's infohash from the list of active
10496
// swarms that this peer is participating in.
105-
if (peer.socket) {
97+
if (peer.socket && !peer.socket.destroyed) {
10698
var index = peer.socket.infoHashes.indexOf(this.infoHash)
10799
arrayRemove(peer.socket.infoHashes, index)
108100
}

server.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
491491
if (!socket.peerId) socket.peerId = params.peer_id // as hex
492492

493493
self._onRequest(params, function (err, response) {
494-
if (self.destroyed) return
494+
if (self.destroyed || socket.destroyed) return
495495
if (err) {
496496
socket.send(JSON.stringify({
497497
action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape',
@@ -545,6 +545,7 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
545545
debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id)
546546

547547
self.getSwarm(params.info_hash, function (err, swarm) {
548+
if (self.destroyed) return
548549
if (err) return self.emit('warning', err)
549550
if (!swarm) {
550551
return self.emit('warning', new Error('no swarm with that `info_hash`'))
@@ -587,6 +588,7 @@ Server.prototype._onWebSocketSend = function (socket, err) {
587588
Server.prototype._onWebSocketClose = function (socket) {
588589
var self = this
589590
debug('websocket close %s', socket.peerId)
591+
socket.destroyed = true
590592

591593
if (socket.peerId) {
592594
socket.infoHashes.slice(0).forEach(function (infoHash) {

0 commit comments

Comments
 (0)