Unhandled System.Net.Sockets.SocketException during test run

1,981 views
Skip to first unread message

Johan Nilsson

unread,
Mar 4, 2011, 3:42:56 AM3/4/11
to NUnit-Discuss
Hi,

I get an intermittent System.Net.Sockets.SocketException thrown during
test passes that seems to originate from NUnit. This happens in a
project with many test assemblies and the failure occurs when running
tests from varying assemblies (i.e. not in the same assembly and not
every time).

The assemblies under test are .NET 4.0 assemblies.

Sample output from the console test runner below (as you can see the
tests themselves pass, however the exception causes my CI server to
signal a build failure).

---
NUnit version 2.5.9.10348
Copyright (C) 2002-2009 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A.
Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment -
OS Version: Microsoft Windows NT 5.1.2600 Service Pack 3
CLR Version: 2.0.50727.3615 ( Net 2.0 )

ProcessModel: Default DomainUsage: Single
Execution Runtime: Default
.......................
Tests run: 23, Errors: 0, Failures: 0, Inconclusive: 0, Time:
1,046875 seconds
Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

Unhandled Exception:
System.Net.Sockets.SocketException: An existing connection was
forcibly closed by the remote host

Server stack trace:
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags)
at System.Runtime.Remoting.Channels.SocketStream.Read(Byte[]
buffer, Int32 offset, Int32 size)
at
System.Runtime.Remoting.Channels.SocketHandler.ReadFromSocket(Byte[]
buffer, Int32 offset, Int32 count)
at System.Runtime.Remoting.Channels.SocketHandler.Read(Byte[]
buffer, Int32 offset, Int32 count)
at
System.Runtime.Remoting.Channels.SocketHandler.ReadAndMatchFourBytes(Byte[]
buffer)
at
System.Runtime.Remoting.Channels.Tcp.TcpSocketHandler.ReadAndMatchPreamble()
at
System.Runtime.Remoting.Channels.Tcp.TcpSocketHandler.ReadVersionAndOperation(UInt16&
operation)
at
System.Runtime.Remoting.Channels.Tcp.TcpClientSocketHandler.ReadHeaders()
at
System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.ProcessMessage(IMessage
msg, ITransportHeaders requestHeaders, Stream requestStream,
ITransportHeaders& responseHeaders, Stream& responseStream)
at
System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMessage(IMessage
msg)

Exception rethrown at [0]:
at
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
reqMsg, IMessage retMsg)
at
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&
msgData, Int32 type)
at NUnit.Core.TestAgent.Stop()
at NUnit.Util.ProcessRunner.Dispose()
at NUnit.ConsoleRunner.ConsoleUi.Execute(ConsoleOptions options)
at NUnit.ConsoleRunner.Runner.Main(String[] args)
---

Any hints on how to troubleshoot this?

Regards / Johan

Morgan Persson

unread,
Mar 4, 2011, 3:58:25 AM3/4/11
to nunit-...@googlegroups.com
HI
Can you see nunits exit code in your build log?
I have seen -100 from nunit when my code throw exceptions after the assert's, for example in a Dispose method.
I'm guessing you need to look at your cleanup code for socket handling :)

/Morgan


--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To post to this group, send email to nunit-...@googlegroups.com.
To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.


Johan Nilsson

unread,
Mar 4, 2011, 4:37:17 AM3/4/11
to NUnit-Discuss
Hi,

On Mar 4, 9:58 am, Morgan Persson <lazy...@gmail.com> wrote:
> HI
> Can you see nunits exit code in your build log?
> I have seen -100 from nunit when my code throw exceptions after the
> assert's, for example in a Dispose method.

Yeah, the exit code is -100.

> I'm guessing you need to look at your cleanup code for socket handling :)

Sounds like a plausible explanation, but ...

