Skip to content

Commit c211394

Browse files
committed
ignore task cancelled exceptions on app shutdown
1 parent 887093d commit c211394

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

Common/Websocket/WebsockBaseController.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,15 @@ await HttpContext.Response.WriteAsJsonAsync(
169169
// We send close if the client sent a close message though
170170
if (WebSocket is { State: WebSocketState.Open or WebSocketState.CloseReceived })
171171
{
172-
await WebSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Normal closure",
173-
LinkedToken);
172+
try
173+
{
174+
await WebSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Normal closure",
175+
LinkedToken);
176+
}
177+
catch (TaskCanceledException) when (lifetime.ApplicationStopping.IsCancellationRequested)
178+
{
179+
// Ignore, this happens when the application is shutting down
180+
}
174181
}
175182
}
176183

@@ -209,7 +216,7 @@ protected virtual Task SendWebSocketMessage(T message, WebSocket websocket, Canc
209216

210217
#endregion
211218

212-
private CancellationTokenSource _receiveCancellationTokenSource = new();
219+
private readonly CancellationTokenSource _receiveCancellationTokenSource = new();
213220

214221
/// <summary>
215222
/// Main receiver logic for the websocket

0 commit comments

Comments
 (0)