diff --git a/src/Transports.Subscriptions.WebSockets/WebSocketReaderPipeline.cs b/src/Transports.Subscriptions.WebSockets/WebSocketReaderPipeline.cs index 7f065653..2dd7ff27 100644 --- a/src/Transports.Subscriptions.WebSockets/WebSocketReaderPipeline.cs +++ b/src/Transports.Subscriptions.WebSockets/WebSocketReaderPipeline.cs @@ -42,26 +42,23 @@ public void LinkTo(ITargetBlock target) public async Task Complete(WebSocketCloseStatus closeStatus, string statusDescription) { - if (_socket.State != WebSocketState.Closed && _socket.State != WebSocketState.CloseSent) + if (_socket.State != WebSocketState.Closed && _socket.State != WebSocketState.CloseSent) + try { - if (closeStatus == WebSocketCloseStatus.NormalClosure) - { - // If nothing went wrong, close connection with handshakes. - await _socket.CloseOutputAsync( - closeStatus, - statusDescription, - CancellationToken.None); - } - else - { - // Something went wrong, so don't wait for answer from the other side, just close the connection. - await _socket.CloseAsync( - closeStatus, - statusDescription, - CancellationToken.None); - } - - _startBlock.Complete(); + if (closeStatus == WebSocketCloseStatus.NormalClosure) + await _socket.CloseAsync( + closeStatus, + statusDescription, + CancellationToken.None); + else + await _socket.CloseOutputAsync( + closeStatus, + statusDescription, + CancellationToken.None); + } + finally + { + _startBlock.Complete(); } }