- I have no unit tests opening or closing sockets; at least not
directly from the code I control. Neither are there any obvious places
where this could happen implicitly inside the tests.
- In one of the assemblies under test (that have failed) there is
actually an implementation of a FTP upload using an FtpWebRequest, but
this is only created explicitly and is replaces by a fake in all
tests.
- In another of the assemblies under test that have also failed, there
are no (obvious) references to sockets at all.

There might of course still be some indirect references to sockets
that I'm unaware of, but I have a hard time finding anything.

Thanks / Johan

Charlie Poole

unread,
Mar 4, 2011, 11:24:34 AM3/4/11
to nunit-...@googlegroups.com
Hi Johan,

As far as I can tell, the SocketException is not connected with your
code directly.

It takes place at the point where NUnit is attempting to call the remote
nunit-agent process to stop it. The process has closed the connection for
some reason.

Because all error information is reported across the connecting socket, it may
provide more useful information if you can force nunit-console to run under
.NET 4.0 in the first place. To avoid changing nunit's own config
file, I suggest
you set the COMPLUS_Version environment variable to v.4.21006 before
running nunit-console.

Charlie

Johan Nilsson

unread,
Mar 7, 2011, 4:45:38 AM3/7/11
to NUnit-Discuss
Hi,

On Mar 4, 5:24 pm, Charlie Poole <nunit...@gmail.com> wrote:
> Hi Johan,
>
> As far as I can tell, the SocketException is not connected with your
> code directly.
>
> It takes place at the point where NUnit is attempting to call the remote
> nunit-agent process to stop it. The process has closed the connection for
> some reason.
>
> Because all error information is reported across the connecting socket, it may
> provide more useful information if you can force nunit-console to run under
> .NET 4.0 in the first place. To avoid changing nunit's own config
> file, I suggest
> you set the COMPLUS_Version environment variable to v.4.21006 before
> running nunit-console.

I'll try that and get back with more information.

/ Johan

Johan Nilsson

unread,
Mar 24, 2011, 8:06:09 AM3/24/11
to NUnit-Discuss
[inlined]
Well now, here I am.

Haven't seen the problem since we started to run nunit-console
under .NET 4 as well, which is now more than two weeks ago.
Coincidence, or ...?

/ Johan

Charlie Poole

unread,
Mar 24, 2011, 11:33:46 AM3/24/11
to nunit-...@googlegroups.com
Not a coincidence.

If you are not using nunit-agent (because you already run under 4.0) then
you NUnit is not using sockets at all. So you can't have a SocketException. :-)

Glad it helped. We'll continue to work on the underlying problem.

Charlie

Simone Busoli

unread,
May 13, 2013, 1:31:19 PM5/13/13
to NUnit-Discuss
Henry, I would suggest that you create a new thread, this thread is years old now. Regardless, please provide some more details, you are doing I/O all over the place in your code, a SocketException might be coming from pretty much anywhere.


On Mon, May 13, 2013 at 4:46 PM, Henry Karuhanga <ehenr...@gmail.com> wrote:
I am trying to post data to a server using the SM5100B programmed using Netduino 2 but my code is showing an unhandled sockets exception.

I have appended my code for review just in case. Any help will greatly appreciated.

Thank you.

using System;
using System.IO;
using System.Net;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Text;
using System.IO.Ports;

