From 78cd54797310f3b3993425019baba0dd68ed4b23 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 28 May 2016 17:35:51 -0700 Subject: [PATCH] Fix exception in file.js If file is destroyed and stream ends afterwards, then an exception is thrown because self._torrent is undefined. --- lib/file.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/file.js b/lib/file.js index c73fc386..ff19a1c1 100644 --- a/lib/file.js +++ b/lib/file.js @@ -16,6 +16,7 @@ function File (torrent, file) { EventEmitter.call(this) this._torrent = torrent + this._destroyed = false this.name = file.name this.path = file.path @@ -61,6 +62,7 @@ File.prototype.createReadStream = function (opts) { fileStream._notify() }) eos(fileStream, function () { + if (self._destroyed) return if (!self._torrent.destroyed) { self._torrent.deselect(fileStream._startPiece, fileStream._endPiece, true) } @@ -89,5 +91,6 @@ File.prototype.renderTo = function (elem, cb) { } File.prototype._destroy = function () { + this._destroyed = true this._torrent = null }