0

I'm trying to create simple static file server with Node.js, namely to serve videos as video/mp4 (content-type), for download via http.

To note, the files are large (more then 100mb)

tried using the package node-serve which is recommended for static file serving but does not serve my purpose (and looks to be using blocking reads).

My custom code does work, yet i'm getting a memory leak each request, which after 10-20 requests crashes the process with the following error: FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory

each refresh is increasing the node.js's memory footprint by around 200mb.

Looked at most of the answers here for the memory leaks with node.js, none of the answers solve my issue, as it seems to be somewhat platform specific or a combination... and not strictly a code fault, have also noted developers experience this issue since version 0.6+ of node.js...

also saw posts that claim it happens with Express (https://npmjs.org/package/express)

the code used:

var fs = require('fs');
var http = require('http');
var mediaPath = '/mypublicdir';


function error404(res) {
    res.writeHead(404,{'content-type':'text/plain'});
    res.end();
} 

function err(str) {
    console.log(str);
}

var server = http.createServer(function (req,res){

  var filename,readStream;


  filename = mediaPath+req.url
  if (fs.existsSync(filename)) {
    readStream = fs.createReadStream(filename, {
        'flags': 'r',
            'mode': 0666,
            //'encoding': 'binary',
            'bufferSize': 8 * 1024      
    });

  } else {

    return;

  }

  readStream.on('data',function(data){
    res.write(data);
  });   

  readStream.on('open',function(data){
    console.log('Stream Open');
    res.writeHead(200,{'content-type':contentTypeSelector('video'),
    'Content-Transfer-Encoding' : 'binary',
    'Transfer-Encoding' : 'chunked'
    });
  });

  readStream.on('error',function(e){
    error404(res);
    err('Stream Read Error',{request:collectRequestMeta(req),filename:filename,error:e});
  });

  readStream.on('end',function(data){
  });

});


server.listen(8080);

the full warning each request:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at Socket.EventEmitter.addListener (events.js:160:15)
    at Socket.Readable.on (_stream_readable.js:653:33)
    at Socket.EventEmitter.once (events.js:179:8)
    at TCP.onread (net.js:527:26)

while writing this, i've found the following issue: NodeJS : How to debug "EventEmitter memory leak detected. 11 listeners added"

which states that this is a bug in nodejs core :(

strangely on my windows machine, it does Not happen. any ideas other then installing the legacy version v0.8.23 would be appreciated..

Community
  • 1
  • 1
Liam
  • 1
  • 2

3 Answers3

0

I actually experienced this same issue on node 0.10.4 (Node.js app becomes unresponsive if I reload multiple times in quick succession) - the best solution was to upgrade to 0.10.15 - the bug has been fixed in newer releases.

Community
  • 1
  • 1
sirKitty
  • 143
  • 2
  • 11
0

I have version 0.10.36 on Windows and still happens :/

Kurtz
  • 85
  • 8
-1

As was discussed in the issue on GitHub, this has been fixed since node v0.10.11. If you're encountering a bug with node, try updating first to see if it's already been fixed.