Skip to content

Commit 61061b0

Browse files
committed
Added try catch where needed
1 parent 0eed8e9 commit 61061b0

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

src/MongoDB.Driver/Core/Bindings/CoreTransaction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ internal void SetState(CoreTransactionState state)
135135
internal void ResetState()
136136
{
137137
_state = CoreTransactionState.Starting;
138-
_isEmpty = false; //TODO This is wrong
138+
_isEmpty = true;
139139
}
140140

141141
internal void UnpinAll()

src/MongoDB.Driver/Core/Operations/RetryableReadContext.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,34 @@ public void Dispose()
7777

7878
public void AcquireOrReplaceChannel(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers)
7979
{
80-
operationContext.ThrowIfTimedOutOrCanceled();
81-
ReplaceChannelSource(Binding.GetReadChannelSource(operationContext, deprioritizedServers));
82-
ReplaceChannel(ChannelSource.GetChannel(operationContext));
80+
try
81+
{
82+
operationContext.ThrowIfTimedOutOrCanceled();
83+
ReplaceChannelSource(Binding.GetReadChannelSource(operationContext, deprioritizedServers));
84+
ReplaceChannel(ChannelSource.GetChannel(operationContext));
85+
}
86+
catch
87+
{
88+
_channelSource?.Dispose();
89+
_channel?.Dispose();
90+
throw;
91+
}
8392
}
8493

8594
public async Task AcquireOrReplaceChannelAsync(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers)
8695
{
87-
operationContext.ThrowIfTimedOutOrCanceled();
88-
ReplaceChannelSource(await Binding.GetReadChannelSourceAsync(operationContext, deprioritizedServers).ConfigureAwait(false));
89-
ReplaceChannel(await ChannelSource.GetChannelAsync(operationContext).ConfigureAwait(false));
96+
try
97+
{
98+
operationContext.ThrowIfTimedOutOrCanceled();
99+
ReplaceChannelSource(await Binding.GetReadChannelSourceAsync(operationContext, deprioritizedServers).ConfigureAwait(false));
100+
ReplaceChannel(await ChannelSource.GetChannelAsync(operationContext).ConfigureAwait(false));
101+
}
102+
catch
103+
{
104+
_channelSource?.Dispose();
105+
_channel?.Dispose();
106+
throw;
107+
}
90108
}
91109

92110
private void ReplaceChannel(IChannelHandle channel)

src/MongoDB.Driver/Core/Operations/RetryableWriteContext.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,42 @@ public void Dispose()
7878
}
7979
}
8080

81-
//TODO Do this inside the main loop, but remember that this follows reads retryability logic, even with writes
8281
public void AcquireOrReplaceChannel(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers, IMayUseSecondaryCriteria mayUseSecondaryCriteria = null)
8382
{
84-
operationContext.ThrowIfTimedOutOrCanceled();
85-
var writeChannelSource = mayUseSecondaryCriteria == null //TODO The implementation of those two overloads is different, I'm worried there some important difference I can't appreciate
86-
? Binding.GetWriteChannelSource(operationContext, deprioritizedServers)
87-
: Binding.GetWriteChannelSource(operationContext, deprioritizedServers, mayUseSecondaryCriteria);
88-
ReplaceChannelSource(writeChannelSource);
89-
ReplaceChannel(ChannelSource.GetChannel(operationContext));
83+
try
84+
{
85+
operationContext.ThrowIfTimedOutOrCanceled();
86+
var writeChannelSource = mayUseSecondaryCriteria == null //TODO The implementation of those two overloads is different, I'm worried there some important difference I can't appreciate
87+
? Binding.GetWriteChannelSource(operationContext, deprioritizedServers)
88+
: Binding.GetWriteChannelSource(operationContext, deprioritizedServers, mayUseSecondaryCriteria);
89+
ReplaceChannelSource(writeChannelSource);
90+
ReplaceChannel(ChannelSource.GetChannel(operationContext));
91+
}
92+
catch
93+
{
94+
_channelSource?.Dispose();
95+
_channel?.Dispose();
96+
throw;
97+
}
9098
}
9199

92100
public async Task AcquireOrReplaceChannelAsync(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers, IMayUseSecondaryCriteria mayUseSecondaryCriteria = null)
93101
{
94-
operationContext.ThrowIfTimedOutOrCanceled();
95-
var writeChannelSource = mayUseSecondaryCriteria == null //TODO The implementation of those two overloads is different, I'm worried there some important difference I can't appreciate
96-
? await Binding.GetWriteChannelSourceAsync(operationContext, deprioritizedServers).ConfigureAwait(false)
97-
: await Binding.GetWriteChannelSourceAsync(operationContext, deprioritizedServers, mayUseSecondaryCriteria).ConfigureAwait(false);
98-
ReplaceChannelSource(writeChannelSource);
99-
ReplaceChannel(await ChannelSource.GetChannelAsync(operationContext).ConfigureAwait(false));
102+
try
103+
{
104+
operationContext.ThrowIfTimedOutOrCanceled();
105+
var writeChannelSource = mayUseSecondaryCriteria == null //TODO The implementation of those two overloads is different, I'm worried there some important difference I can't appreciate
106+
? await Binding.GetWriteChannelSourceAsync(operationContext, deprioritizedServers).ConfigureAwait(false)
107+
: await Binding.GetWriteChannelSourceAsync(operationContext, deprioritizedServers, mayUseSecondaryCriteria).ConfigureAwait(false);
108+
ReplaceChannelSource(writeChannelSource);
109+
ReplaceChannel(await ChannelSource.GetChannelAsync(operationContext).ConfigureAwait(false));
110+
}
111+
catch
112+
{
113+
_channelSource?.Dispose();
114+
_channel?.Dispose();
115+
throw;
116+
}
100117
}
101118

102119
private void ReplaceChannel(IChannelHandle channel)

src/MongoDB.Driver/Core/Operations/RetryableWriteOperationExecutor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ public static TResult Execute<TResult>(OperationContext operationContext, IRetry
7272
}
7373
catch (Exception ex)
7474
{
75-
var innerException = ex is MongoAuthenticationException mongoAuthenticationException ? mongoAuthenticationException.InnerException : ex;
7675
originalException ??= ex;
7776

78-
if (!ShouldRetry(operationContext, operation.WriteConcern, context, server, tokenBucket, innerException, attempt, context.Random, out var backoff))
77+
if (!ShouldRetry(operationContext, operation.WriteConcern, context, server, tokenBucket, ex, attempt, context.Random, out var backoff))
7978
{
8079
throw originalException;
8180
}

0 commit comments

Comments
 (0)