Skip to content

Commit 3f3db7d

Browse files
committed
client: socketPool should not be shared across clients
Caught this issue because of the new eviction tests. Essentially, this change moves the socketPool into the client instance instead of a reused variable at the module level. When a client sends stop (or is evicted) the server will close the websocket connection if that client is not in any other swarms (based on peerId). However, if we are using a single socket for multiple clients (as was the case before this commit), then other clients will have their sockets unintentionally closed by the server.
1 parent 806ce1d commit 3f3db7d

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

client.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ function Client (opts) {
6969
// See: https://github.com/feross/webtorrent-hybrid/issues/46
7070
self._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc
7171

72+
// Use a socket pool, so WebSocket tracker clients share WebSocket objects for
73+
// the same server. In practice, WebSockets are pretty slow to establish, so this
74+
// gives a nice performance boost, and saves browser resources.
75+
self._socketPool = {}
76+
7277
var announce = typeof opts.announce === 'string'
7378
? [ opts.announce ]
7479
: opts.announce == null ? [] : opts.announce

lib/client/websocket-tracker.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ var Socket = require('simple-websocket')
1010
var common = require('../common')
1111
var Tracker = require('./tracker')
1212

13-
// Use a socket pool, so tracker clients share WebSocket objects for the same server.
14-
// In practice, WebSockets are pretty slow to establish, so this gives a nice performance
15-
// boost, and saves browser resources.
16-
var socketPool = {}
17-
1813
var RECONNECT_MINIMUM = 15 * 1000
1914
var RECONNECT_MAXIMUM = 30 * 60 * 1000
2015
var RECONNECT_VARIANCE = 30 * 1000
@@ -129,15 +124,15 @@ WebSocketTracker.prototype.destroy = function (cb) {
129124
self._onSocketDataBound = null
130125
self._onSocketCloseBound = null
131126

132-
if (socketPool[self.announceUrl]) {
133-
socketPool[self.announceUrl].consumers -= 1
127+
if (self.client._socketPool[self.announceUrl]) {
128+
self.client._socketPool[self.announceUrl].consumers -= 1
134129
}
135130

136131
// Other instances are using the socket, so there's nothing left to do here
137-
if (socketPool[self.announceUrl].consumers > 0) return cb()
132+
if (self.client._socketPool[self.announceUrl].consumers > 0) return cb()
138133

139-
var socket = socketPool[self.announceUrl]
140-
delete socketPool[self.announceUrl]
134+
var socket = self.client._socketPool[self.announceUrl]
135+
delete self.client._socketPool[self.announceUrl]
141136
socket.on('error', noop) // ignore all future errors
142137
socket.once('close', cb)
143138

@@ -182,11 +177,11 @@ WebSocketTracker.prototype._openSocket = function () {
182177
self._onSocketClose()
183178
}
184179

185-
self.socket = socketPool[self.announceUrl]
180+
self.socket = self.client._socketPool[self.announceUrl]
186181
if (self.socket) {
187-
socketPool[self.announceUrl].consumers += 1
182+
self.client._socketPool[self.announceUrl].consumers += 1
188183
} else {
189-
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
184+
self.socket = self.client._socketPool[self.announceUrl] = new Socket(self.announceUrl)
190185
self.socket.consumers = 1
191186
self.socket.once('connect', self._onSocketConnectBound)
192187
}

0 commit comments

Comments
 (0)