Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Bindings;
using MongoDB.Driver.Core.Servers;
Expand Down Expand Up @@ -50,12 +51,11 @@ public static TResult Execute<TResult>(OperationContext operationContext, IRetry
}
catch (Exception ex)
{
originalException ??= ex;
if (!ShouldRetryOperation(operationContext, operation.WriteConcern, context, server, ex, attempt))
{
throw originalException ?? ex;
Rethrow(originalException);
}

originalException ??= ex;
}

deprioritizedServers ??= new HashSet<ServerDescription>();
Expand All @@ -67,12 +67,12 @@ public static TResult Execute<TResult>(OperationContext operationContext, IRetry
}
catch
{
throw originalException;
Rethrow(originalException);
}

if (!AreRetryableWritesSupported(context.ChannelSource.ServerDescription))
{
throw originalException;
Rethrow(originalException);
}

attempt++;
Expand Down Expand Up @@ -105,12 +105,11 @@ public static async Task<TResult> ExecuteAsync<TResult>(OperationContext operati
}
catch (Exception ex)
{
originalException ??= ex;
if (!ShouldRetryOperation(operationContext, operation.WriteConcern, context, server, ex, attempt))
{
throw originalException ?? ex;
Rethrow(originalException);
}

originalException ??= ex;
}

deprioritizedServers ??= new HashSet<ServerDescription>();
Expand All @@ -122,12 +121,12 @@ public static async Task<TResult> ExecuteAsync<TResult>(OperationContext operati
}
catch
{
throw originalException;
Rethrow(originalException);
}

if (!AreRetryableWritesSupported(context.ChannelSource.ServerDescription))
{
throw originalException;
Rethrow(originalException);
}

attempt++;
Expand Down Expand Up @@ -185,5 +184,11 @@ private static bool DoesContextAllowRetries(RetryableWriteContext context, Serve
private static bool IsOperationAcknowledged(WriteConcern writeConcern)
=> writeConcern == null || // null means use server default write concern which implies acknowledged
writeConcern.IsAcknowledged;

private static void Rethrow(Exception exception)
{
// Use ExceptionDispatchInfo to avoid losing the original exception stack trace
ExceptionDispatchInfo.Capture(originalException).Throw();
}
}
}