Socket descriptors not freed on close

priyatendulkar's Avatar, Join Date: Jun 2011
Go4Expert Member
Hi All

I am facing a wired problem.The socket descriptor is not free (entry persists in /proc/pid/fd)
even though it is closed.

Code snippet is
Code:
int  ctrlSock = socket (AF_INET, SOCK_STREAM, 0);
    if (ctrlSock < 0)
        return (-1);

   struct sockaddr_in ctrlAddr;

    ctrlAddr.sin_family      = AF_INET;
    ctrlAddr.sin_addr.s_addr = INADDR_ANY;
    ctrlAddr.sin_port        = htons (0);
    if (bind (ctrlSock, (struct sockaddr *)&ctrlAddr, sizeof (ctrlAddr)) < 0)
        {
        close (ctrlSock);
        return (-1);
        }

  close (ctrlSock);
Here , even after close, the descriptor entry exits in /proc/pid/fd

Last edited by shabbir; 3Jan2014 at 13:41.. Reason: Code blocks
0
priyatendulkar's Avatar, Join Date: Jun 2011
Go4Expert Member
Hi,

The issue has been resolved..
Actually the sockets were getting freed but same fd was getting reallocated immediately.

As the same socket fd were reused, it was thought as if the descriptor was not getting released...

It was verified using netstat, /proc/<pid>/fd,/proc/net/tcp,/proc/net/raw and strace.

/proc/<pid>/fd, /proc/net/tcp OR /proc/net/raw... (depending on the socket type) are very good debugging tools for verifying socket usage in process.