Skip to content

Commit e752e63

Browse files
committed
Handle cancellation
1 parent 92bc738 commit e752e63

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

src/Dotnet.Watch/AspireService/AspireServerService.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -394,34 +394,31 @@ private async ValueTask<bool> SendMessageAsync(string dcpId, byte[] messageBytes
394394
return false;
395395
}
396396

397-
var success = false;
397+
using var cancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
398+
cancellationToken, _shutdownCancellationSource.Token, connection.HttpRequestAborted);
399+
398400
var lockAcquired = false;
399401
try
400402
{
401-
using var cancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
402-
cancellationToken, _shutdownCancellationSource.Token, connection.HttpRequestAborted);
403-
404403
await _webSocketAccess.WaitAsync(cancelTokenSource.Token);
405404
lockAcquired = true;
406405

407406
await connection.Socket.SendAsync(new ArraySegment<byte>(messageBytes), WebSocketMessageType.Text, endOfMessage: true, cancelTokenSource.Token);
408-
success = true;
407+
return true;
408+
}
409+
catch (Exception e) when (e is not OperationCanceledException)
410+
{
411+
// If the connection throws it almost certainly means the client has gone away, so clean up that connection
412+
_socketConnectionManager.RemoveSocketConnection(connection);
413+
return false;
409414
}
410415
finally
411416
{
412-
if (!success)
413-
{
414-
// If the connection throws it almost certainly means the client has gone away, so clean up that connection
415-
_socketConnectionManager.RemoveSocketConnection(connection);
416-
}
417-
418417
if (lockAcquired)
419418
{
420419
_webSocketAccess.Release();
421420
}
422421
}
423-
424-
return success;
425422
}
426423

427424
private async Task HandleStopSessionRequestAsync(HttpContext context, string sessionId)

0 commit comments

Comments
 (0)