Socket descriptors not freed on close

Discussion in 'C' started by priyatendulkar, Jan 3, 2014.

  1. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    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 a moderator: Jan 3, 2014
  2. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice