Main Site Documentation

Cobra freezed until SocketException is thrown


#1

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


#2

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


#3

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?


#4

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.


#5

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


#6

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?


#7

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.


#8

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.


#9

HttpWebRequest is done in seperate thread.

Loop of blinking LED is status that Cobra is working.


#10

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.


#11

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


#12

The delay of not blinking LED is the same.


#13

and what if you deploy a release version?


#14

Still the same.


#15

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


#16

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.


#17

Do You see any workaround for that?

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

Jack


#18

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

www.netmf.com


#19

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.