SignalR Core throws unobserved task exceptions
See original GitHub issueDescription
SignalR Core client used in WPF application throws an unhandled task exception if a connection was closed.
To Reproduce
Steps to reproduce the behavior:
- Using SignalR Core v1.1.0 on WPF .NET Framework 4.6.2
- Create HubConnection and connect to a server
- Stop server or block client so that it disconnects
- HubConnection is closed
- After GC has collected an unhandled task exception is raised
Expected behavior
HubConnection.Close containing an exception is fired. No additional exceptions are thrown, or at least not the ones that cannot be handled.
Stacktrace
There are multiple variants of an exception occuring. I can provide them if needed.
AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> WebSocketException: The 'System.Net.WebSockets.InternalClientWebSocket' instance cannot be used for communication because it has been transitioned into the 'Aborted' state. ---> WebSocketException: An internal WebSocket error occurred. Please see the innerException, if present, for more details. ---> IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> SocketException: An established connection was aborted by the software in your host machine
at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult) --- End of inner exception stack trace ---
at System.Net.Security._SslStream.EndRead(IAsyncResult asyncResult)
at System.Net.TlsStream.EndRead(IAsyncResult asyncResult)
at System.Net.PooledStream.EndRead(IAsyncResult asyncResult)
at System.IO.Stream+<>c.<BeginEndReadAsync>b__43_1(Stream stream, IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1+FromAsyncTrimPromise`1.Complete(TInstance thisRef, Func`3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketConnectionStream+<ReadAsync>d__21.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at System.Net.WebSockets.WebSocketBase+WebSocketOperation+<Process>d__19.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.WebSockets.WebSocketBase+<ReceiveAsyncCore>d__45.MoveNext() --- End of inner exception stack trace ---
at System.Net.WebSockets.WebSocketBase.ThrowIfAborted(Boolean aborted, Exception innerException)
at System.Net.WebSockets.WebSocketBase.ThrowIfConvertibleException(String methodName, Exception exception, CancellationToken cancellationToken, Boolean aborted)
at System.Net.WebSockets.WebSocketBase+<ReceiveAsyncCore>d__45.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport+<StartReceiving>d__19.MoveNext() --- End of inner exception stack trace ---
at System.Net.WebSockets.WebSocketBase.ThrowIfAborted(Boolean aborted, Exception innerException)
at System.Net.WebSockets.WebSocketBase.ThrowIfConvertibleException(String methodName, Exception exception, CancellationToken cancellationToken, Boolean aborted)
at System.Net.WebSockets.WebSocketBase+<ReceiveAsyncCore>d__45.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport+<StartReceiving>d__19.MoveNext() --- End of inner exception stack trace ---
at System.Threading.Tasks.TaskScheduler.PublishUnobservedTaskException(Object sender, UnobservedTaskExceptionEventArgs ueea)
at System.Threading.Tasks.TaskExceptionHolder.Finalize()
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
SignalR Core throws unobserved task exceptions #10210
Description SignalR Core client used in WPF application throws an unhandled task exception if a connection was closed.
Read more >How to catch this SignalR exception
All of my code is wrapped in try/catch statements and I have an unobserved exception handler defined using TaskScheduler.UnobservedTaskException ...
Read more ><ThrowUnobservedTaskExceptions> Element
The following example demonstrates how an unobserved exception is thrown from a task. The code must be run as a released program to...
Read more >HubException Class (Microsoft.AspNetCore.SignalR)
Initializes a new instance of the HubException class with a specified error message and a reference to the inner exception that is the...
Read more >C# Async/Await Interview Questions And Answers (2023)
Properly handle exceptions: When using Task.Run , exceptions thrown within the delegate are propagated when you await the task. Be sure to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

We’ll look in to this in preview 7. Since it doesn’t actually cause a functional error, it’s not a super-high priority for us, but it’s a ugly thing to have to ignore in that event handler so we want to clean that up. We need to make sure we fully understand why that exception is rethrown before suppressing it 😃.
Closing this as we have done some further suppressing of unobserved exceptions. Please let us know if this repros for you on 3.0.0-preview8 or higher (not yet released, and I don’t have a specific release date, but it should be fairly soon since we’re about to branch the code and start final stabilization for it).