4
votes

System Description: I have a WCF service (self hosted in a windows service) which is consumed by a client (also a windows service). The two applications are designed to have constant uptime. The 'queue' application reads entries from a database, sends 'jobs' to the WCF service for processing which returns some information to the client, and the client stores the results back into the DB.

Error Details: After around two hours the client application can no longer connect to the WCF service reporting the error:

"Insufficient winsock resources available to complete socket connection initiation.",

A second application which runs on the same server also starts throwing exceptions from this time onwards:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"

The second application is nothing to do with the WCF client and server, it just runs on the same server and simply reads/writes to a database.

Server/Client setup/code:

Server Details:

  • Self hosted inside a windows service.
  • netTcpBinding
  • receiveTimeout="00:00:15"
  • serviceThrottling maxConcurrentCalls="2147483647"
  • maxConcurrentSessions="2147483647"

Client Connection Class::

public class ClientConnectionClass : ClientBase<IFileService>, IFileService, IDisposable
{

    public void callMethod(InputRequest request)
    {
        result = base.Channel.doRequest(request);
    }

    void Dispose()
    {
        bool success = false;
        try
        {
            if (State != CommunicationState.Faulted)
            {
                Close();
                success = true;
            }
        }
        finally
        {
            if (!success)
            {
                Abort();
            }
        }
    }
 }

Client Process (windows service):

while(true)
{
    // Do some stuff before, code ommited
    ClientConnectionClass ccc = new ClientConnectionClass();
    ccc.callMethod(inputRequest);
    // Do some stuff with the response
    // Close the connection class, is this the right way to close it?
    ccc.Close();
    Thread.Sleep(1000);
}

Possible Explanations: I think that based on the first exception error, the code is not closing/releasing socket connections or running out of ports to connect on (please note I am explicitly calling the Close() method of the ClientBase from the ClientProcess, in addition to implementing the Dispose() method on the ClientBase).

Notes: Client application is multi-threaded, has up to 4 concurrent threads running at the same time, each of which calls the WCF service. The client to WCF service is fully working up until the 2 hour (ish) point where the Client Process then spits out errors, and other (unrelated) Windows Services (which use network resources) also start to spit out errors.

Possible Questions to answer: Am I creating/disposing of the Class that implements the ClientBase class? Is there any easier way to debug/log the current state of either the Client or WCF service ( I have attached perfmon.exe, but it doesn't really give much useful information on the socket/network side of things ).

Thanks

UPDATE: I have actually now wrapped the ClientConnectionClass into a 'using' statement, I am currently testing this ( which usually takes 2 hours ). Update, this did not work.

2
My hunch will be on your threads. Try just running one thread and see what happensSurjit Samra

2 Answers

2
votes

I am running into a very similar situation using Windows Server 2008 R2 SP1. Been troubleshooting and researching for a while now. Found a support article which sounds exactly like it.

This might be a fix for you as well:

http://support.microsoft.com/kb/2577795

There's a hotfix for it.

Symptom as described in the article:

"Consider the following scenario: You have a multiprocessor computer that is running Windows Server 2008 R2 or Windows 7. You run an application that creates loopback sockets on the computer. In this scenario, the application may be unable to create new sockets and a "no buffer space available" exception occurs on the computer. Additionally, when this issue occurs users cannot remotely connect to the computer until it is restarted."

Cause as described in the article:

"This issue occurs because of a race condition in the Ancillary Function Driver for WinSock (Afd.sys) that causes sockets to be leaked. With time, the issue that is described in the "Symptoms" section occurs if all available socket resources are exhausted."

UPDATE: I've verified through upgrading one server and not another that the hotfix does resolve the issue for me. Ran them side by side and the one without the hotfix ran into the WinSock problem while the server with the hotfix is still reseeding its source ports in the 49-50k range.

0
votes

What platform are you running? Win2k3, Win2k8? I have seen unsubstantiated reports on the web of applications experiencing similar issues running on pre-Win2k8 systems that "resolve" running on Win2k8. Also, if the client and server are running on the same system, is it possible to use a namedpipe binding vs. nettcp? Not really a fix but may workaround your particular issue.