mafintosh / torrent-stream

The low level streaming torrent engine that peerflix uses
MIT License
1.94k stars 227 forks source link

Memory leak #177

Open vanilla38 opened 7 years ago

vanilla38 commented 7 years ago

Hello, I noticed that memory wasnt freed after download is complete, here's my code:

`app.get('/dltorrent', function (req, res) { var urlObj = url.parse(req.url, true), query = urlObj.query;

if (query.magnet !== undefined && query.title !== undefined && query.magnet !== 'undefined' && query.title !== 'undefined') {
    var opts = {
        tmp: '/usr/share/nodejs/www/data',
        connections: 10,
        uploads: 0
    }

    var engine = torrentStream('magnet:?xt=urn:btih:' + query.magnet, opts);
    engine.on('ready', function() {
        engine.files.forEach(function(file) {
            if (file.name == query.title) {
                var stream = file.createReadStream();
                    res.setHeader('Content-disposition', 'attachment; filename=' + query.title);
                    res.setHeader('Content-type', mime.lookup(query.title));
                    stream.pipe(res);
            }
        });
    });
} else {
    res.end('invalid dl parameters');
}

});`

vanilla38 commented 7 years ago

Okay, I noticed that these leaks were caused by:

swarm.add(addr) => torrent-stream index.js Line 702

Any idea on how to solve it ?

dizel3d commented 7 years ago

The leak is reproduced on Linux, not on Windows. I simplified the leak reproduction, see https://github.com/dizel3d/node-memory-leak-sample I made 300 iterations and couldn't detect leaking objects using v8-profiler, comparing heap snapshots. It seems the leak has native code roots.