Cobra freezed until SocketException is thrown

Hi!

I am doing http request.


HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();

Cobra is connected with cable to router which does not have internet connection.
Unfortunately until exception below is thrown Cobra is not responding at all - is freezed.


    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (37) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
    #### System.Net.Dns::GetHostEntry [IP: 0008] ####
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 00e1] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### N.Net.MailNotify+SendMesageRequest::Send [IP: 004d] ####
    #### SocketException ErrorCode = -1
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (39) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
    #### System.Net.Dns::GetHostEntry [IP: 0008] ####
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 00e1] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### N.Net.MailNotify+SendMesageRequest::Send [IP: 004d] ####

What can be done, so Cobra will work simultaneously ?

Regards,
Jack

Check this:
http://www.tinyclr.com/forum/23/6126/

But for me it is not a sollution.
I may check that dns is available, but another connection will hang.
Why is Cobra freezing on this request?

My guess: because it is a blocking call. You could do this operation in a separate thread from your UI so the whole board is not setting waiting on the HttpWebRequest.

It is done in separate thread.
But all other threads are stopped.

You mean you have no other threads running when you call the blocking method? Or, do you mean calling HTTPRequest stops all other threads? If so how do you know it stops all other threads?

I have very main function with blinking led:


public static void Main()
        {
            Led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);

            Setup();

            while (true)
            {
                Thread.Sleep(500);
                Program.LED(true);
                Thread.Sleep(500);
                Program.LED(false);
            }
        }

When it is done LED stops blinking.

is your HttpRequest done by ‘SetUp();’? If so the LED will never start blinking until set up returns. Unless you create a new thread to run the Http code it in will run in the same thread as your Main.

HttpWebRequest is done in seperate thread.

Loop of blinking LED is status that Cobra is working.

Sample app demonstrating behaviour:


public class Program : Microsoft.SPOT.Application
    {
        public static void Main()
        {
            Thread thread = new Thread(new ThreadStart(Http));
            thread.Start();

            OutputPort Led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);

            while (true)
            {

                Thread.Sleep(500);
                Debug.Print(DateTime.Now.ToUniversalTime() + ": Led: ON");
                Led.Write(true);
                Thread.Sleep(500);
                Debug.Print(DateTime.Now.ToUniversalTime() + ": Led: OFF");
                Led.Write(false);
            }
        }

        public static void Http()
        {
            Thread.Sleep(5000);
            try
            {
                Debug.Print(DateTime.Now.ToUniversalTime() + ": Request: START");
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.google.pl");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception)
            {
                Debug.Print(DateTime.Now.ToUniversalTime() + ": Request: EXCEPTION");
            }
        }

    }

Output:


01/01/2009 01:28:13: Led: ON
01/01/2009 01:28:14: Led: OFF
01/01/2009 01:28:14: Led: ON
01/01/2009 01:28:15: Led: OFF
01/01/2009 01:28:16: Led: ON
01/01/2009 01:28:16: Led: OFF
01/01/2009 01:28:17: Led: ON
01/01/2009 01:28:17: Led: OFF
01/01/2009 01:28:18: Led: ON
01/01/2009 01:28:18: Request: START
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (3) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
    #### System.Net.Dns::GetHostEntry [IP: 0008] ####
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 00e1] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### InetTest.Program::Http [IP: 002f] ####
    #### SocketException ErrorCode = -1
    #### SocketException ErrorCode = -1
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = -1
    #### SocketException ErrorCode = -1
    #### Exception System.Net.WebException - 0x00000000 (3) ####
    #### Message: host not available
    #### System.Net.HttpWebRequest::EstablishConnection [IP: 00f1] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### InetTest.Program::Http [IP: 002f] ####
01/01/2009 01:28:52: Led: OFF
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
    #### Exception System.Net.WebException - 0x00000000 (3) ####
    #### Message: 
    #### System.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
    #### InetTest.Program::Http [IP: 002f] ####
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
01/01/2009 01:28:53: Request: EXCEPTION
The thread '<No Name>' (0x3) has exited with code 0 (0x0).
01/01/2009 01:28:53: Led: ON
01/01/2009 01:28:53: Led: OFF
01/01/2009 01:28:54: Led: ON
01/01/2009 01:28:55: Led: OFF
01/01/2009 01:28:55: Led: ON
01/01/2009 01:28:56: Led: OFF
01/01/2009 01:28:56: Led: ON
01/01/2009 01:28:57: Led: OFF
01/01/2009 01:28:57: Led: ON
01/01/2009 01:28:58: Led: OFF
01/01/2009 01:28:58: Led: ON

As you can see after Request: START there is no blinking of LED for 35 seconds.

Can you comment out all Debug.Print calls and see what’s happening?

The delay of not blinking LED is the same.

and what if you deploy a release version?

Still the same.

Then I’m out of guesses. Seems like the DNS request blocks the whole thread sheduler…

NETMF doesn’t have preemptive multitasking, it’s cooperative. That means if something (in this case, the network stack) doesn’t release the CPU back to the framework, then nothing else gets done until it does.

I think.

Do You see any workaround for that?

It is really unacceptable that system will be freezed for 30 seconds.

Jack

I agree with you. I think you should post your code and output to Microsoft to comment. Keep us updated please.

www.netmf.com

The simple fix is to reduce the timeout for sockets. Other than that, you’d need asynchronous I/O, which isn’t a minor feature.