client: destroy callback isn't called until after cleanup

This commit is contained in:
Feross Aboukhadijeh 2015-07-16 18:33:50 -07:00
parent 82aea33dfa
commit 5b79d42dcd
5 changed files with 27 additions and 9 deletions

View File

@ -4,6 +4,7 @@ var debug = require('debug')('bittorrent-tracker')
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var once = require('once')
var parallel = require('run-parallel')
var url = require('url')
var common = require('./lib/common')
@ -46,8 +47,10 @@ function Client (peerId, port, torrent, opts) {
self._infoHashHex = self._infoHash.toString('hex')
self._infoHashBinary = self._infoHash.toString('binary')
self._port = port
self.torrentLength = torrent.length
self.destroyed = false
self._port = port
self._rtcConfig = opts.rtcConfig
self._wrtc = opts.wrtc
@ -236,14 +239,19 @@ Client.prototype.setInterval = function (intervalMs) {
Client.prototype.destroy = function (cb) {
var self = this
if (self.destroyed) return
self.destroyed = true
debug('destroy')
self._trackers.forEach(function (tracker) {
tracker.destroy()
tracker.setInterval(0) // stop announcing on intervals
var tasks = self._trackers.map(function (tracker) {
return function (cb) {
tracker.destroy(cb)
tracker.setInterval(0) // stop announcing on intervals
}
})
parallel(tasks, cb)
self._trackers = []
if (cb) process.nextTick(function () { cb(null) })
}
Client.prototype._defaultAnnounceOpts = function (opts) {

View File

@ -88,9 +88,11 @@ HTTPTracker.prototype.setInterval = function (intervalMs) {
}
}
HTTPTracker.prototype.destroy = function () {
HTTPTracker.prototype.destroy = function (cb) {
var self = this
if (self.destroyed) return
self.destroyed = true
cb(null)
}
HTTPTracker.prototype._request = function (requestUrl, opts, cb) {

View File

@ -63,7 +63,7 @@ UDPTracker.prototype.setInterval = function (intervalMs) {
}
}
UDPTracker.prototype.destroy = function () {
UDPTracker.prototype.destroy = function (cb) {
var self = this
if (self.destroyed) return
self.destroyed = true
@ -72,6 +72,7 @@ UDPTracker.prototype.destroy = function () {
cleanup()
})
self._cleanupFns = []
cb(null)
}
UDPTracker.prototype._request = function (opts) {

View File

@ -87,7 +87,7 @@ WebSocketTracker.prototype.setInterval = function (intervalMs) {
}
}
WebSocketTracker.prototype.destroy = function () {
WebSocketTracker.prototype.destroy = function (onclose) {
var self = this
if (self.destroyed) return
self.destroyed = true
@ -95,12 +95,18 @@ WebSocketTracker.prototype.destroy = function () {
self._socket.removeListener('data', self._onSocketDataBound)
self._socket.removeListener('close', self._onSocketCloseBound)
self._socket.removeListener('error', self._onSocketErrorBound)
self._onSocketErrorBound = null
self._onSocketDataBound = null
self._onSocketCloseBound = null
self._socket.on('error', noop) // ignore all future errors
try { self._socket.close() } catch (e) {}
try {
self._socket.destroy(onclose)
} catch (err) {
if (onclose) onclose()
}
self._socket = null
}

View File

@ -31,6 +31,7 @@
"minimist": "^1.1.1",
"once": "^1.3.0",
"random-iterate": "^1.0.1",
"run-parallel": "^1.1.2",
"run-series": "^1.0.2",
"simple-get": "^1.3.0",
"simple-peer": "^5.0.0",