WebRequest.GetResponse causing SocketException - CLR_E_FAIL (6)

Hi All,

I’ve got a bit of a strange issue where I get the below exception being thrown after a few minutes of running. I can catch it fine, however when I do, the next time I call request.GetResponse() the method call never returns - when I pause in debugger, this is the last line avaialble in the call stack. Other threads are continuing to run ok, but this just seems to block indefinatlely.

The code is nothing special, it just calls to the same URL every minute within a while(true) loop, the response has .Close() explictly called on it each time, so it is being cleaned up, it is also wrapped in a using block, so dispose will be called on it also.

The exception thrown is:

#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (6) ####
#### Message: 
#### Microsoft.SPOT.Net.SocketNative::recv [IP: 0000] ####
#### System.Net.Sockets.Socket::Receive [IP: 0018] ####
#### System.Net.Sockets.NetworkStream::Read [IP: 0062] ####
#### System.Net.InputNetworkStreamWrapper::RefillInternalBuffer [IP: 0038] ####
#### System.Net.InputNetworkStreamWrapper::Read_HTTP_Line [IP: 004b] ####
#### System.Net.HttpWebRequest::ParseHTTPResponse [IP: 002e] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 0035] ####

Code being used is:


 using (HttpWebRequest wr = HttpWebRequest.Create(this.OperationUri) as HttpWebRequest)
            {
                wr.Timeout = 60*1000;
                wr.Method = "GET";

                using (HttpWebResponse resp = wr.GetResponse() as HttpWebResponse)
                {
                    if (resp.StatusCode == HttpStatusCode.OK)
                    {
                        Stream receiveStream = resp.GetResponseStream();
                        ....
                        .... Processes the stream .....
                    }

                    resp.Close();
                 }
          }

What does CLR_E_FAIL (6) actually mean? Any ideas on what is going on, and how to resolve this?

SocketException has a property ErrorCode. Catch that exception and check the ErrorCode - that will give you more information. Also please make sure that when you are done with the NetworkStream you close it properly (can’t see if you do it from your snippet).

Thanks. I’ve added the close to the stream as well, but still get the error occuring. The error code is 10035. I’ve found a few posts online with same error code, but no resolution for it. Any ideas??

I added KeepAlive = false to the web request, that seems to stop the thread from becoming blocked indefinatly. Exception still occurs, but can call the web request again ok now (well has been for the last couple of hours continuously).

It works now, so i’m happy. cheers.

I take that back, I’ve started getting socket exceptions with ErrorCode = -1728053248

Any ideas?

10035 is SocketError.WouldBlock. There are some examples how to handle it during send/receive operations. I am not sure what is the second one is.

If you post a simple class that we can run here to replicate the issue it might help.

I’ve put together this basic test. The only code omitted is that which sets up the wifi connection. The code just calls to google every second.

I am running this on Fez Cobra with WiFi module, using WiFi network.


  ...(Main)
   NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(RunBasicHttpTest);
  ....


 static void RunBasicHttpTest(object sender, NetworkAvailabilityEventArgs e)
        {
            if (e.IsAvailable && WiFi.IsLinkConnected && IsStarted == false)
            {
                while (true)
                {
                    try
                    {
                        using (HttpWebRequest request = HttpWebRequest.Create(@ "http://www.google.com") as HttpWebRequest)
                        {
                            request.Method = "GET";

                            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                            {
                                using (Stream receiveStream = response.GetResponseStream())
                                {

                                    // simulate reading the stream.
                                    using (StreamReader readStream = new StreamReader(receiveStream))
                                    {
                                        string s = readStream.ReadToEnd();

                                        readStream.Close();
                                        receiveStream.Close();

                                        response.Close();
                                    }
                                }
                            }

                        }

                        Debug.Print(DateTime.UtcNow.ToString());
                    }
                    catch (WebException webEx)
                    {
                        Debug.Print("Web Exception Occured");
                        if (webEx.InnerException is SocketException)
                        {
                            SocketException sock = webEx.InnerException as SocketException;
                            Debug.Print("Socket error code: " + sock.ErrorCode.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Print("Exception of type: " + ex.GetType().FullName);
                    }
                    finally
                    {
                        Thread.Sleep(1000);
                    }

                }

            }
        }
        


This ran ok for 25 mintutes, the last parts of the output are below, after this the thread that was calling the URL is blocked on the line below (Paused debugger and this is where the call stack ends)


 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

Output from sample code:

… (~25 mintutes)

01/01/2009 05:48:03
01/01/2009 05:48:05
01/01/2009 05:48:06
01/01/2009 05:48:08
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (5) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::recv [IP: 0000] ####
#### System.Net.Sockets.Socket::Receive [IP: 0018] ####
#### System.Net.Sockets.NetworkStream::Read [IP: 0062] ####
#### System.Net.InputNetworkStreamWrapper::RefillInternalBuffer [IP: 0038] ####
#### System.Net.InputNetworkStreamWrapper::Read_HTTP_Line [IP: 004b] ####
#### System.Net.HttpWebRequest::ParseHTTPResponse [IP: 002e] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 0035] ####
#### Sparrow.Program::RunBasicHttpTest [IP: 0036] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkChange::OnNetworkChangeCallback [IP: 002e] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkChange+NetworkChangeListener::OnEvent [IP: 000d] ####
#### Microsoft.SPOT.EventSink::ProcessEvent [IP: 0023] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
#### SocketException ErrorCode = -1728053248
#### SocketException ErrorCode = -1728053248
#### SocketException ErrorCode = -1728053248
#### Exception System.Net.WebException - 0x00000000 (5) ####
#### Message:
#### System.Net.HttpWebRequest::GetResponse [IP: 00c8] ####
#### Sparrow.Program::RunBasicHttpTest [IP: 0036] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkChange::OnNetworkChangeCallback [IP: 002e] ####
#### Microsoft.SPOT.Net.NetworkInformation.NetworkChange+NetworkChangeListener::OnEvent [IP: 000d] ####
#### Microsoft.SPOT.EventSink::ProcessEvent [IP: 0023] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll
Web Exception Occured
Socket error code: -1728053248

This is causing quite a headache for me, help would be very much appreciated.

I wonder if google just temporarily blocks you from slamming them every second for 25 minutes?

I will try your code here.

[offtopic]

Seems that your code messed up something. It does not matter where you click (within the frame), it heads to google.

Josh to the rescue :smiley:

[/offtopic]

Robert,

Hmm… Works fine for me here - no redirects or anything.

Architect, normally the URL is my own without dos / flooding protections, but either way I wouldn’t expect the tcp stack to have issues with that (and cause clr to lock up), worst case a timeout exception or http error code.

Any chance someone could run above example on a fez with wifi overnight and let me know the result? I can’t see what errors I could be making from a code pov to cause this.

Cheers all, and happy new year!

GHI or anyone else able to help me out and try on another cobra + wifi?