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.