From 6d5ff7ab06541847f86819825fe229516148b26e Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 2 Feb 2017 16:22:50 -0800 Subject: Be more defensive: prevent code from running after destroy --- lib/torrent.js | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/torrent.js') diff --git a/lib/torrent.js b/lib/torrent.js index 0f6b891..ab2dcda 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -587,9 +587,14 @@ Torrent.prototype._verifyPieces = function () { parallelLimit(self.pieces.map(function (_, index) { return function (cb) { if (self.destroyed) return cb(new Error('torrent is destroyed')) + self.store.get(index, function (err, buf) { + if (self.destroyed) return cb(new Error('torrent is destroyed')) + if (err) return process.nextTick(cb, null) // ignore error sha1(buf, function (hash) { + if (self.destroyed) return cb(new Error('torrent is destroyed')) + if (hash === self._hashes[index]) { if (!self.pieces[index]) return self._debug('piece verified %s', index) @@ -1523,6 +1528,8 @@ Torrent.prototype._request = function (wire, index, hotswap) { // TODO: might need to set self.pieces[index] = null here since sha1 is async sha1(buf, function (hash) { + if (self.destroyed) return + if (hash === self._hashes[index]) { if (!self.pieces[index]) return self._debug('piece verified %s', index) -- cgit v1.2.3