Skip to content

Commit 9fba754

Browse files
authoredAug 6, 2021
Added various catches to prevent unobserved task exceptions, tried to prevent the exceptions by checking web-socket state (#4052)
1 parent 14252d5 commit 9fba754

File tree

6 files changed

+67
-11
lines changed

6 files changed

+67
-11
lines changed
 

‎src/HotChocolate/AspNetCore/src/AspNetCore/Subscriptions/DefaultMessagePipeline.cs

+14-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Diagnostics.CodeAnalysis;
55
using System.Linq;
6+
using System.Net.WebSockets;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using HotChocolate.AspNetCore.Subscriptions.Messages;
@@ -30,15 +31,22 @@ public async Task ProcessAsync(
3031
ReadOnlySequence<byte> slice,
3132
CancellationToken cancellationToken)
3233
{
33-
if (TryParseMessage(slice, out OperationMessage? message))
34+
try
3435
{
35-
await HandleMessageAsync(connection, message, cancellationToken);
36+
if (TryParseMessage(slice, out OperationMessage? message))
37+
{
38+
await HandleMessageAsync(connection, message, cancellationToken);
39+
}
40+
else
41+
{
42+
await connection.SendAsync(
43+
KeepConnectionAliveMessage.Default,
44+
CancellationToken.None);
45+
}
3646
}
37-
else
47+
catch (WebSocketException)
3848
{
39-
await connection.SendAsync(
40-
KeepConnectionAliveMessage.Default,
41-
CancellationToken.None);
49+
// we will just stop receiving
4250
}
4351
}
4452

‎src/HotChocolate/AspNetCore/src/AspNetCore/Subscriptions/KeepConnectionAliveJob.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net.WebSockets;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using HotChocolate.AspNetCore.Subscriptions.Messages;
@@ -52,6 +53,10 @@ await _connection.SendAsync(
5253
{
5354
// the message processing was canceled.
5455
}
56+
catch (WebSocketException)
57+
{
58+
// we will just stop receiving
59+
}
5560
}
5661
}
5762
}

‎src/HotChocolate/AspNetCore/src/AspNetCore/Subscriptions/MessageProcessor.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Buffers;
33
using System.IO.Pipelines;
4+
using System.Net.WebSockets;
45
using System.Threading;
56
using System.Threading.Tasks;
67

@@ -67,6 +68,10 @@ await _pipeline.ProcessAsync(
6768
}
6869
}
6970
catch(OperationCanceledException) when (cancellationToken.IsCancellationRequested) { }
71+
catch (WebSocketException)
72+
{
73+
// we will just stop receiving
74+
}
7075
finally
7176
{
7277
// reader should be completed always, so that related pipe writer can

‎src/HotChocolate/AspNetCore/src/AspNetCore/Subscriptions/WebSocketConnection.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Task SendAsync(
6363
{
6464
WebSocket? webSocket = _webSocket;
6565

66-
if (_disposed || webSocket == null)
66+
if (_disposed || webSocket == null || webSocket.State != WebSocketState.Open)
6767
{
6868
return Task.CompletedTask;
6969
}
@@ -95,6 +95,11 @@ public async Task ReceiveAsync(
9595

9696
if (success)
9797
{
98+
if (webSocket.State != WebSocketState.Open)
99+
{
100+
break;
101+
}
102+
98103
try
99104
{
100105
socketResult = await webSocket.ReceiveAsync(buffer, cancellationToken);
@@ -122,6 +127,10 @@ public async Task ReceiveAsync(
122127
{
123128
// we will just stop receiving
124129
}
130+
catch (WebSocketException)
131+
{
132+
// we will just stop receiving
133+
}
125134
}
126135

127136
public async Task CloseAsync(
@@ -133,7 +142,7 @@ public async Task CloseAsync(
133142
{
134143
WebSocket? webSocket = _webSocket;
135144

136-
if (_disposed || Closed || webSocket is null)
145+
if (_disposed || Closed || webSocket is null || webSocket.State != WebSocketState.Open)
137146
{
138147
return;
139148
}

‎src/HotChocolate/Core/src/Execution/Processing/ResolverTask.cs

+25-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading;
5+
using System.Threading.Channels;
56
using System.Threading.Tasks;
67
using HotChocolate.Types;
78

@@ -34,6 +35,10 @@ private async ValueTask TryExecuteAndCompleteAsync(CancellationToken cancellatio
3435
CompleteValue(success, cancellationToken);
3536
}
3637
}
38+
catch (ObjectDisposedException)
39+
{
40+
IsCanceled = true;
41+
}
3742
catch
3843
{
3944
IsCanceled = true;
@@ -52,10 +57,28 @@ private async ValueTask TryExecuteAndCompleteAsync(CancellationToken cancellatio
5257
}
5358
else
5459
{
55-
_operationContext.Execution.TaskStats.TaskCompleted();
60+
try
61+
{
62+
_operationContext.Execution.TaskStats.TaskCompleted();
63+
}
64+
catch (ChannelClosedException)
65+
{
66+
}
67+
catch (ObjectDisposedException)
68+
{
69+
}
5670
}
5771

58-
_operationContext.Execution.TaskPool.Return(this);
72+
try
73+
{
74+
_operationContext.Execution.TaskPool.Return(this);
75+
}
76+
catch (ChannelClosedException)
77+
{
78+
}
79+
catch (ObjectDisposedException)
80+
{
81+
}
5982
}
6083
}
6184

‎src/HotChocolate/Core/src/Fetching/BatchScheduler.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ private async ValueTask ExecuteAsync(CancellationToken cancellationToken)
110110
finally
111111
{
112112
IsCanceled = cancellationToken.IsCancellationRequested;
113-
_context.Completed();
113+
try
114+
{
115+
_context.Completed();
116+
}
117+
catch (ObjectDisposedException)
118+
{
119+
}
114120
}
115121
}
116122
}

0 commit comments

Comments
 (0)
Please sign in to comment.