Node.js does not close files created by fs.createReadStream ()

On my server, every time a user uses our service, we must get a JSON file for them from the server. I do this using fs.createReadStream() inside my own function.

 function getJSONFromServer(filepath, callback){ var data = fs.createReadStream(filepath); data.on('error', function (error) { console.log("Caught", error); callback(undefined, error); }); var jsonFile = ""; data.on('data', function(chunk) { jsonFile += chunk; }); data.on('end', function() { var jsonData = JSON.parse(jsonFile); callback(jsonData); data.destroy(); data.close(); }); } 

This is a task, but it does not close the connection to the file. Therefore, after reading 1024 files (the limit on my server) Node.js will EMFILE, too many open files error EMFILE, too many open files . Then I have to kill our Node.js server, open it again, and this will clear the "open files".

I check the number of open files lsof -i -n -P | grep nodejs lsof -i -n -P | grep nodejs . It displays something like this:

 nodejs 13707 node 10u IPv4 1163695 0t0 TCP 127.0.0.1:55643->127.0.0.1:27017 (ESTABLISHED) nodejs 13707 node 11u IPv4 1163697 0t0 TCP 127.0.0.1:55644->127.0.0.1:27017 (ESTABLISHED) 

for the number of open files.

I tried using graceful-fs. I tried calling stream.destroy() and stream.close() , but I still get the same problem. My server is essentially a ticking time bomb because we get a strong, steady stream of users, and after many users have connected, it just stops working.

In addition, ulimit -n [open file amount] does not work, and even if it is, it is not a long-term solution, because I would like my file connections to close and not open for no reason.

I am using Node.js version v0.10.25 , Ubuntu 15.04 (GNU/Linux 3.19.0-42-generic x86_64) and the latest version of graceful-fs if that helps!

Thanks for any help you can provide.

+5
source share
1 answer

It must be the stupidest mistake I have ever made. Regardless, here is the answer. I hope I can save someone from this mistake and almost tear my hair out.

I ran my application with nodejs , not node . It turns out that if you do nodejs --version , most likely it will return a very old version, which for me was v0.10.25 . node --version however v5.6.0 . Obviously, this massive leap in versions will fix some things, so I ran the application with node app.js instead of nodejs app.js , and since then I have not had a problem. Currently, there are only 6 open files, whereas before we had more than 1000 with time.

Damn, I feel good that it's from my chest.

+3
source

Source: https://habr.com/ru/post/1242299/


All Articles