Home » Inode, File descriptors and FD Leaking

In Linux, we do have that philosophy where “Everything is a File” even though it’s beyond that. I randomly came across an issue with File Descriptor leak and thought of writing this article about Inodes and File descriptors.

What are Inodes in Linux ?


Inodes is like an identity Proof for a file or a directory in linux which has all the metadata regarding that filesystem object like size, ownership,permissions,timestamp[creation/modification/access] and most importantly, pointers which points to the actual block where the data is really stored.

The maximum number of inodes that can be created is allocated during the creation of the file system itself and the maximum number of inodes will determine the maximum number of files which can be created in that file system.

How to find the number of Inodes available ?

To find the number of available inodes in a system we can use df -i command.

How can we increase the inode capacity ?

We can achieve it in two ways.

Note: Increasing the number of inodes may result in a smaller amount of disk space available for file storage, as more space is allocated for metadata.


What are File Descriptors in Linux ?

File Descriptors are non-negative integer values that acts as handle to files or I/O resources such as pipes,sockets or data streams. Every file or resource opened in linux is associated with this file descriptor. Each process has its own table of file descriptors which is maintained by kernel. The three standard File descriptors in linux are stdin[0], stdout[1] and stderr[2].

How to check the FDs ?

We can easily check the opened files and FDs for a process using lsof command or by listing under /proc/<pid>/fd

How are inodes and File descriptors are related ?

When a process wants to access a file, it uses the file’s path to locate the corresponding inode in the file system as the inode contains information about the file, including its data blocks and metadata. Once the process has located the inode, it requests the operating system to open the file using system calls like open(). The operating system then assigns a file descriptor to the open file and returns the file descriptor to the process.

Once a file is opened and a file descriptor is obtained, the process can use the file descriptor to perform read, write, seek, and other I/O operations on the file. When the process performs an I/O operation using the file descriptor, the operating system translates these operations into actions on the underlying inode and data blocks. When a process is done with a file, it closes the file using the close() system call. This closes the file descriptor associated with the file in the process’s file descriptor table which will release the process’s reference to the file.


What is a File Descriptor leak in Linux ?


A file descriptor leak in Linux occurs when a process repeatedly opens file descriptors without properly closing them even when they dont need that anymore, which will end up in the exhaustion of available file descriptors.

Default value for number of FDs which can be opened by a process is 1024. This can be varied W.R.T different OS. Using ulimit command, we will be able to retrieve that information.

root@ubuntu-desktop:~# ulimit -n
1024

Mostly, FD leaks occurs due to programming errors such as forgetting to close the opened file, unclosed network sockets, forking without closing the already opened FDs etc.


How to fix a File descriptor leak ?

These are some general guidelines to fix file descriptor leaks in your program.

1. Review the code and inspect if any file descriptors are opened and not closed properly. 
2. Ensure Proper error handling is enabled while opening a file or resource to get that resource released after usage. 
3. Make sure that file descriptors are closed as soon as they are no longer needed.
4. If the application forks to create child processes, ensure that child processes close inherited file descriptors that are not needed.
5. Consider using resource limits provided by the operating system to restrict the number of file descriptors that a process can open. 
6. Use monitoring tools to monitor file descriptor usage and detect any leaks in production environments.

By following these steps and ensuring that file descriptors are properly managed and closed when they are no longer needed, you can effectively fix file descriptor leaks and improve the stability and performance of your applications.

I hope this article was helpful.
Adios Amigo…………

Add Comment

Click here to post a comment