I have a multi-threaded application having 2 threads. Each thread services a different interface. Whenever a message needs to be sent out through an interface, the corresponding thread opens a socket(gets a fd) and sends the message and closes the fd. The problem is, when I have both the threads running, the sockets are not getting reused, I see fd number to increase inside /proc//fd. I can also verify this from the logs.
The believe the problem is that the thread requests a fd even before the other thread closes it's fd. So the OS returns the next available fd number and this keeps going on.
Thread 1 opens a tcp socket and thread 2 opens a can(controller area network) socket
time thread1 thread2
0 req fd idle
1 fd 1 req fd
2 close fd1 fd 2
3 req fd close fd 2
4 fd 3 req fd
5 close fd 3 fd 4
I believe this is what it would look like if I were to write it down. How can I make sure that the fd number doesn't keep growing? I am running 32 bit linux on an ARM processor.
-
What kind of sockets?user3657941– user36579412016年11月16日 18:21:43 +00:00Commented Nov 16, 2016 at 18:21
-
Thread 1 opens a tcp socket and thread 2 opens a can(controller area network) socketbhargava_sg– bhargava_sg2016年11月16日 18:47:14 +00:00Commented Nov 16, 2016 at 18:47
-
Posix sockets I assume?smac89– smac892016年11月16日 18:50:05 +00:00Commented Nov 16, 2016 at 18:50
-
I am guessing that you are using some kind of embedded operating system running on an 8-bit micro-controller if the maximum file descriptor value is 127. Don't close the sockets. There should be no need.user3657941– user36579412016年11月16日 19:06:18 +00:00Commented Nov 16, 2016 at 19:06
-
@DavidCullen, I am running 32 bit linux on an ARM processor. I was returning the sockfd as int8_t and that explains the 127 limit. But I still want to know how I can make sure the fd number does not grow. The software architecture is such that every message is sent as a new connection so I can not change that.bhargava_sg– bhargava_sg2016年11月16日 19:17:51 +00:00Commented Nov 16, 2016 at 19:17
2 Answers 2
The gaps in socket descriptor values are not an evidence of a resource leak. Allocating a file descriptor is a thread-safe operation (probably, as are all system calls). If you close each socket properly, there will be no leaks.
You can monitor how the number of open file descriptor changes with the following command:
watch --interval=0.5 "sh -c 'lsof -p YOUR_PROGRAM_PID_HERE | wc -l'"
If the number grows, then probably there are some sockets (or other FDs) not closing.
If you are still not sure if there are descriptor leaks, you may try to count number of open file descriptor programmatically at the exit of your program, either by examining number of entries at /proc/self/fd directory, or by repeatedly trying to close all descriptors and counting how many calls did not return EBADF error. If this number is different each time, then probably there are leaks.
Comments
Turns out, in the can code at one condition I was not closing the fd!