namespace Send_SMS
{
    public class Program
    {
        static SerialPort port = null;
        static byte[] buffer = null;
        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        public static void Main()
        {
            port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
            port.Handshake = Handshake.None;
            port.Open();
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            buffer = Encoding.UTF8.GetBytes("AT\r");
            port.Write(buffer, 0, buffer.Length);
            Thread.Sleep(10000);
            byte[] buffer2 = Encoding.UTF8.GetBytes("AT+CGATT=1\r");
            port.Write(buffer2, 0, buffer2.Length);
            Thread.Sleep(10000);
            byte[] buffer3 = Encoding.UTF8.GetBytes("AT+CIPSTATUS");
            port.Write(buffer3, 0, buffer3.Length);
            Thread.Sleep(10000);

            byte[] buffer4 = Encoding.UTF8.GetBytes("AT+CIPHEAD=1");
            port.Write(buffer4, 0, buffer4.Length);

            byte[] buffer5 = Encoding.UTF8.GetBytes("AT+CDNSORIP=1");
            port.Write(buffer5, 0, buffer5.Length);
            Thread.Sleep(10000);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dops.ubunifuauction.com/start.php?");
            //bus=1&stage=2&source=3&destination=4&long=0.336401&lat=32.565022 ");
            request.Method = "POST";
            request.Headers.Add("Content-Type", "text/x-gwt-rpc; charset=utf-8");
            string postData = "Test";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            /*byte[] buffer6 = Encoding.UTF8.GetBytes("AT+CIPSTART=TCP"+""+"80");
            port.Write(buffer6, 0, buffer6.Length);
            Thread.Sleep(10000);*/
            byte[] buffer7 = Encoding.UTF8.GetBytes("AT+SSTRSEND=1" + "ABCD");
            port.Write(buffer7, 0, buffer7.Length);
            Thread.Sleep(10000);
            byte[] buffer8 = Encoding.UTF8.GetBytes("AT+CIPSEND");
            port.Write(buffer8, 0, buffer8.Length);
            Thread.Sleep(10000);
            Thread.Sleep(-1);


        }

        static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {


            Debug.Print(port.BytesToRead + "");

            byte[] bytes = new byte[25];

            while (port.BytesToRead > 0)
            {
                port.Read(bytes, 0, bytes.Length);

            }

            led.Write(true);  // turn on the LED
            Thread.Sleep(500); // sleep for 5s
            led.Write(false);//turn o

           
        }

    }
}

--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discus...@googlegroups.com.

To post to this group, send email to nunit-...@googlegroups.com.

Henry Karuhanga

unread,
May 13, 2013, 1:43:40 PM5/13/13
to nunit-...@googlegroups.com
Hi Simone,
I would appreciate some guidance. I have been all over google and I don't seem to find an alternative approach. Thanx

Simone Busoli

unread,
May 13, 2013, 1:45:03 PM5/13/13
to NUnit-Discuss
An alternative approach to what? We need more details before anyone can suggest an alternative approach :)
To start with, do you have a stack trace?


On Mon, May 13, 2013 at 7:43 PM, Henry Karuhanga <ehenr...@gmail.com> wrote:
Hi Simone,
I would appreciate some guidance. I have been all over google and I don't seem to find an alternative approach. Thanx

Henry Karuhanga

unread,
May 13, 2013, 1:55:02 PM5/13/13
to nunit-...@googlegroups.com
Am using an SM5100B GSM/GPRS modem interconnected to a Netduino 2. The encoding portion of the code is for writing the At commands to the modem's UART.

Simone Busoli

unread,
May 13, 2013, 1:57:22 PM5/13/13
to NUnit-Discuss
I can see that from your code, but not knowing what exactly fails makes it difficult to help you out. Do you have the stack trace of the exception you get?


On Mon, May 13, 2013 at 7:55 PM, Henry Karuhanga <ehenr...@gmail.com> wrote:
Am using an SM5100B GSM/GPRS modem interconnected to a Netduino 2. The encoding portion of the code is for writing the At commands to the modem's UART.

Henry Karuhanga

unread,
May 13, 2013, 3:07:22 PM5/13/13
to nunit-...@googlegroups.com
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Http.dll
An unhandled exception of type 'System.Net.WebException' occurred in System.Http.dll


I get the above exception messages and the line  "Stream dataStream = request.GetRequestStream();" is highlighted.

Simone Busoli

unread,
May 13, 2013, 5:20:05 PM5/13/13
to NUnit-Discuss
Not very useful unfortunately, first chance exceptions are handled exceptions which don't necessarily indicate a problem. The unhandled exception at the bottom is more interesting, if you could provide a stack trace for it.
I also assumed you are running your code under NUnit, are you?


Reply all
Reply to author
Forward
0 new messages