Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

Socket memory leak when you call a WCF service that uses net.tcp binding and reliable sessions


Symptom

Consider the following scenario:
  • You have a WCF service that is hosted in Internet Information Services (IIS).
  • This service uses the net.tcp port sharing service and is also configured to use reliable messaging or reliable sessions.
  • You have a client website that calls the WCF service in a using pattern.
In this scenario, you may experience out of kernel resources opening a new socket in this application. Additionally, you receive the following error message:
System.InsufficientMemoryException: Insufficient winsock resources available to complete socket connection initiation. ---> System.Net.Sockets.SocketException: An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full 127.0.0.1:808  
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)  
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)  
at System.ServiceModel.Channels.SocketConnectionInitiator.Connect(Uri uri, TimeSpan timeout)



↑ Back to the top


Cause

This issue occurs because of a timing bug in WCF reliable messaging.

This issue is that the factory object is being closed before the socket that is used to make the WCF call is added to the Factory’s idle connection pool. Therefore the System.Net.Sockets.Socket is leaked. Over time as you continue to see leaked Socket objects, you eventually run out of kernel mode space to open a new socket and receive the error message.  

↑ Back to the top


Workaround

To resolve this issue, use one of the following methods:
  • Disable reliable messaging on your binding.
  • Add a sleep call to force a wait, to prevent the timing bug, between the time that you close the channel object and the factory object.
  • Create one factory object and reuse that existing object every time that you need a new channel, and then make your call. Do not create a new factory and channel object for each service call, just reuse the same factory object and go from there.

↑ Back to the top


Keywords: kbsurveynew, kbexpertiseadvanced, kbtshoot, kb

↑ Back to the top

Article Info
Article ID : 3012125
Revision : 1
Created on : 1/7/2017
Published on : 12/5/2014
Exists online : False
Views : 73