Main Site Documentation

Socket.connect hangs/bug EMX?


#1

Hi,

I have a huge issue. Working on EMX VS2012 MF 4.2

Have a client application that is supposed to connect to a given server ip and port.
When server is listening, all is fine, but if server is unreachable/off the socket.connect call hangs forever, even when server starts listening after the call to connect is done.
I run the client code in separate thread:

               // Create socket and connect to the server's IP address and port
                Socket socket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(ep); //Never returns if cable is disconnected or server away!!!
                return socket;

It does not throw any exceptions or anything, just hangs.

I can see two approaches here:

  1. Hardware reboot using watchdog, dirty
  2. Kill the thread and start again, have not tried this but can see issues with memory etc in the end if this is an underlying bug/issue.
    I have many threads that listen on my device and they all work without any problem:
  • Telnet
  • WEB
  • UDP
  • Remote flash
    I only have this issue when I try to connect out from my device. This is basic socket TCP/IP function and it me that this would be an issue!!

Hope anyone can help me out, this is a very important aspect of my product and need to work…

Best regards
Thomas


#2

you may have run into a known issue see: http://netmf.codeplex.com/workitem/1830

After searching old Forum postings for work-arounds, it appears the watchdog/reset solution is the preferred workaround, for example see: posting #7 in http://www.ghielectronics.com/community/forum/topic?id=12099&page=1#msg123125


#3

I agree, preferred is relative. And I forgot the posting about ExecutionConstraint which probably is easier to code (still relative).


#4

Hi and thanks for the quick response.

A full system restart is not a good way to solve this, my system is supposed to do many things and if it keeps restarts, every minute if the server is away other functions will suffer.
Thanks for the workaround tips will try them out.
Anyhow it looks like this problem has hang around for a while and it baffles me that this issue is not fixed ASAP. Socket.Connect should have a working timeout property in my mind…

Will try to work around this issue.


#5

ExecutionConstraint worked just fine, easy and quick. Not have time to do long time testing though. Have tried a number of connect disconnect scenarios and remove ethernet cable from EMX all handled like expected.
Thanks all but still, this is a bug 
For now hardcoded a 10 sec interval, not sure how aggressive I should be.

Thanks for the help, here are my connect code:

        private Socket ConnectSocket(String server, Int32 port)
        {

            try
            {
                IPAddress hostIP = IPAddress.Parse(server);
                IPEndPoint ep = new IPEndPoint(hostIP, port);
                // Create socket and connect to the server's IP address and port
                Socket socket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                ExecutionConstraint.Install(10000, 0);
                tryConnect = true;
                socket.Connect(ep);
                ExecutionConstraint.Install(-1, 0);
                return socket;
            }
            catch
            {
                return null;
            }
        }

#6

@ andre.m I was a bit quick to say ok to this fix. Every approximate 30min I get a full system crash!!
This was what I was afraid of because the underlying socket.connect code fails I guess it does not get cleaned up correct so eventually the system crashes.
Any comments/thoughts are appreciated.

