Windows Sockets 2.0 Duplicated Sockets Persist After Closing

ID: Q229658


The information in this article applies to:


SYMPTOMS

Windows Sockets version 2.0 does not deallocate a socket that has been duplicated [using WSADuplicateSocket()] if the Closesocket() function is called against the duplicated socket descriptor first, and then against the duplicate socket.

Although the socket is ultimately deallocated when the process quits, overuse of socket resources may occur in the interim. Even after closing the socket at the program level, the socket provider may see a socket using that address.


CAUSE

The socket handle is obtained from the system kernel by the socket provider. The internal data that the kernel handle references contains an entry point into a socket provider's close function. Duplicate socket handles do not include provider links.

When Closesocket() is called, the handle reference count is decremented. If it is zero, the socket provider close function is invoked, and the handle is then destroyed. Because the duplicate socket is not linked to the provider, the provider close function cannot be invoked if it is closed last.


RESOLUTION

Use of the SO_REUSEADDR socket option is indicated, because by default, a socket cannot be bound to a local address that is already in use.


STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

For additional information about duplicated sockets and Closesocket(), please see the following article in the Microsoft Knowledge Base:

Q198663 BUG: Closesocket() on a Duplicated Socket Fails to Clean Up

Additional query words: leak


Keywords          : kbprg win95x 
Version           : WINDOWS:95
Platform          : WINDOWS 
Issue type        : kbprb 

Last Reviewed: July 22, 1999