SignalR/SignalR — Issue #143: Exception when connection is restored after forceful disconnection
Author: simoneb
Created: 2012-01-22 12:11:34+00:00
State: open

Using the current revision on the repository.

Here's the server code, hosted in ASP.NET:

public class SpikeConnection : PersistentConnection
{
protected override Task OnReceivedAsync(string clientId, string data)
{
return Task.Factory.StartNew(() =>
{
var counter = 0;

while (true)
{
Send(clientId, counter++);
Thread.Sleep(20);
}
});
}
}

Here's the client code, in a console app:

class Program
{
static void Main(string[] args)
{
var connection = new Connection("http://localhost:12345/spike");

connection.Received += OnReceived;
connection.Error += Console.WriteLine;

connection.Start(Transport.LongPolling).ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client id {0}", connection.ConnectionId);
connection.Send("here i am");
}
});

Console.ReadLine();
}

private static void OnReceived(string data)
{
int received = int.Parse(data);

if(received % 100 == 0)
Console.WriteLine("Received message {0}", received);
}
}

I have a [TCP proxy] (http://www.pocketsoap.com/tcptrace/) listening on port 12345 and proxying it on whatever port the server is listening on, to mimick a network connection going down.

Here's the output when shutting down the proxy and restarting in (comments inline to show what I do, and when):

Success! Connected with client id e387fae1-8a24-4632-b40c-2b7dc67123f5
Received message 0
Received message 100
Received message 200
Received message 300

### PROXY IS DISCONNECTED HERE, the two exceptions below are printed by the Error event handler ###

System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedl
y.
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at SignalR.HttpHelper.<>c__DisplayClass2.b__0(IAsyncResult ar)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endMethod, Ta
skCompletionSource`1 tcs)
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketExcept
ion: No connection could be made because the target machine actively refused it 127.0.0.1:12345
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, So
cket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout,
Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at SignalR.HttpHelper.<>c__DisplayClass2.b__0(IAsyncResult ar)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endMethod, Ta
skCompletionSource`1 tcs)

### PROXY IS RESTARTED HERE ###

Received message 400
Received message 500
Received message 600
Received message 700
Received message 800

### THIS EXCEPTION IS THROWN BY A TASK FINALIZER (cannot be caught by client code) AND SHUTS DOWN THE APP ###

Unhandled Exception: System.AggregateException: A Task's exception(s) were not observed either by Wa
iting on the Task or accessing its Exception property. As a result, the unobserved exception was ret
hrown by the finalizer thread. ---> System.AggregateException: One or more errors occurred. ---> Sys
tem.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at SignalR.HttpHelper.<>c__DisplayClass2.b__0(IAsyncResult ar)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endMethod, Ta
skCompletionSource`1 tcs)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Threading.Tasks.TaskExceptionHolder.Finalize()
Received message 900
Received message 1000
Received message 1100
Received message 1200
Received message 1300
Received message 1400
Received message 1500
Received message 1600
Received message 1700
Received message 1800
Press any key to continue . . .