Windows Sockets 2.0 Duplicated Sockets Persist After Closing (229658)



The information in this article applies to:

  • Microsoft Windows 98
  • Microsoft Windows 95

This article was previously published under Q229658

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 that this is a problem in the Microsoft products that are 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:

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


Modification Type:MinorLast Reviewed:12/17/2004
Keywords:kbnofix kbprb kbProgramming KB229658