ERROR: Undef Instr
cpsr=0x00000010
pc =0x129e615f
lr =0x0000adcb
r0=0x4000f2b4
r1=0x00000000
r2=0xa0e5f96e
r3=0x00000000
r4=0x00930a28
r5=0x00000000
r6=0x4000f2b4
r7=0x4000f268
r8=0x0cd310e2
r9=0x8a881134
r10=0x5040a546
r11=0x70cc0742
r12=0xa0000fd5
sp =0x4000577c
ERROR: Undef Instr
cpsr=0x00000010
pc =0x129e615f
lr =0x0000adcb
r0=0x4000f2b4
r1=0x00000000
r2=0xa0e5f96e
r3=0x00000000
r4=0x00930a28
r5=0x00000000
r6=0x4000f2b4
r7=0x4000f268
r8=0x0cd310e2
r9=0x8a881134
r10=0x5040a546
r11=0x70cc0742
r12=0xa0000fd5
sp =0x4000577c
[0x4000577c] : ff ff ff ff ac f2 00 40 e2 10 d3 0c 34 11 88 8a ÿÿÿÿ¬ò.@ â.Ó.4.
[0x4000578c] : 46 a5 40 50 42 07 cc 70 00 00 00 00 f9 0f b0 45 F¥@ PB.Ìp…ù.°E
[0x4000579c] : 28 0a 93 00 00 00 00 00 b4 f2 00 40 68 f2 00 40 (…´ò.@ hò.@
[0x400057ac] : 00 00 00 00 ac f2 00 40 e2 10 d3 0c 34 11 88 8a …¬ò.@ â.Ó.4.
[0x400057bc] : 46 a5 40 50 42 07 cc 70 00 00 00 00 f9 0f b0 45 F¥@ PB.Ìp…ù.°E
[0x400057cc] : 28 0a 93 00 00 00 00 00 b4 f2 00 40 68 f2 00 40 (…´ò.@ hò.@
[0x400057dc] : 00 00 00 00 ac f2 00 40 e2 10 d3 0c 34 11 88 8a …¬ò.@ â.Ó.4.
[0x400057ec] : 46 a5 40 50 42 07 cc 70 00 00 00 00 f9 0f b0 45 F¥@ PB.Ìp…ù.°E
[0x400057fc] : 28 0a 93 00 00 00 00 00 b4 f2 00 40 68 f2 00 40 (…´ò.@ hò.@
[0x4000580c] : 00 00 00 00 ac f2 00 40 e2 10 d3 0c 34 11 88 8a …¬ò.@ â.Ó.4.
[0x4000581c] : 46 a5 40 50 42 07 cc 70 00 00 00 00 f9 0f b0 45 F¥@ PB.Ìp…ù.°E
[0x4000582c] : 28 0a 93 00 00 00 00 00 b4 f2 00 40 68 f2 00 40 (…´ò.@ hò.@
[0x4000583c] : 00 00 00 00 ac f2 00 40 e2 10 d3 0c 34 11 88 8a …¬ò.@ â.Ó.4.
[0x4000584c] : 46 a5 40 50 42 07 cc 70 00 00 00 00 f9 0f b0 45 F¥@ PB.Ìp…ù.°E
[0x4000585c] : 28 0a 93 00 00 00 00 00 b4 f2 00 40 68 f2 00 40 (…´ò.@ hò.@
[0x4000586c] : 00 00 00 00 ac f2 00 40 e2 10 d3 0c 34 11 88 8a …¬ò.@ â.Ó.4.


#7

@ andre.m but the other solution was basically the same as using ExecutionConstraint but make a thread handler myself. I will not be to assertive but I guess a enough calls to socket.Connect without a listener in the other end will result in a system crash regardless of how you kill it when it does not return. Will see if I get time to try the other solution also…
I suspect there are some underlying resource issue when socket.Connect does not return but that is only speculation on my end.


#8

By doing the watchdog approach this problem will never arise…


#9

Only one, have 4 listener sockets but only one connect, I always wait for the one connect to return or timeout of 10sec using ExecutionConstraint


#10

But like I wrote earlier that is a bad fix, during system boot other functions of my system will suffer. While waiting for a valid server we need to buffer some data and monitor inputs…

In some installations we have a local server that we can wait for connections from, but if the unit is standalone behind firewalls and what not we need it to connect out. If someone forget to disable this feature if not in use the system will suffer if it reboots every 10sec.


#11

I will try your approach with the thread and timer, what timeout are you using if you dont mind me asking?

Sad it takes so long to fix stuff, especially things like this, I would think socket.connect is a critical functions to any Ethernet enabled device these days…


#12

I had same problems on EMX with 4.1 and Im fix it with opening connection in other thread and in new thread check for timeout and then close it… Im post somewhere on old forum snippet how to do it…


#13

This one:
https://www.ghielectronics.com/community/forum/topic?id=6126

P.S. Im fix link to new codeshare


#14

Hello everybody,

if this is a bug, are there anyone who know what default timeout value should be? And is there anyway to change this timeout? (except for 2 ways from this link) because as I l know, socket.connect has only 1 param (Endpoint)


#15

@ andre.m -

Can you please show me how to set it directly please!

I only found something like this: http://stackoverflow.com/questions/1062035/how-to-config-socket-connect-timeout-in-c-sharp

Look like they write a wrapper socket class or use a thread to check timeout expired. It doesn’t set timeout directly.


#16

MS said that it is fixed in 4.3, I am wondering that if it is fixed like default 15 secs, (4.2 hangs forever).

In case user want to reduce that value, how do they set it? :))))
So 4.3 may go back with our current solutions, use a wrapper class or another thread to reduce time out value. :)))


#17

@ andre.m -

100% agree. I just wanted to improve it, but it looks like using wrapper is the best solution now.


#18

Hi all, been on summer holyday so have not been able to try the wrapper solution.
The ExecutionConstraint solution crashes every 30-40min so I hope the thread wrapper works better.

Are there any ETA from GHI for 4.3 support, cant fins any release date? (Q3, Q4, 2014)


#19

Please see https://www.ghielectronics.com/docs/41/netmf-4.3-developer


#20

Gus, have seen that but it does not give any hint of date/month or quarter (quote: GHI Electronics will hold on releasing NETMF 4.3 for some time) so thats the reason for me asking. I also have understood it that the problem in this thread are solved in 4.3?

It also looks that 4.3 has better support for webservices, have not been looking at that yet but may need to in a couple of months.