Error: accept EMFILE
I just made a simple server which receives data from client via http protocols and store them into the mongodb. But the server always crashes with this error:
events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: accept EMFILE
at errnoException (net.js:769:11)
at TCP.onconnection (net.js:1017:24)
source code is like this:
app.get('/log', function(req, res) {
var db = require("mongojs").connect("mydb");
var collection = db.collection(req.query.id);
collection.save(req.query, function(err, saved) {
var result = (err || !saved) ? err + " : log not saved " : req.query;
console.log(result);
res.send(result);
});
});
The server finds a collection by an id argument which is received by a client - most of the time, the id is identical - and it stores the whole data which is represented by json format.
A significant thing is, in this case, whenever the requests are received, the count of the file's descriptor that are used by this process always gets bigger. I verified it using this shell command:
ls -l /proc/<process id>/fd|wc -l
If I remove the lines that are dealt with mongojs, nothing happens.
I know that a file descriptor is always used by a process when it opens a file or connects somewhere. But I guess this should not happen because this is not the case for the concurrent condition.
If this is a bug, could you please recommend a work around for this issue?
It seems like you are making a new connection to the database on each request which would explain why you are running out of file descriptors.
Try moving this line into the beginning of the your program:
var db = require("mongojs").connect("mydb");
app.get('/log', ... );