Why are my finished torrents (sometimes) not getting written to disk?

329 Views Asked by At

I try to write a little torrent client with electron and webtorrent. Everything seems fine at first but sometimes when a torrent is finished downloading the resulting files are not getting written to disk.

My project is setup via the SimulatedGREG/electron-vue boilerplate.

Here the code of my torrent client class:

const WebTorrent = require('webtorrent');
const client = new WebTorrent();  
export default class TorrentClient {
  download (downloadInfo) {
    console.log('download torrent from magnet link:', downloadInfo.magnetLink);

    let torrent = client.add(downloadInfo.infoHash);
    torrent.on('download', function (bytes) {
      console.log('just downloaded: ' + bytes);
      console.log('total downloaded: ' + torrent.downloaded);
      console.log('download speed: ' + torrent.downloadSpeed);
      console.log('progress: ' + torrent.progress);
    });
    torrent.on('done', function () {
      console.log('done...');
    });
  }
}
2

There are 2 best solutions below

0
On BEST ANSWER

Update: I fixed the problem.

After I debugged the fs-chunk-store which webtorrent is using, I found out that an error is thrown right before the data is writen to disk. The error occures when fs.mkdir is called to create the new path for the download destination ( ENOENT: no such file or directory, mkdir 'path/to/folder'). Without a callback function as parameter the error will not be printed to stdout.

My solution now is to use a custom implementation of fs-chunk-store which allows the creation of folders recursivly.

2
On

Does the Electron app shut down after the download, or does it keep running? (If the former, you'll need a way to delay that shutdown until you are sure writing has completed. E.g. Make sure there are no unresolved promises.)

Does the done message happen in the case when not everything is written to disk?

There are two error handlers you need to add to your code, which are likely to explain the problem.

The client has an error handler.

The torrent object also has an error handler.