Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect file descriptor leaks in Node

Tags:

node.js

I suspect that I have a file descriptor leak in my Node application, but I'm not sure how to confirm this. Is there a simple way to detect file descriptor leaks in Node?

like image 482
David Jones Avatar asked Sep 04 '13 19:09

David Jones


People also ask

How do I know if my file descriptor is leaking?

You can detect a file descriptor leak in two different ways: You may notice a lot of IOExceptions with the message “Too many open files.” During load testing, you periodically run a profiling script, such as lsof (on UNIX), and you notice that the list of file descriptors grows continually.

What is node file descriptor?

The file descriptor will be used to reference the correct file stream by all file system related functions. In fact stdout, stdin and stderr get assigned a file descriptor too, they are occupying fd 0 through 2 , the next free file descriptor value is 3. Thats why the value returned in your example is 3 .


1 Answers

Track open files

On linux you can use the lsof command to list the open files [for a process].

Get the PIDs of the thing you want to track:

ps aux | grep node

Let's say its PID 1111 and 1234, list the open files:

lsof -p 1111,1234

You can save that list and compare when you expect them to be released by your app.

Make it easier to reproduce

If it's taking a while to confirm this (because it takes a while to run out of descriptors) you can try to lower the limit for file descriptors available using ulimit

ulimit -n 500 #or whatever number makes sense for you
#now start your node app in this terminal
like image 60
Amir T Avatar answered Oct 17 '22 01:10

Amir T