diff --git a/dotnet/src/webdriver/BiDi/Communication/Broker.cs b/dotnet/src/webdriver/BiDi/Communication/Broker.cs index 3ad14385aa814..77b592968b850 100644 --- a/dotnet/src/webdriver/BiDi/Communication/Broker.cs +++ b/dotnet/src/webdriver/BiDi/Communication/Broker.cs @@ -123,21 +123,33 @@ public async Task ConnectAsync(CancellationToken cancellationToken) private async Task ReceiveMessagesAsync(CancellationToken cancellationToken) { - while (!cancellationToken.IsCancellationRequested) + try { - try + while (!cancellationToken.IsCancellationRequested) { var data = await _transport.ReceiveAsync(cancellationToken).ConfigureAwait(false); - ProcessReceivedMessage(data); - } - catch (Exception ex) - { - if (cancellationToken.IsCancellationRequested is not true && _logger.IsEnabled(LogEventLevel.Error)) + try { - _logger.Error($"Couldn't process received BiDi remote message: {ex}"); + ProcessReceivedMessage(data); } + catch (Exception ex) + { + if (_logger.IsEnabled(LogEventLevel.Error)) + { + _logger.Error($"Unhandled error occured while processing remote message: {ex}"); + } + } + } + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + if (_logger.IsEnabled(LogEventLevel.Error)) + { + _logger.Error($"Unhandled error occured while receiving remote messages: {ex}"); } + + throw; } }