Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dht.lookup() throws uncatchable error #224

Open
phillmac opened this issue Dec 27, 2020 · 1 comment
Open

dht.lookup() throws uncatchable error #224

phillmac opened this issue Dec 27, 2020 · 1 comment

Comments

@phillmac
Copy link

const q = query.q.toString()

Version 9.0.3
node v14.15.3

The line numbers may have changed a little due to a slightly outdated version , but the above seems to be the referenced line.
Moreover the cause of the error should still be present, because the underlying cause hasn't changed.
The trace is as follows:

/torrent-health-scraper/node_modules/bittorrent-dht/client.js:505
  var q = query.q.toString()
                  ^

TypeError: Cannot read property 'toString' of undefined
    at DHT._onquery (/torrent-health-scraper/node_modules/bittorrent-dht/client.js:505:19)
    at RPC.onquery (/torrent-health-scraper/node_modules/bittorrent-dht/client.js:95:10)
    at RPC.emit (events.js:315:20)
    at RPC.onquery (/torrent-health-scraper/node_modules/k-rpc/index.js:67:10)
    at RPC.emit (events.js:315:20)
    at Socket.onmessage (/torrent-health-scraper/node_modules/k-rpc-socket/index.js:121:12)
    at Socket.emit (events.js:315:20)
    at UDP.onMessage [as onmessage] (dgram.js:919:8)

Possibly this is an error in k-rpc? if the q property should always be present and it somehow isn't.
However imho it seems we could make some improvements here by removing the assumption that everything will always be fine and at least throw a catchable error instead of crashing.

@phillmac
Copy link
Author

My code looks something like this, I don't think there's any way I can trap this error.
I'd be glad to be wrong and have it pointed out.

function scrapeDHT (torrent) {
    return new Promise((resolve, reject) => {
      console.debug('Scraping DHT peers ...')
      try {
        const dht = new DHT()
        const peers = []
        dht.on('peer', function (peer, _ih, _from) {
          const peerhash = crypto.createHash('md5').update(peer.host + peer.port).digest('hex')
          if (!(peers.includes(peerhash))) {
            peers.push(peerhash)
          }
        })
        dht.lookup(torrent._id, async function () {
          console.debug('DHT scrape complete')
          torrent.dhtData = {
            infoHash: torrent._id,
            peers: peers.length,
            scraped_date: Math.floor(new Date() / 1000)
          }
          resolve()
        })
      } catch (err) {
        reject(err)
      }
    })
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant