Skip to content

Commit

Permalink
fix: Cleanup duplicated deselect() code (#2113)
Browse files Browse the repository at this point in the history
The fileStream._destroy() function would never run because when fileStream.destroy() is called then it sets a 'destroyed' getter to true, which shadows our own fileStream.destroyed property.

The code outside running eos() was reaching into fileStream internals, so I just moved it in there.
  • Loading branch information
feross committed Jul 2, 2021
1 parent 0e2c88e commit b94d713
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
20 changes: 12 additions & 8 deletions lib/file-stream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const debug = require('debug')('webtorrent:file-stream')
const stream = require('readable-stream')
const eos = require('end-of-stream')

/**
* Readable stream of a torrent file
Expand All @@ -13,7 +14,6 @@ class FileStream extends stream.Readable {
constructor (file, opts) {
super(opts)

this.destroyed = false
this._torrent = file._torrent

const start = (opts && opts.start) || 0
Expand All @@ -33,6 +33,15 @@ class FileStream extends stream.Readable {
this._reading = false
this._notifying = false
this._criticalLength = Math.min((1024 * 1024 / pieceLength) | 0, 2)

this._torrent.select(this._startPiece, this._endPiece, true, () => {
this._notify()
})

// Ensure that cleanup happens even if destroy() is never called (readable-stream v3 currently doesn't call it automaticallly)
eos(this, (err) => {
this.destroy(err)
})
}

_read () {
Expand Down Expand Up @@ -85,16 +94,11 @@ class FileStream extends stream.Readable {
this._piece += 1
}

_destroy (err) {
if (this.destroyed) return
this.destroyed = true

_destroy (err, cb) {
if (!this._torrent.destroyed) {
this._torrent.deselect(this._startPiece, this._endPiece, true)
}

if (err) this.emit('error', err)
this.emit('close')
cb(err)
}
}

Expand Down
20 changes: 11 additions & 9 deletions lib/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { EventEmitter } = require('events')
const { PassThrough } = require('readable-stream')
const eos = require('end-of-stream')
const path = require('path')
const render = require('render-media')
const streamToBlob = require('stream-to-blob')
Expand All @@ -15,6 +14,7 @@ class File extends EventEmitter {

this._torrent = torrent
this._destroyed = false
this._fileStreams = new Set()

this.name = file.name
this.path = file.path
Expand Down Expand Up @@ -103,15 +103,12 @@ class File extends EventEmitter {
}

const fileStream = new FileStream(this, opts)
this._torrent.select(fileStream._startPiece, fileStream._endPiece, true, () => {
fileStream._notify()
})
eos(fileStream, () => {
if (this._destroyed) return
if (!this._torrent.destroyed) {
this._torrent.deselect(fileStream._startPiece, fileStream._endPiece, true)
}

this._fileStreams.add(fileStream)
fileStream.once('close', () => {
this._fileStreams.delete(fileStream)
})

return fileStream
}

Expand Down Expand Up @@ -154,6 +151,11 @@ class File extends EventEmitter {
_destroy () {
this._destroyed = true
this._torrent = null

for (const fileStream of this._fileStreams) {
fileStream.destroy()
}
this._fileStreams.clear()
}
}

Expand Down

0 comments on commit b94d713

Please sign in to comment.