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?
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.
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 .
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With