0

I am currently working on a MEAN stack project (Angular 5 + NodeJS), and I am relatively new to NodeJS. Currently I am facing an issue when it is deployed to production server which is hosted in Amazon EC2 servers which is in Linux. The server is getting crashed with following error.

Error: EMFILE: too many open files, open '/var/app/current/dist/index.html'

I have searched for solutions for this problem but unfortunately it didn't help. One of the solution was to increase the limit of open files and I did it, but today the server got crashed again. The most surprising fact is that I don't have any file operations in my application. Is this a code issue (like memory leak) or a OS limitation?. One more thing, during the development phase I was hosting my application in Heroku, over there I didn't get this issue. How can I permanently fix this issue? Please help me with some suggestions on how to identify the issue and fix this.

Here is my server.js code (removed api sections)

// API file for interacting with MongoDB

const config = require('./server/config');
require('./server/mongodb/collections');
require('./server/mongodb/initialisers');

// Parsers
app.use(compression());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: true
}));

// Angular DIST output folder
app.use(express.static(path.join(__dirname, 'dist')));

app.use(session({
  secret: 'mysecret',
  resave: true,
  saveUninitialized: true,
}));


// app.use(function(req, res, next) {
//   res.header("Access-Control-Allow-Origin", "*");
//   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//   next();
// });



// Send all other requests to the Angular app
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
});

//Set Port
const port = process.env.PORT || '3000';
app.set('port', port);

/*app.listen(port, '0.0.0.0', function() {
    console.log('Listening to port:  ' + port);
});*/
const server = http.createServer(app);

server.listen(port, () => console.warn(`Running on localhost:${port}`));

UPDATE: 29/08/2018

I have checked the logs from the server, I got these errors

Error: EMFILE: too many open files, open '/var/app/current/dist/styles.92f52039f26f58ffb65b.bundle.css'
Error: EMFILE: too many open files, open '/var/app/current/dist/index.html'

The css files are not directly called by my code, can anyone tell me why this issue is happening with a css files and any suggestions that I can try out for resolving this issue?

Thanks, Kapil

wizzardz
  • 5,664
  • 5
  • 44
  • 66
  • How many conncurrent connections do you usually have to your server? For each connection a new socket will be opened, which is essentially a file in linux. I believe that you just need to increase the default limit for the number of file descriptors, either the system wide or for the user under which your server is running, e.g. `ulimit -n` – vvraskin Jul 15 '18 at 06:29
  • vvraskin: so this is not a code issue or is it because the opened sockets are not getting closed? – wizzardz Jul 15 '18 at 11:17
  • Actually they must be closed due to timeout either on the client or on the server side. For me it sounds like you have indeed too many requests that are not getting closed before you are reaching the inodes limit, e.g. during spikes. To verify that I would have added some debugging to your code to check currently open sockets to see whether the curve grows fast. If its the case, then either decreasing the server timeout or bumping the inodes even more. – vvraskin Jul 15 '18 at 12:34
  • Might be this post could be of any use https://coderwall.com/p/f4loaw/hunting-leaking-connections-in-node-js-apps – vvraskin Jul 15 '18 at 12:38
  • Isn't this post duplicating this one? https://stackoverflow.com/questions/8965606/node-and-error-emfile-too-many-open-files – vvraskin Jul 15 '18 at 12:40
  • Possible duplicate of [node and Error: EMFILE, too many open files](https://stackoverflow.com/questions/8965606/node-and-error-emfile-too-many-open-files) – vvraskin Jul 15 '18 at 12:41

0 Answers0