Skip to content

Commit 5b79d42

Browse files
committed
client: destroy callback isn't called until after cleanup
1 parent 82aea33 commit 5b79d42

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

client.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var debug = require('debug')('bittorrent-tracker')
44
var EventEmitter = require('events').EventEmitter
55
var inherits = require('inherits')
66
var once = require('once')
7+
var parallel = require('run-parallel')
78
var url = require('url')
89

910
var common = require('./lib/common')
@@ -46,8 +47,10 @@ function Client (peerId, port, torrent, opts) {
4647
self._infoHashHex = self._infoHash.toString('hex')
4748
self._infoHashBinary = self._infoHash.toString('binary')
4849

49-
self._port = port
5050
self.torrentLength = torrent.length
51+
self.destroyed = false
52+
53+
self._port = port
5154

5255
self._rtcConfig = opts.rtcConfig
5356
self._wrtc = opts.wrtc
@@ -236,14 +239,19 @@ Client.prototype.setInterval = function (intervalMs) {
236239

237240
Client.prototype.destroy = function (cb) {
238241
var self = this
242+
if (self.destroyed) return
243+
self.destroyed = true
239244
debug('destroy')
240245

241-
self._trackers.forEach(function (tracker) {
242-
tracker.destroy()
243-
tracker.setInterval(0) // stop announcing on intervals
246+
var tasks = self._trackers.map(function (tracker) {
247+
return function (cb) {
248+
tracker.destroy(cb)
249+
tracker.setInterval(0) // stop announcing on intervals
250+
}
244251
})
252+
253+
parallel(tasks, cb)
245254
self._trackers = []
246-
if (cb) process.nextTick(function () { cb(null) })
247255
}
248256

249257
Client.prototype._defaultAnnounceOpts = function (opts) {

lib/http-tracker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ HTTPTracker.prototype.setInterval = function (intervalMs) {
8888
}
8989
}
9090

91-
HTTPTracker.prototype.destroy = function () {
91+
HTTPTracker.prototype.destroy = function (cb) {
9292
var self = this
93+
if (self.destroyed) return
9394
self.destroyed = true
95+
cb(null)
9496
}
9597

9698
HTTPTracker.prototype._request = function (requestUrl, opts, cb) {

lib/udp-tracker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ UDPTracker.prototype.setInterval = function (intervalMs) {
6363
}
6464
}
6565

66-
UDPTracker.prototype.destroy = function () {
66+
UDPTracker.prototype.destroy = function (cb) {
6767
var self = this
6868
if (self.destroyed) return
6969
self.destroyed = true
@@ -72,6 +72,7 @@ UDPTracker.prototype.destroy = function () {
7272
cleanup()
7373
})
7474
self._cleanupFns = []
75+
cb(null)
7576
}
7677

7778
UDPTracker.prototype._request = function (opts) {

lib/websocket-tracker.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,26 @@ WebSocketTracker.prototype.setInterval = function (intervalMs) {
8787
}
8888
}
8989

90-
WebSocketTracker.prototype.destroy = function () {
90+
WebSocketTracker.prototype.destroy = function (onclose) {
9191
var self = this
9292
if (self.destroyed) return
9393
self.destroyed = true
9494

9595
self._socket.removeListener('data', self._onSocketDataBound)
9696
self._socket.removeListener('close', self._onSocketCloseBound)
9797
self._socket.removeListener('error', self._onSocketErrorBound)
98+
9899
self._onSocketErrorBound = null
99100
self._onSocketDataBound = null
100101
self._onSocketCloseBound = null
101102

102103
self._socket.on('error', noop) // ignore all future errors
103-
try { self._socket.close() } catch (e) {}
104+
try {
105+
self._socket.destroy(onclose)
106+
} catch (err) {
107+
if (onclose) onclose()
108+
}
109+
104110
self._socket = null
105111
}
106112

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"minimist": "^1.1.1",
3232
"once": "^1.3.0",
3333
"random-iterate": "^1.0.1",
34+
"run-parallel": "^1.1.2",
3435
"run-series": "^1.0.2",
3536
"simple-get": "^1.3.0",
3637
"simple-peer": "^5.0.0",

0 commit comments

Comments
 (0)